Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can i close an alert dialog on list click?

I have an Alert Dialog which has a list on it, and i want to close onlistclick is it possible?

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

    final String[] Categories = SQLiteHelper.getAllCategories();//this is where i get the array for my list
    ListView myList = new ListView(this);

    ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, R.layout.alert_dialog_list_view, Categories);
    myList.setAdapter(adapter);
    myList.setOnItemClickListener(new OnItemClickListener() {
        public void onItemClick(AdapterView<?> parent, View view, int position, long rowId) {
                     //doing something in here and then close
         }
    });
    builder.setTitle("Please Choose");
    builder.setInverseBackgroundForced(true);
    builder.setView(myList);
    final Dialog dialog = builder.create();
    dialog.show();
    }

The alert dialog is running perfect i just dont want to put any buttons in it.

like image 541
Stelios M Avatar asked Apr 11 '12 13:04

Stelios M


People also ask

How do I close alerts in dialog?

AlertDialog generally consists of the main title, the message, and two buttons, technically termed as a positive button and a negative button. Both positive and negative buttons can be programmed to perform various actions. By default, the negative button lets close the AlertDialog without any additional lines of code.

How do I close alert dialog in Flutter?

then((exit) { if (exit == null) return; if (exit) { // user pressed Yes button } else { // user pressed No button } }); The below code will close AlertBox/DialogBox in Flutter. Navigator. of(context).

What is the difference between an alert and an alert dialog?

AlertDialog is a lightweight version of a Dialog. This is supposed to deal with INFORMATIVE matters only, That's the reason why complex interactions with the user are limited. Dialog on the other hand is able to do even more complex things .

How do you dismiss dialog with click on outside of dialog?

By default, dialog can be closed by pressing Esc key and clicking the close icon on the right of dialog header. It can also be closed by clicking outside of the dialog using hide method. Set the CloseOnEscape property value to false to prevent closing of the dialog when pressing Esc key.


1 Answers

If you define the onItemClickListener after the Dialog you can just call dialog.dismiss(); in the onItemClick() method.

like image 87
Jave Avatar answered Oct 02 '22 13:10

Jave