How to display a Yes/No dialog box on Android?
AlertDialog.Builder really isn't that hard to use. It's a bit intimidating at first for sure, but once you've used it a bit it's both simple and powerful. I know you've said you know how to use it, but here's just a simple example anyway:
DialogInterface.OnClickListener dialogClickListener = new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
switch (which){
case DialogInterface.BUTTON_POSITIVE:
//Yes button clicked
break;
case DialogInterface.BUTTON_NEGATIVE:
//No button clicked
break;
}
}
};
AlertDialog.Builder builder = new AlertDialog.Builder(context);
builder.setMessage("Are you sure?").setPositiveButton("Yes", dialogClickListener)
.setNegativeButton("No", dialogClickListener).show();
You can also reuse that DialogInterface.OnClickListener
if you have other yes/no boxes that should do the same thing.
If you're creating the Dialog from within a View.OnClickListener
, you can use view.getContext()
to get the Context. Alternatively you can use yourFragmentName.getActivity()
.
Yes/NO Alert Dialog box in Android
Yes, don't invoke that as per previous user's response. super.onBackPressed();
will onStop
method of the Activity. Instead of onBackPressed();
you can use onKeyDown
for your requirement. If you need to open an AlertDialog
when you press the back button you can simply try that with KeyEvent
For example -
@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
// TODO Auto-generated method stub
switch(keyCode)
{
case KeyEvent.KEYCODE_BACK:
AlertDialog.Builder ab = new AlertDialog.Builder(AlertDialogExampleActivity.this);
ab.setMessage("Are you sure?").setPositiveButton("Yes", dialogClickListener)
.setNegativeButton("No", dialogClickListener).show();
break;
}
return super.onKeyDown(keyCode, event);
}
DialogInterface.OnClickListener dialogClickListener = new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
switch (which){
case DialogInterface.BUTTON_POSITIVE:
//Yes button clicked
break;
case DialogInterface.BUTTON_NEGATIVE:
//No button clicked
break;
}
}
};
When you're overriding onKeyDown
method it will detect back key with your KEYCODE_BACK
Hope this helps you.
Yes and No option on Alert Dialog is Missing in Android
Its not missing please check your theme color in color.xml may be your colorAccent is white change it to other colour
How to implement a confirmation (yes/no) DialogPreference?
That is a simple alert dialog, Federico gave you a site where you can look things up.
Here is a short example of how an alert dialog can be built.
new AlertDialog.Builder(this)
.setTitle("Title")
.setMessage("Do you really want to whatever?")
.setIcon(android.R.drawable.ic_dialog_alert)
.setPositiveButton(android.R.string.yes, new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int whichButton) {
Toast.makeText(MainActivity.this, "Yaay", Toast.LENGTH_SHORT).show();
}})
.setNegativeButton(android.R.string.no, null).show();
Simplest yes/no dialog fragment
A DialogFragment is really just a fragment that wraps a dialog. You can put any kind of dialog in there by creating and returning the dialog in the onCreateDialog() method of the DialogFragment.
Heres an example DialogFragment:
class MyDialogFragment extends DialogFragment{
Context mContext;
public MyDialogFragment() {
mContext = getActivity();
}
@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(mContext);
alertDialogBuilder.setTitle("Really?");
alertDialogBuilder.setMessage("Are you sure?");
//null should be your on click listener
alertDialogBuilder.setPositiveButton("OK", null);
alertDialogBuilder.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
dialog.dismiss();
}
});
return alertDialogBuilder.create();
}
}
To create the dialog call:
new MyDialogFragment().show(getFragmentManager(), "MyDialog");
And to dismiss the dialog from somewhere:
((MyDialogFragment)getFragmentManager().findFragmentByTag("MyDialog")).getDialog().dismiss();
All of that code will work perfectly with the support library, by just changing the imports to use the support library classes.
How to create Yes/NO Alert Dialog in Fragment in Android
Try this method:
private void createAndShowAlertDialog() {
AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
builder.setTitle("My Title");
builder.setPositiveButton(android.R.string.yes, new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
//TODO
dialog.dismiss();
}
});
builder.setNegativeButton(android.R.string.cancel, new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
//TODO
dialog.dismiss();
}
});
AlertDialog dialog = builder.create();
dialog.show();
}
Yes' button is not shown in confirmation box in Android app?
I think you should follow this Builder pattern example given below.
AlertDialog.Builder builder = new AlertDialog.Builder(context)
.setCancelable(true)
.setTitle(titleResourceId)
.setMessage(messageResourceId)
.setPositiveButton("Yes", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
Toast.makeToast(mContext, "Yes", Toast.LENGTH_SHORT).show();
dialog.dismiss();
}
}).setNegativeButton("No", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
Toast.makeToast(mContext, "No", Toast.LENGTH_SHORT).show();
}
});
builder.show();
Using setPositiveButton()
and setNegativeButton()
allows android to place the buttons in the correct order according to the platform the app runs in.
Related Topics
Will Google Android Ever Support .Net
Can't Include C++ Headers Like Vector in Android Ndk
Error: Resource Android:Attr/Fontvariationsettings Not Found
How to Resolve "Waiting for Debugger" Message
Flutter Projects & Android X Migration Issues
Problems with Gridview Inside Scrollview in Android
Progressbar Togther with Asynctask
Load Dimension Value from Res/Values/Dimension.Xml from Source Code
Can't Accept License Agreement Android Sdk Platform 24
How to Start an Intent by Passing Some Parameters to It
How to Have a Transparent Imagebutton: Android
How to Add a Fast-Scroller to the Recyclerview
Getapplicationcontext(), Getbasecontext(), Getapplication(), Getparent()
Android - R Cannot Be Resolved to a Variable
Detect Touch Event on a View When Dragged Over from Other View
Ongloballayoutlistener: Deprecation and Compatibility