How to dismiss AlertDialog.Builder? How to dismiss AlertDialog.Builder? android android

How to dismiss AlertDialog.Builder?


What didn't work about dismiss()?

You should be able to use either Dialog.dismiss(), or Dialog.cancel()

alertDialog.setNeutralButton("Cancel", new DialogInterface.OnClickListener() { // define the 'Cancel' button    public void onClick(DialogInterface dialog, int which) {        //Either of the following two lines should work.        dialog.cancel();        //dialog.dismiss();    } });


With the following code you can show a ListBox AlertDialog and when you press on a item you dismiss the Dialog. The order of code is importan.

AlertDialog.Builder alertDialog = new AlertDialog.Builder(mContext);String names[] ={"A","B","C","D"};ArrayAdapter<String> adapter = new ArrayAdapter<String>(mContext,android.R.layout.simple_list_item_1,names);LayoutInflater inflater = getLayoutInflater();View convertView = (View)inflater.inflate(R.layout.list_layout, null);ListView lv = (ListView) convertView.findViewById(R.id.listView1);lv.setAdapter(adapter);alertDialog.setView(convertView);alertDialog.setTitle("List");final AlertDialog ad = alertDialog.show();lv.setOnItemClickListener(new AdapterView.OnItemClickListener(){    @Override    public void onItemClick(AdapterView<?> parent, View view, int position, long id)    {        //Toast.makeText(mContext, position, Toast.LENGTH_LONG).show();        ad.dismiss();    }});


The code is very simple:

final AlertDialog show = alertDialog.show();

finally in the action of button for example:

show.dismiss();

For example with a custom alertdialog:

enter image description here

Code on java, you could create a Object AlertDialog:

public class ViewAlertRating {    Context context;    public ViewAlertRating(Context context) {        this.context = context;    }    public void showAlert(){        AlertDialog.Builder alertDialog = new AlertDialog.Builder(context);        LayoutInflater inflater = ((Activity) context).getLayoutInflater();        View alertView = inflater.inflate(R.layout.layout_test, null);        alertDialog.setView(alertView);        final AlertDialog show = alertDialog.show();        Button alertButton = (Button) alertView.findViewById(R.id.btn_test);        alertButton.setOnClickListener(new View.OnClickListener() {            @Override            public void onClick(View v) {                show.dismiss();            }        });    }}

Code XML layout_test.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"    android:orientation="vertical"    android:layout_width="match_parent"    android:layout_height="wrap_content">    <TextView        android:layout_width="match_parent"        android:layout_height="wrap_content"        android:text="ValoraciĆ³n"        android:id="@+id/text_test1"        android:textSize="20sp"        android:textColor="#ffffffff"        android:layout_centerHorizontal="true"        android:gravity="center_horizontal"        android:textStyle="bold"        android:paddingTop="10dp"        android:paddingBottom="10dp"        android:background="#ff37dabb"        android:paddingLeft="20dp"        android:paddingRight="20dp" />    <LinearLayout        android:orientation="vertical"        android:layout_width="match_parent"        android:layout_height="wrap_content"        android:paddingLeft="20dp"        android:paddingRight="20dp"        android:layout_marginTop="15dp">        <EditText            android:layout_width="match_parent"            android:layout_height="120dp"            android:id="@+id/edit_test"            android:hint="DescripciĆ³n"            android:textColor="#aa000000"            android:paddingLeft="10dp"            android:paddingRight="10dp"            android:textColorHint="#aa72777a"            android:gravity="top" />    </LinearLayout>    <LinearLayout        android:orientation="horizontal"        android:layout_width="match_parent"        android:layout_height="match_parent"        android:gravity="center_horizontal"        android:paddingTop="10dp"        android:paddingLeft="15dp"        android:paddingRight="15dp"        android:paddingBottom="15dp" >        <LinearLayout            android:orientation="horizontal"            android:layout_width="match_parent"            android:layout_height="match_parent" >            <LinearLayout                android:orientation="horizontal"                android:layout_width="match_parent"                android:layout_height="match_parent"                android:weightSum="1.00"                android:gravity="right" >                <Button                    android:layout_width="match_parent"                    android:layout_height="wrap_content"                    android:text="Enviar"                    android:id="@+id/btn_test"                    android:gravity="center_vertical|center_horizontal"                    android:textColor="#ffffffff"                    android:background="@drawable/btn_flat_blue_selector" />            </LinearLayout>        </LinearLayout>    </LinearLayout></LinearLayout>

finally, call on Activity:

ViewAlertRating alertRating = new ViewAlertRating(this);alertRating.showAlert();