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
Related
I added an alert dialogue into my app and i want to user when he press yes he should exit from the complete app but it dose not. by pressing yes i just come back to previous activity
here is code
#Override
public void onBackPressed() {
new AlertDialog.Builder(Chapter1.this)
.setTitle("Exit")
.setIcon(R.drawable.ic_baseline_warning_24)
.setMessage("Are you sure you want to go back")
.setPositiveButton("yes", new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
finish();
}
}).setNeutralButton("Help", new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
Toast.makeText(Chapter1.this, "This is an \"Android Studio\" learning app", Toast.LENGTH_SHORT).show();
}
}).setNegativeButton("No", new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
dialog.dismiss();
}
}).show();
can any one tell me which kind of change in code should i made to that when i press yes i should leave the app instead of going to previous activity.
finish();
also try looking at the response that was provided here How to quit android application programmatically you will get some clue.
#Override
public void onBackPressed() {
new AlertDialog.Builder(Chapter1.this)
.setTitle("Exit")
.setIcon(R.drawable.ic_baseline_warning_24)
.setMessage("Are you sure you want to go back")
.setPositiveButton("yes", new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
finishAffinity();
System.exit(0);
}
}).setNeutralButton("Help", new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
Toast.makeText(Chapter1.this, "This is an \"Android Studio\" learning
app", Toast.LENGTH_SHORT).show();
}
}).setNegativeButton("No", new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
dialog.dismiss();
}
}).show();
I'm trying to open 2 alert dialogs like permissions, the first one pops up and dismiss when I press the positiveButton, then It inflates the second one, but when I press positiveButton on second alertdialog it does not dosmiss and keeps showing it
private void requestXiaomiBootPermission(){
AlertDialog.Builder builder = new AlertDialog.Builder(MainActivity.this);
builder.setTitle("Automatic boot");
builder.setMessage("we need this permission to work");
builder.setCancelable(false);
builder.setPositiveButton("Allow", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int i) {
addAutoStartup();
requestXiaomiBackgroundPermission();
dialog.dismiss();
}
}).show();
builder.setNegativeButton("Deny", new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
dialog.dismiss();
}
}).show();
builder.setNeutralButton("do not show", new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
}
}).show();
builder.show();
}
private void requestXiaomiBackgroundPermission(){
AlertDialog.Builder builder2 = new AlertDialog.Builder(MainActivity.this);
builder2.setTitle("Popup window");
builder2.setMessage("we need this permission to work");
builder2.setCancelable(false);
builder2.setPositiveButton("Allow", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int i) {
addPermissionsOnBackgroud();
dialog.dismiss(); // --> Here it does not dismiss when this dialog inflates
}
}).show();
builder2.setNegativeButton("Deny", new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
dialog.dismiss();
}
}).show();
builder2.setNeutralButton("do not show", new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
}
}).show();
builder2.show();
}
So the circuit is this
I open the app and the first dialog pops up ( requestXiaomiBootPermission() ) , then when I press the positiveButton allow it opens the second dialog ( requestXiaomiBackgroundPermission() ) but when I press allow on this one, dialog.dismiss() does not dismiss my dialog and keeps there, any solution?
Don't invoke show() after every setXXXXXButton() call. Just invoke it once, at the end. You're creating 4 boot permission dialogs, and 4 background permission dialogs, and they're all getting stacked on top of each other.
You are opening both dialog 4 times. Remove show() method after all three button.just keep show() method at end.
private void requestXiaomiBootPermission(){
AlertDialog.Builder builder = new AlertDialog.Builder(context);
builder.setTitle("Automatic boot");
builder.setMessage("we need this permission to work");
builder.setCancelable(false);
builder.setPositiveButton("Allow", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int i) {
dialog.dismiss();
}
});
builder.setNegativeButton("Deny", new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
dialog.dismiss();
}
});
builder.setNeutralButton("do not show", new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
}
});
builder.show();
}
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();
}
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
I created a Button,While i Click that Button a new Dialog Box is Displayed.In that dialog Box i have set icon and text.text is Displayed but icon(image) is not displayed in dialog Box.
Here my Coding
Button btnteam_westernbulldogs=new Button(this);
btnteam_westernbulldogs.setId(team_westernbulldogsid);
btnteam_westernbulldogs.setBackgroundResource(R.drawable.team_westernbulldogs);
public void onClick(View v){
createbtnteam_westernbulldogs();
}
});
public void createbtnteam_westernbulldogs()
{
AlertDialog alertDialog = new AlertDialog.Builder(this).create();
alertDialog.setIcon(R.drawable.team_westernbulldogs);
alertDialog.setMessage("What kind of Banner do you Want to Create?");
alertDialog.setButton("Text", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
createText();
}
alertDialog.setIcon(R.drawable.icon);
alertDialog.show();
} });
Try this..
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setMessage("Are you sure you want to exit?")
.setIcon(R.drawable.icon)
.setCancelable(false)
.setPositiveButton("Yes", new DialogInterface.OnClickListener()
{
public void onClick(DialogInterface dialog, int id)
{
MyActivity.this.finish();
}
})
.setNegativeButton("No", new DialogInterface.OnClickListener()
{
public void onClick(DialogInterface dialog, int id)
{
dialog.cancel();
}
});
AlertDialog alert = builder.create();
public void createbtnteam_westernbulldogs()
{
AlertDialog alertDialog = new AlertDialog.Builder(this).create();
alertDialog.setIcon(R.drawable.team_westernbulldogs);
alertDialog.setTitle("What kind of Banner do you Want to Create?");
alertDialog.setButton("Text", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
createText();
}
alertDialog.setIcon(R.drawable.icon);
alertDialog.show();
} });
You must call setTitle with non-empty string!
Icon is displayed only if there's also title.