Open two dialog together - java

I have two dialog.i open dialog one and click one button in adapter listview in dialog, and i want open another dialog to ask question for are you sure delet this item or no.
But just i can open one of them at a time.
What can i do?
viewHolder.btnDelet.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
new DialogDelet(context).setOnListener(new ListenerDelet() {
#Override
public void onSuccess() {
listenerAdapterUserAndRole.unSelect(item);
}
#Override
public void onCancel() {
}
}).Show();
}
});

You can cancel first dialog when user is confirm or you want to open next dialog
like:
public void open(){
AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(this);
alertDialogBuilder.setMessage("Are you sure,
You wanted to make decision");
alertDialogBuilder.setPositiveButton("yes",
new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface arg0, int arg1) {
Toast.makeText(MainActivity.this,"You clicked yes
button",Toast.LENGTH_LONG).show();
//cancel first dialog here
firstDialogObj.cancel;
// do your stuff contnious what you want
}
});
alertDialogBuilder.setNegativeButton("No",new DialogInterface.OnClickListener() {
Override
public void onClick(DialogInterface dialog, int which) {
finish();
}
});
AlertDialog alertDialog = alertDialogBuilder.create();
alertDialog.show();
}

Related

showing alert dialog on pressing back button [duplicate]

This question already has answers here:
How to prevent going back to the previous activity?
(14 answers)
Closed 6 years ago.
I want to show an alert message on pressing back button of Android device asking user weather he is sure or not. If user press "Yes" then he should navigate to previous. in case of "NO" activity should resume. But the problem occurs when I press back button it performs both the actions. It also navigate to previous activity and displays the alert dialog.
Here is my code.
public void onBackPressed() {
super.onBackPressed();
AlertDialog.Builder alertdialog=new AlertDialog.Builder(this);
alertdialog.setTitle("Warning");
alertdialog.setMessage("Are you sure you Want to exit the tutorial???");
alertdialog.setPositiveButton("yes", new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
Intent intent=new Intent(secAddition.this,addition.class);
startActivity(intent);
secAddition.this.finish();
}
});
alertdialog.setNegativeButton("No", new DialogInterface.OnClickListener(){
#Override
public void onClick(DialogInterface dialog, int which) {
dialog.cancel();
}
});
AlertDialog alert=alertdialog.create();
alertdialog.show();
}
If you want to show the alert dialog without closing the app or clearing the stack, you should not call
super.onBackPressed();
Please check the following code snippet
#Override
public void onBackPressed() {
//super.onBackPressed();
IsFinish("Want to close app?");
}
public void IsFinish(String alertmessage) {
DialogInterface.OnClickListener dialogClickListener = new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
switch (which) {
case DialogInterface.BUTTON_POSITIVE:
android.os.Process.killProcess(android.os.Process.myPid());
// This above line close correctly
//finish();
break;
case DialogInterface.BUTTON_NEGATIVE:
break;
}
}
};
AlertDialog.Builder builder = new AlertDialog.Builder(context);
builder.setMessage(alertmessage)
.setPositiveButton("Yes", dialogClickListener)
.setNegativeButton("No", dialogClickListener).show();
}
This is what you are looking for. you don't need to startActivity on back pressed. If user select Yes than call super onBackPress
public void onBackPressed() {
AlertDialog.Builder alertdialog=new AlertDialog.Builder(this);
alertdialog.setTitle("Warning");
alertdialog.setMessage("Are you sure you Want to exit the tutorial???");
alertdialog.setPositiveButton("yes", new DialogInterface.OnClickListener(){
#Override
public void onClick(DialogInterface dialog, int which) {
super.onBackPressed();
}
});
alertdialog.setNegativeButton("No", new DialogInterface.OnClickListener(){
#Override
public void onClick(DialogInterface dialog, int which) {
dialog.cancel();
}
});
AlertDialog alert=alertdialog.create();
alertdialog.show();
}
Remove
super.onBackPressed();
This should work.
public void onBackPressed() {
AlertDialog.Builder alertdialog=new AlertDialog.Builder(this);
alertdialog.setTitle("Warning");
alertdialog.setMessage("Are you sure you Want to exit the tutorial???");
alertdialog.setPositiveButton("yes", new DialogInterface.OnClickListener(){
#Override
public void onClick(DialogInterface dialog, int which) {
Intent intent=new Intent(secAddition.this,addition.class);
startActivity(intent);
secAddition.this.finish();
}
});
alertdialog.setNegativeButton("No", new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
dialog.cancel();
}
});
AlertDialog alert=alertdialog.create();
alertdialog.show();
}
you can write like this for your code to work :
public void onBackPressed() {
AlertDialog.Builder alertdialog=new AlertDialog.Builder(this);
alertdialog.setTitle("Warning");
alertdialog.setMessage("Are you sure you Want to exit the tutorial???");
alertdialog.setPositiveButton("yes", new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
super.onBackPressed();
}
});
alertdialog.setNegativeButton("No", new DialogInterface.OnClickListener(){
#Override
public void onClick(DialogInterface dialog, int which) {
dialog.cancel();
}
});
AlertDialog alert=alertdialog.create();
alertdialog.show();
}
No need to start the Activity again if you are going to previous Activity,
In your code when you do a back press, super.onBackPressed() gets called so it goes to previous Activity

How to set AlertDialog.finish() in the AlertDialog?

