I have a created a app with List view when i click back button from home page it will toast a message and ask to exit or not. after i have implement a Navigation drawer and set open and close. but when i click back button from navigation go back and popup the toast which i set to hoe page.
#Override
public void onBackPressed() {
if (drawerLayout.isDrawerOpen(GravityCompat.START)){
drawerLayout.closeDrawer(GravityCompat.START);
}else{
super.onBackPressed();
}
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setMessage("Are you sure you want to Exit?")
.setCancelable(false)
.setPositiveButton("Yes", new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
finish();
AdminHome.super.onBackPressed();
}
})
.setNegativeButton("No", new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
dialog.cancel();
}
});
AlertDialog alertDialog = builder.create();
alertDialog.show();
}
how to stop the toast message when i click back from navigation bar and set toast from home page.
hi You can change your code like this
#Override
public void onBackPressed() {
if (drawerLayout.isDrawerOpen(GravityCompat.START)){
drawerLayout.closeDrawer(GravityCompat.START);
}else{
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setMessage("Are you sure you want to Exit?")
.setCancelable(false)
.setPositiveButton("Yes", new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
finish();
AdminHome.super.onBackPressed();
}
})
.setNegativeButton("No", new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
dialog.cancel();
}
});
AlertDialog alertDialog = builder.create();
alertDialog.show();
}
}
Put the AlertDialog code in else block.
Put your alertdialog inside else of your condition
#Override
public void onBackPressed() {
if (drawerLayout.isDrawerOpen(GravityCompat.START)){
drawerLayout.closeDrawer(GravityCompat.START);
}else{
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setMessage("Are you sure you want to Exit?")
.setCancelable(false)
.setPositiveButton("Yes", new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
super.onBackPressed();
}
})
.setNegativeButton("No", new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
dialog.cancel();
}
});
AlertDialog alertDialog = builder.create();
alertDialog.show();
}
}
Related
I want to add one more button that if someone presses the back button two times the alert dialog box should disappear or at least get a close dialog box X button on the top right side of the alert box. I have already used setpossitivebutton and setnegativebutton.
public void onBackPressed()
{
AlertDialog.Builder builder = new AlertDialog.Builder(MainActivity.this);
builder.setTitle(R.string.app_name);
builder.setIcon(R.mipmap.ic_launcher);
builder.setMessage("Wanna Exit?")
.setCancelable(false)
.setPositiveButton("Exit", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
finish();
}
})
.setNegativeButton("Share on Whatsapp", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
Intent sendIntent = new Intent(); sendIntent.setAction(Intent.ACTION_SEND);
sendIntent.putExtra(Intent.EXTRA_TEXT,"simple text");
sendIntent.setType("text/plain");
sendIntent.setPackage("com.whatsapp");
startActivity(sendIntent);
}
});
AlertDialog alertDialog = builder.create();
alertDialog.show();
}
Try this code :
AlertDialog alertDialog;
public void onBackPressed() {
if(alertDialog == null || !alertDialog.isShowing()){
alertDialog = = new AlertDialog.Builder(context)
.setTitle("Delete entry")
.setMessage("Are you sure you want to delete this
entry?")
.setPositiveButton(android.R.string.yes, new
DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
// continue with delete
}
})
.setNegativeButton(android.R.string.no, new
DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
// do nothing
}
})
.setIcon(android.R.drawable.ic_dialog_alert)
.show();
}else{
alertDialog.cancel();
}
}
Hope it will help you :)
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 am translating a platform from C# to android.
And I am stuck on how to wait for the Dialog response,
to determine which action will be done. (It will always bypass the decision.)
I have tried to use handler, but it doesn't work.
Any help is appreciated.
Here are my code, which alertDialog wont wait for Dialog box Response finish.
public boolean CheckWriteCriteria()
{
new AlertDialog.Builder(Labelling.this)
.setTitle("Test 1")
.setMessage("Question_1")
.setPositiveButton("Yes", new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
Toast.makeText(this, "YES button click ", Toast.LENGTH_SHORT).show();
}
})
.setNegativeButton("No", new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
Toast.makeText(this, "No button click ", Toast.LENGTH_SHORT).show();
}
}).show();
new AlertDialog.Builder(Labelling.this)
.setMessage("Question_2")
.setPositiveButton("Yes", new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which)
{
Toast.makeText(this, "YES button click ", Toast.LENGTH_SHORT).show();
}
})
.setNegativeButton("No", new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
Toast.makeText(this, "No button click ", Toast.LENGTH_SHORT).show();
}
}).show();
return true;
}
public void Testing()
{
if(CheckWriteCriteria())
{
Toast.makeText(this, "Testing is before AlertDialog.", Toast.LENGTH_SHORT).show();
}
}
Android support AlertDialog. Let see
AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(
MainActivity.this);
// set title
alertDialogBuilder.setTitle("AlertDialog Title");
// set dialog message
alertDialogBuilder
.setMessage("Some Alert Dialog message.")
.setCancelable(false)
.setPositiveButton("OK",new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog,int id) {
Toast.makeText(this, "OK button click ", Toast.LENGTH_SHORT).show();
}
})
.setNegativeButton("CANCEL",new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
Toast.makeText(this, "CANCEL button click ", Toast.LENGTH_SHORT).show();
dialog.cancel();
}
});
// create alert dialog
AlertDialog alertDialog = alertDialogBuilder.create();
// show it
alertDialog.show();
only Ok and Cancel action here, if you want more, just define custom dialog.
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.