16

OK so I am creating an ArrayAdapter and using it in my Alert Dialog because I don't want to show the default radio buttons on SingleItemSelection dialog.

Instead I want to change the background of the item that is selected, and then when the user presses the positive button I will perform the action related to the item that has been selected.

    private void showAlertDialog()
    {
        final String[] options = getResources().getStringArray(R.array.dialog_options);
        ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, options);
        
        AlertDialog.Builder dialogBuilder = new AlertDialog.Builder(this);
        dialogBuilder.setTitle("My Dialog");
        
        dialogBuilder.setAdapter(adapter, new OnClickListener()
        {
            @Override
            public void onClick(DialogInterface dialog, int which)
            {
                Toast.makeText(getApplicationContext(), "item clicked at index " + which, Toast.LENGTH_LONG).show();
// Here I need to change the background color of the item selected and prevent the dialog from being dismissed
            }
        });
    
        //String strOkay = getString(R.string.okay);
        dialogBuilder.setPositiveButton("OK", null); // TODO
        dialogBuilder.setNegativeButton("Cancel", null); // nothing simply dismiss
    
        AlertDialog dialog = dialogBuilder.create();
        dialog.show();
    }

There are two problems I'm trying to tackle.

How do I prevent the dialog from being dismissed when the user clicks on an item

How do I change the background of the item that has been selected when the user clicks on it

4 Answers 4

24

To prevent dialog from dismissing on item click you can use AdapterView.OnItemClickListener instead of DialogInterface.OnClickListener.

Like this:

dialogBuilder.setAdapter(adapter, null);
...
AlertDialog dialog = dialogBuilder.create();
alertDialog.getListView().setOnItemClickListener(
new AdapterView.OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
// do your stuff here
}
});
1
  • I'm trying to make an AlertDialog with an item list and a text input. This answer allowed me to keep the dialog open after the user touched an item.
    – Jahaziel
    Jul 6, 2016 at 6:10
11
  1. You can set custom ListView as content of AlertDialog and set OnItemClickListener

    AlertDialog.Builder builder = new AlertDialog.Builder(this);
    String[] items = ...;
    ListView list = new ListView(this);
    list.setAdapter(new ArrayAdapter<String>(this, android.R.layout.select_dialog_item, items));
    list.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            public void onItemClick(AdapterView<?> arg0, View view, int pos, long id) {
                ...
            }
        });
    builder.setView(list);
    

    and then save reference to dialog

    mDialog = builder.show();
    

    in order to dismiss it if necessary

    mDialog.dismiss();
    
1
  • This doesn't work on Android 4.0.1. The listview is disabled. When you leave the app and return it will be enabled again. I think it's a bug.
    – Roel
    Jul 1, 2015 at 8:33
1

How do I prevent the dialog from being dismissed when the user clicks on an item

How do I change the background of the item that has been selected when the user clicks on it

Here is example

public class MainActivity extends AppCompatActivity {
private static final String listFragmentTag = "listFragmentTag";
private static final String data[] = {"one", "two", "three", "four"};

public MainActivity() {
    super();
}

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
}

public void btnClick(View v) {
    ListFragment lf = new ListFragment();
    lf.show(getSupportFragmentManager(), listFragmentTag);
}

public static class ListFragment extends DialogFragment {
    @Override @NonNull
    public Dialog onCreateDialog(Bundle savedInstanceState) {
        AlertDialog.Builder adb = new AlertDialog.Builder(getActivity());
        adb.setIcon(android.R.drawable.ic_dialog_info)
                .setTitle("List")
                .setItems(data, null)
                .setPositiveButton("OK", null); // pass your onClickListener instead of null

        // to keep dialog open after click on item
        AlertDialog ad = adb.create();
        ad.getListView().setOnItemClickListener(new AdapterView.OnItemClickListener() {
            private int colorOrg = 0x00000000;
            private int colorSelected = 0xFF00FF00;
            private View previousView;
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                // restoring color of previous view
                if(previousView != null) {
                    previousView.setBackgroundColor(colorOrg);
                }
                // changing items's BG color
                view.setBackgroundColor(colorSelected);
                previousView = view;
            }
        });

        return ad;
    }

    @Override
    public void onDismiss(DialogInterface dialog) {
        super.onDismiss(dialog);
    }

}
}
-3
  1. You can use setCanceledOnTouchOutside(false) or setCanceleable(false).
  2. Set selector for the root element tag of the dialog layout xml.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.