Search code examples
androidandroid-alertdialog

No Items shown in setSingleChoiceItems function of AlertDialog?


I want to create an AlertDialog which will offer 3 option to user. To accomplish this i use AlertDialog.Builder.setSingleChoiceItems function. However when i start the program it always shows blank page just with Icon, Message, Buttons but not options.

Here is my code;

        final CharSequence[] options = {"item1", "item2", "item3"};
AlertDialog.Builder builder2 = new AlertDialog.Builder(this);
builder2.setTitle("Title")
        .setMessage("Message")
        .setIcon(R.mipmap.icon)
        .setSingleChoiceItems(options, 0, new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                Log.d("Selection", "Something selected");
            }
        })
        .setPositiveButton("Accept", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                Toast.makeText(MainActivity.this, "Toast1", Toast.LENGTH_SHORT).show();
            }
        })
        .setNegativeButton("Decline", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                Toast.makeText(MainActivity.this, "Toast2", Toast.LENGTH_SHORT).show();
            }
        }).create();
Button btnDialog2 = (Button) findViewById(R.id.btnDialog2);
btnDialog2.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View v) {
        builder2.show();
    }
});

Solution

  • Remove

    .setMessage("Message")
    

    and it should work You cannot show both message and list in same content area. use

    .setTitle("Put Your Title Here")
    

    to set the title.