AlertDialog use

AlertDialog use

Article Directory

What do we need to do to create a dialog

The dialog box can be divided into three parts: the
upper area is the title bar and the icon, the
middle area is the content area, and the
lower area is the button area

Other dialog boxes of various forms are also based on this variant.

1. 1. you need to create an AlertDialog.Builder object, the basic syntax:

AlertDialog.Builder alertDialogBuilder=new AlertDialog.Builder(this); Copy code

2. A dialog box can be constructed by calling its create() method

AlertDialog alertDialog = alertDialogBuilder.create(); alertDialog.show();//Display the dialog Copy code

3. Realize by calling the setXX method of the alertDialogBuilder object

alertDialogBuilder.setTitle();//Set the title alertDialogBuilder.setIcon();//Set chart /*Set the button below*/ alertDialogBuilder.setPositiveButton(); alertDialogBuilder.setNegativeButton(); alertDialogBuilder.setNeutralButton(); /*The setting of the content area of the dialog provides a variety of methods*/ setMessage();//Set the display text setItems();//Set the content of the dialog box to a simple list item setSingleChoiceItems();//Set the content of the dialog box as a single-selection list item setMultiChoiceItems();//Set the content of the dialog box to multiple selection list items setAdapter();//Set the content of the dialog box to a custom list item setView();//Set the content of the dialog box to a custom View //Set whether the dialog box can be cancelled setCancelable(booleab cancelable); setCancelListener(onCancelListener); Copy code

Simple AlertDialog

Key code

