Example Dialog box in Android


Dialog is the good way to providing floating menu in Android which users can do several actions on there. It’s different with Toast which is have purpose for showing information only. At this example, we can try which showing selection menu in Dialog box.

For instance, We will make Users can click Button and Dialog will be pop-up. Suppose we have activity like this:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
public class MainActivity extends Activity {
    private static final int DIALOG_ALERT = 10;
    private static final String ANDROID_TAG = "ANDROID";
   
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        Button workout = (Button) findViewById(R.id.top_done_button);
        workout.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                showDialog(DIALOG_ALERT);
            }
        });
    }

showDialog() is built-in method which will showing Dialog based on the unique ID. Later, we can give related Dialog based on ID using switch(). But we should know that showDialog will be expecting for onCreateDialog() method to process all showDialog() request.

So this is example of onCreateDialog() :

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
@Override
protected Dialog onCreateDialog(int id) {
    String[] options = {"RED", "YELLOW", "GREEN"};

    switch(id) {
    case DIALOG_ALERT:
        AlertDialog.Builder builder = new AlertDialog.Builder(this);
        builder.setTitle("Choose Workout Menu");
        builder.setCancelable(false);
        builder.setItems(options, new DialogInterface.OnClickListener() {
           
            @Override
            public void onClick(DialogInterface dialog, int which) {
                // TODO Auto-generated method stub
                Log.d(ANDROID_TAG, Integer.toString(which));
            }
        });
       
        builder.setPositiveButton("Yes", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                // TODO Auto-generated method stub
//                  MainActivity.this.finish();
            }
        });
       
        builder.setNegativeButton("No", new DialogInterface.OnClickListener() {
           
            @Override
            public void onClick(DialogInterface dialog, int which) {
                // TODO Auto-generated method stub
               
            }
        });
       
        AlertDialog alertDialog = builder.create();
        alertDialog.show();
    }
    return super.onCreateDialog(id);
}

The explanation here is :

1. Build array items that will be a menu list

1
String[] options = {"RED", "YELLOW", "GREEN"};

2. Switch() which will doing the logic based on the given ID

3. Create a Dialog builder

1
AlertDialog.Builder builder = new AlertDialog.Builder(this);

4. setTitle and setItems to define Title of Dialog and Items that will be show-up

5. builder.setPositiveButton and builder.setNegativeButton to handle “yes” and “no” button on Dialog.

And the results will be like this:


Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.