Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android: create a popup that has multiple selection options

People also ask

How do I use pop up menu on Android?

Go to app > res > menu > right-click > New > Menu Resource File and create a menu resource file and name it as popup_menu. In the popup_menu file, we will add menu items. Below is the code snippet for the popup_menu. xml file.

What is AlertDialog builder in Android?

Alert Dialog shows the Alert message and gives the answer in the form of yes or no. Alert Dialog displays the message to warn you and then according to your response the next step is processed. Android Alert Dialog is built with the use of three fields: Title, Message area, Action Button.

How many action button can a dialog have in Android Studio?

There should be no more than three action buttons in a dialog.


You can create a String array with the options you want to show there and then pass the array to an AlertDialog.Builder with the method setItems(CharSequence[], DialogInterface.OnClickListener).

An example:

String[] colors = {"red", "green", "blue", "black"};

AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setTitle("Pick a color");
builder.setItems(colors, new DialogInterface.OnClickListener() {
    @Override
    public void onClick(DialogInterface dialog, int which) {
        // the user clicked on colors[which]
    }
});
builder.show();

The output (on Android 4.0.3):

Output

(Background map not included. ;))


Try this :

public void onClick(View v) {

    final String[] fonts = {
        "Small", "Medium", "Large", "Huge"
    };

    AlertDialog.Builder builder = new AlertDialog.Builder(TopicDetails.this);
    builder.setTitle("Select a text size");
    builder.setItems(fonts, new DialogInterface.OnClickListener() {@
        Override
        public void onClick(DialogInterface dialog, int which) {
            if ("Small".equals(fonts[which])) {
                Toast.makeText(TopicDetails.this, "you nailed it", Toast.LENGTH_SHORT).show();
            } else if ("Medium".equals(fonts[which])) {
                Toast.makeText(TopicDetails.this, "you cracked it", Toast.LENGTH_SHORT).show();
            } else if ("Large".equals(fonts[which])) {
                Toast.makeText(TopicDetails.this, "you hacked it", Toast.LENGTH_SHORT).show();
            } else if ("Huge".equals(fonts[which])) {
                Toast.makeText(TopicDetails.this, "you digged it", Toast.LENGTH_SHORT).show();
            }
            // the user clicked on colors[which]

        }
    });
    builder.show();
}

The pop ups are nothing but AlertDialog.So you just need to create AlertDialog, then inflate your desired view using LayoutInflater and set the inflated view using setView() method of AlertDialog


ALTERNATIVE OPTION

This is my first post so I'm excited to share my code! This worked for me:

Place these two lines above the OnCreate event

final String[] Options = {"Red", "Blue"};
AlertDialog.Builder window;

Place this code on the event that will trigger this

window = new AlertDialog.Builder(this);
window.setTitle("Pick a color");
window.setItems(Options, new DialogInterface.OnClickListener() {
    @Override
    public void onClick(DialogInterface dialog, int which) {
        if(which == 0){
           //first option clicked, do this...

        }else if(which == 1){
           //second option clicked, do this...

        }else{
        //theres an error in what was selected
            Toast.makeText(getApplicationContext(), "Hmmm I messed up. I detected that you clicked on : " + which + "?", Toast.LENGTH_LONG).show();
        }
    }
});

window.show();