I am currently using an app that needs to utilize a lot of AlertDialogs. I've currently coded a basic one here:
protected void StopButton () {
AlertDialog.Builder StopDialog = new AlertDialog.Builder(MainActivity.this);
StopDialog.setTitle(R.string.Stop_Title);
StopDialog.setMessage(R.string.Stop_Message);
StopDialog.setPositiveButton(R.string.Yes_Button, new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int id) {
((Protoype2) getApplication()).setRequestingLocationUpdates(false);
finish();
}
});
StopDialog.setNegativeButton(R.string.No_Button, new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
((Protoype2) getApplication()).setRequestingLocationUpdates(true);
}
});
StopDialog.setNeutralButton(R.string.Negative_Button, new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
//Closes box
finish();
}
});
AlertDialog alert = StopDialog.create();
alert.show();
}
The StopButton works and the Dialog comes up when I call for it. However, the finish(); function does not work.
Upon review, I found that finish(); did not finish the Dialog, rather the entire app. I know I need to get a AlertDialog.cancel in there.
The problem is this: as you can see, the AlertDialog is only created AFTER the StopDialog is finished.
How can I set an AlertDialog.finish() before StopDialog is finished?
Replace finish() with dialog.dismiss() like this:
AlertDialog.Builder StopDialog = new AlertDialog.Builder(TestActivity.this);
StopDialog.setTitle("Title");
StopDialog.setMessage("Stop");
StopDialog.setPositiveButton(R.string.yes, new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int id) {
((Protoype2) getApplication()).setRequestingLocationUpdates(false);
dialog.dismiss();
}
});
StopDialog.setNegativeButton(R.string.no, new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
((Protoype2) getApplication()).setRequestingLocationUpdates(true);
}
});
StopDialog.setNeutralButton("Neutral", new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
//Closes box
dialog.dismiss();
}
});
AlertDialog alert = StopDialog.create();
alert.show();
There is no such method finish() for AlertDialog. finish() refers to Activity class and will finish the current Activity. You should instead use dismiss().
You can use the DialogInterface object like dialog.dismiss() to dismiss the AlertDialog.
You can go through the documentation for a broader idea :)
http://developer.android.com/guide/topics/ui/dialogs.html

Popup listView scroll and with imgaes

i want to create a popup with scroll listView and the listView will contain images,
i tried implement it with this code:
button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View arg0) {
AlertDialog.Builder builder = new AlertDialog.Builder(groupContext);
builder.setTitle("Group");
builder.setItems(arrayNames, new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int item) {
Toast.makeText(getApplicationContext(), arrayNames[item], Toast.LENGTH_SHORT).show();
}
});
builder.setPositiveButton("OK ", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
System.out.println("OK CLICKED");
}
});
builder.setNegativeButton("cancel", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
dialog.dismiss();
}
});
AlertDialog alert = builder.create();
alert.show();
}
});
this code is working for me, however i cant add images and the listView is not scroll
thanks alot
You need to define a custom dialog with XML layout containing ListView.
Steps:
Define a XML layout with ListView
set XML layout inside dialog using below:
dialog = new Dialog(MyActivity.this);
dialog.setContentView(R.layout.my_listView_layout);
Check this Simple example: Android Custom Dialog Example

Click and LongClick Listener throw IllegalStateException

Im getting this error when trying to implement both onClickListener and onLongClickListener for a RelativeLayout:
10-19 17:49:31.400: E/AndroidRuntime(30886): FATAL EXCEPTION: main
10-19 17:49:31.400: E/AndroidRuntime(30886): java.lang.IllegalStateException: The specified child already has a parent. You must call removeView() on the child's parent first.
This is part of the code:
RelativeLayout meals_layout = (RelativeLayout) view
.findViewById(R.id.meals);
meals_layout.setOnLongClickListener(new OnLongClickListener() {
#Override
public boolean onLongClick(View v) {
AlertDialog.Builder builder = new AlertDialog.Builder(
JournalActivity.this);
builder.setTitle(R.string.meal_delete);
builder.setView(clickView);
builder.setPositiveButton(R.string.string_delete,
new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
}
});
builder.setNegativeButton(R.string.string_cancel,
new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog,
int which) {
// Do nothing
dialog.dismiss();
}
});
builder.create().show();
return false;
}
});
meals_layout.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
AlertDialog.Builder builder = new AlertDialog.Builder(
JournalActivity.this);
builder.setTitle(R.string.meal_edit); // al que hacerlo bonito
builder.setView(clickView);
builder.setPositiveButton(R.string.string_edit,
new DialogInterface.OnClickListener() {
}
});
builder.setNegativeButton(R.string.string_cancel,
new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog,
int which) {
// Do nothing
dialog.dismiss();
}
});
builder.create().show();
}
});
The first time I open the activity, I can do a longclick as many times as i want. But when I click any other other button (not only the one sentenced here), if I try again to do a longclick... the application crashes.
I will really appreciate your help.
Thanks :)
Each view is only allowed one parent. Apparently clickView already has one, so when you call setView(clickView) it throws the error. One fix is to add:
((GroupView) clickView.getParent()).removeView(clickView);
to the beginning of both of your listeners. This will "orphan" clickView so it can be added to the AlertDialog.

Java - Android Dialog Box via Opening Button

So I have a button with an on click listener. It's supposed to open a dialog window with options... but it doesn't.
Anyone know what the problem is?
seeTexts.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
AlertDialog.Builder builder = new AlertDialog.Builder(context);
builder.setTitle("Pick a color");
builder.setItems(items, new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int item) {
Toast.makeText(getApplicationContext(), items[item], Toast.LENGTH_SHORT).show();
}
});
AlertDialog alert = builder.create();
}
});
EDIT (UPDATED CODE):
seeTexts.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
AlertDialog.Builder builder = new AlertDialog.Builder(context);
builder.setTitle("Pick a color");
builder.setItems(items, new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int item) {
Toast.makeText(getApplicationContext(), items[item], Toast.LENGTH_SHORT).show();
}
});
AlertDialog alert = builder.create();
alert.show();
}
});
You just don't call the show method of your AlertDialog object

Categories

Resources