AlertDialog - do not dismiss on item click
android, android-alertdialog
Solution
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
}
});
Problem
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