How to do dialog with listview inside?

Asked

Viewed 526 times

0

I would like to know how to create a Dialog containing a ListView following the example of the image below.

dialog_listview

2 answers

1


A simple way to do this is to use the Dialog setItems method, from it you arrow your list, and it is loaded automatically in the Dialog.

Example:

final CharSequence[] cores = {"Azul", "Preto", "Verde"};
    AlertDialog.Builder builder = new AlertDialog.Builder(CONTEXT);
    builder.setTitle("Selecione uma Cor");
    builder.setItems(cores, new DialogInterface.OnClickListener() {
        public void onClick(DialogInterface dialog, int selecionado) {
            Toast.makeText(getActivity(), "Cor Selecionada: " + cores[selecionado],
                    Toast.LENGTH_SHORT).show();
        }
    });
    builder.create().show();

Note: The context would be an instance of your Activity, if you are running inside an Activity, you can use this;

0

Browser other questions tagged

You are not signed in. Login or sign up in order to post.