119

I've been searching around trying to figure out how to create a popup or a dialog that has 4 options to choose from.

I see this picture on the Android developer site:

enter image description here

Does anyone know how to code up something like the one on the right? I don't need any icons next to my text, I just need to be able to select from 4 options.

enb081
  • 3,831
  • 11
  • 43
  • 66
Cornomaniac
  • 1,193
  • 2
  • 7
  • 4

4 Answers4

308

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. ;))

zbr
  • 6,860
  • 4
  • 31
  • 43
  • 7
    `.create()` is unnecessary here, `.show()` will return the dialog created by the builder and then show it as well – TronicZomB May 05 '13 at 22:19
  • 3
    Thank you very much for this. I really didn't want to implement an xml layout for a simple text menu and could not find an easy way to do this until I stumbled across this post. – Christopher Rathgeb Jan 24 '14 at 01:24
  • How can we middle these text and can we set the color inside this text? e.g red shows the red color?? green text displays in green color? – Ahmad Arslan Feb 04 '15 at 13:01
  • Note : this example not is multiple, read again tittle from post :) –  Jun 23 '15 at 10:54
  • Hello, can you also show how I can set actions for specific item selections? Example: I want to let the user change the app language by clicking one of those items (probably using if statement). – Arda Çebi Feb 07 '18 at 20:11
  • Also, is it possible to add a subtitle under the title of the dialog? – Arda Çebi Feb 07 '18 at 20:29
7

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();
}
Adil Soomro
  • 37,609
  • 9
  • 103
  • 153
Nikhil jassal
  • 149
  • 2
  • 4
6

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

Vishal Pawale
  • 3,416
  • 3
  • 28
  • 32
3

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();
Bart _
  • 41
  • 2