is it possible to create listview inside dialog?

this implementation doesn’t require you to make any xml layouts. it was written as a case statement in “onCreateDialog” override, but you can adapt if for your purposes very easily:

AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setTitle("Select Color Mode");

ListView modeList = new ListView(this);
String[] stringArray = new String[] { "Bright Mode", "Normal Mode" };
ArrayAdapter<String> modeAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, android.R.id.text1, stringArray);
modeList.setAdapter(modeAdapter);

builder.setView(modeList);
final Dialog dialog = builder.create();

dialog.show();

because you are making a dialog with only a ListView, you set the onItemClickListener of the ListView, as there isn’t one for the basic dialog class.

modeList.setOnItemClickListener(new AdapterView.OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
            switch(i) {
                case 0:
                    //do something for first selection
                    break;
                case 1:
                    //do something for second selection
                    break;
            }
            dialog.dismiss();
        }
    });

Leave a Comment