Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Simplest yes/no dialog fragment

I'd like to make a dialog fragment that asks "Are you sure?" with a "yes/no" reply.

I've looked at the documentation and it's really verbose, going all over the place, explaining how to make advanced dialog boxes, but no intact code on making a simple 'hello world' kind of dialog box. Most tutorials utilize the deprecated dialog box system. The official blog seems to be unnecessarily complicated and difficult to understand.

So, what's the simplest way to create and display a really basic Alert Dialog? Bonus points if it's using the support library.

like image 886
Muz Avatar asked Oct 16 '12 10:10

Muz


People also ask

What are dialog fragments?

Android DialogFragments. DialogFragment is a utility class which extends the Fragment class. It is a part of the v4 support library and is used to display an overlay modal window within an activity that floats on top of the rest of the content. Essentially a DialogFragment displays a Dialog but inside a Fragment.

What is the difference between dialog and dialog fragment?

Dialog: A dialog is a small window that prompts the user to make a decision or enter additional information. DialogFragment: A DialogFragment is a special fragment subclass that is designed for creating and hosting dialogs.

Is dialog fragment deprecated?

This method is deprecated. Called to do initial creation of a fragment. Override to build your own custom Dialog container.

How do I get a yes no dialog box on Android?

setMessage( "Select yes to display toast message and no to dismiss the dialog ?" ) // button and setting text to it. // and setting text to it. // show to display our dialog.


1 Answers

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.

like image 96
athor Avatar answered Sep 20 '22 03:09

athor