private void showNormalDialog () { AlertDialog.Builder builder = new AlertDialog.Builder( this ); builder.setIcon(R.mipmap.ic_launcher); builder.setTitle(R.string.simpleDialogTitle); builder.setMessage(R.string.simpleDialogMessage); //button listener builder.setPositiveButton(R.string.confirmButton, new DialogInterface.OnClickListener() { @Override public void onClick (DialogInterface dialogInterface, int i) { Toast.makeText(getApplicationContext(), "You clicked the OK button" ,Toast.LENGTH_SHORT).show(); } }); builder.setNegativeButton(R.string.cancelButton, new DialogInterface.OnClickListener() { @Override public void onClick (DialogInterface dialogInterface, int i) { Toast.makeText(getApplicationContext(), "You clicked the cancel button" ,Toast.LENGTH_SHORT).show(); } }); //The setting dialog is cancelable builder.setCancelable( true ); AlertDialog alertDialog = builder.create(); alertDialog.show(); } Copy code

Simple list Dialog

Key code

private void showNormalItemDialog () { AlertDialog.Builder builder = new AlertDialog.Builder( this ); builder.setIcon(R.mipmap.ic_launcher); builder.setTitle(R.string.simpleDialogTitle); final String[] items = { "Item one" , "Item two" , "Item three" }; builder.setItems(items, new DialogInterface.OnClickListener() { @Override public void onClick (DialogInterface dialogInterface, int i) { Toast.makeText(getApplicationContext(), "You clicked " +items[i],Toast.LENGTH_SHORT).show(); } }); //The setting dialog is cancelable builder.setCancelable( true ); AlertDialog alertDialog = builder.create(); alertDialog.show(); } Copy code

Single-selection list Dialog

Key code

private void showSingleChoiceItemDialog () { AlertDialog.Builder builder = new AlertDialog.Builder( this ); builder.setIcon(R.mipmap.ic_launcher); builder.setTitle(R.string.singleChoiceItemTitle); final String[] items = { "Item one" , "Item two" , "Item three" }; builder.setSingleChoiceItems(items, 1 , new DialogInterface.OnClickListener() { @Override public void onClick (DialogInterface dialogInterface, int i) { Toast.makeText(getApplicationContext(), "You clicked " +items[i], Toast.LENGTH_SHORT).show(); } }); builder.setCancelable( true ); AlertDialog alertDialog = builder.create(); alertDialog.show(); } Copy code

Multiple selection list Dialog

Key code

private void showMultiChoiceItemDialog () { AlertDialog.Builder builder = new AlertDialog.Builder( this ); builder.setIcon(R.mipmap.ic_launcher); builder.setTitle(R.string.multiChoiceItemTitle); final String[] items = { "Item one" , "Item two" , "Item three" }; builder.setMultiChoiceItems(items, new boolean []{ true , true , false }, new DialogInterface.OnMultiChoiceClickListener() { @Override public void onClick (DialogInterface dialogInterface, int i, boolean b) { Toast.makeText(getApplicationContext(), "You clicked " +items[i]+ " " +b,Toast.LENGTH_SHORT).show(); } }); builder.setCancelable( true ); AlertDialog alertDialog = builder.create(); alertDialog.show(); } Copy code

Custom Layout Dialog

Customize a layout first

dialog_forward_type.xml

<?xml version="1.0" encoding="utf-8"?> < LinearLayout xmlns:android = "http://schemas.android.com/apk/res/android" android:layout_width = "match_parent" android:layout_height = "wrap_content" android:orientation = "vertical" android:gravity = "center" > < TextView android:id = "@+id/dialog_forward_type_title" android:layout_width = "wrap_content" android:layout_height = "wrap_content" android:layout_margin = "10dp" android:text = "Please select the forwarding type"/> < View android:layout_width = "match_parent" android:layout_height = "1dp" android:background = "#dddddd"/> < LinearLayout android:layout_width = "match_parent" android:layout_height = "wrap_content" android:padding = "20dp" > < LinearLayout android:id = "@+id/dialog_forward_video_layout" android:layout_width = "0dp" android:layout_height = "wrap_content" android:orientation = "vertical" android:gravity = "center" android:layout_weight = "1" > < ImageView android:layout_width = "50dp" android:layout_height = "50dp" android:src = "@mipmap/btn_forward_video" /> < TextView android:layout_width = "wrap_content" android:layout_height = "wrap_content" android:text = "Video" android:layout_marginTop = "10dp"/> </LinearLayout > < LinearLayout android:id = "@+id/dialog_forward_image_layout" android:layout_width = "0dp" android:layout_height = "wrap_content" android:orientation = "vertical" android:gravity = "center" android:layout_weight = "1" > < ImageView android:layout_width = "50dp" android:layout_height = "50dp" android:src = "@mipmap/btn_forward_images" /> < TextView android:layout_width = "wrap_content" android:layout_height = "wrap_content" android:text = "Picture" android:layout_marginTop = "10dp"/> </LinearLayout > </LinearLayout > </LinearLayout > copy code

Key code

private void showCustomDialog () { AlertDialog.Builder builder = new AlertDialog.Builder( this ); LinearLayout layout = (LinearLayout)getLayoutInflater().inflate(R.layout.dialog_forward_type, null ); builder.setView(layout); LinearLayout videoLayout = (LinearLayout)layout.findViewById(R.id.dialog_forward_video_layout); LinearLayout imageLayout = (LinearLayout)layout.findViewById(R.id.dialog_forward_image_layout); videoLayout.setOnClickListener( new View.OnClickListener() { @Override public void onClick (View v) { Toast.makeText(getApplicationContext(), "U clicked Video type" ,Toast.LENGTH_SHORT).show(); } }); imageLayout.setOnClickListener( new View.OnClickListener() { @Override public void onClick (View v) { Toast.makeText(getApplicationContext(), "U clicked Image type" ,Toast.LENGTH_SHORT).show(); } }); builder.setCancelable( true ); AlertDialog alertDialog = builder.create(); alertDialog.show(); } Copy code

Possible problems

Setting the width and height, the background does not work

The dialog root layout has set a width and height background, but it does not work. You can add a layer of RelativeLayout to the root layout, as follows:

< RelativeLayout xmlns:android = "http://schemas.android.com/apk/res/android" android:layout_width = "match_parent" android:layout_height = "match_parent" android:background = "#A5000000" android:gravity = " center" > //Write the original Dialog layout in the middle //Root layout RelativeLayout background is necessary, otherwise it won t work, I don t know why </RelativeLayout > copy code

So the width and height and the background will all work

If you just want to limit the width and height, you can skip the layout, directly after dialog.show(), add the following code to set the width and height

Window window = alertDialog.getWindow(); WindowManager.LayoutParams lp = window.getAttributes(); lp.gravity = Gravity.CENTER; lp.width = ( int ) AppUtils.convertDpToPixel( 320 ); //dp to px method, you can write it yourself lp.height = WindowManager.LayoutParams.WRAP_CONTENT; alertDialog.getWindow().setAttributes(lp); Copy code

Demo source download

Click here to download the source code