Show list view in a popup window - Android

喜你入骨 提交于 2019-12-03 20:47:36
Paresh Mayani

Yes you can do it.

1st way: define an activity as Dialog with the below attribute in AndroidManifest.xml file:

<activity android:theme="@android:style/Theme.Dialog" />

2nd way: You can inflate the XML layout inside the dialog as below:

Dialog dialog = new Dialog(context);
LayoutInflater li = context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View v = li.inflate(R.layout.my_layout, null, false);
dialog.setContentView(v);
dialog.show();

for example:

edit: link fixed

Android Dialog with ListView.

Show a simple Alert Dialog with a list:

final CharSequence[] items = {"Red", "Green", "Blue"};

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

builder.setTitle("Pick a color");
builder.setItems(items, new DialogInterface.OnClickListener() {

   public void onClick(DialogInterface dialog, int item) {
        Toast.makeText(getApplicationContext(), items[item], Toast.LENGTH_SHORT).show();
   }

});

AlertDialog alert = builder.create();

alert.show();

If using a custom ArrayAdapter, use setAdapter():

AlertDialog.Builder builder = new Builder(this)
    .setTitle("Dialog Title")
    .setAdapter(new CustomAdapter(context, items, ...), (dialog, itemPosition) -> {
      // Handle item click
    });
builder.show();
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!