Summary of Alert Dialog

Keywords: Android xml encoding

In the development of APP, we often need to set up a pop-up box by clicking on a button. In this pop-up box, we do some selection and screening functions. This is Alert Dialog in Android. Here, let's talk about how to use the AlertDialog class that comes with the system.

The first is the simplest use, as follows:

AlertDialog.Builder builder = new AlertDialog.Builder(context);  
//Create a custom View  
LayoutInflater inflater = LayoutInflater.from(context);  
View view = inflater.inflate(R.layout.xxx, null);  
builder.setView(view)  
//Create an AlertDialog object  
AlertDialog dialog = builder.create();  
dialog.show();  

Or use the second way:

AlertDialog.Builder builder = new AlertDialog.Builder(context);  
builder.setIcon(R.drawable.icon);
builder.setTitle("Android Monkey");  
builder.setMessage("Edit what you want here");  

//Create an AlertDialog object  
AlertDialog dialog = builder.create();  
dialog.show();  

It's easy to see that the second way is that Android uses the Builder Patterns (one of the design patterns) to create a complex object by using the system's own modules without knowing the details.

The Alert Dialog pop-up box actually consists of three parts, which are region 1, region 2 and region 3 from top to bottom. Area 1 is where title s are placed, and Area 2 is the main content. Here we can set up some message information, or define a set of selection boxes, or we can define our own layout pop-up boxes. Area 3 can use Button to define our action buttons. There are three different kinds of Action Buttons for us to choose from.

setPositiveButton(CharSequence text, DialogInterface.OnClickListener listener)
//This is a button equivalent to OK, which determines the operation.

setNegativeButton (CharSequence text, DialogInterface.OnClickListener listener)
//This is the equivalent of a cancel button

setNeutralButton (CharSequence text, DialogInterface.OnClickListener listener)
//This is equivalent to a button that ignores operations.

The AlertBuilder class also has a setItems method that can be called to display a list for radio or multiple selection. Here is an example:

button.setOnClickListener(new OnClickListener(){
    @Override
    public void onClick(View v)
    {
        AlertDialog.Builder builder = new AlertDialog.Builder(MainActivity.this);
        builder.setIcon(R.drawable.ic_launcher);
        builder.setTitle("Please choose gender.");
        final String[] sex = {"male", "female", "Unknown sex"};
        //    Set up an item selection drop-down box
        /**
         * The first parameter specifies the set of drop-down radio boxes that we want to display
         * The second parameter represents the index, specifying which radio box is checked by default, and 1 indicates that the default'female'is checked by default.
         * The third parameter binds a listener to each single option
         */
        builder.setSingleChoiceItems(sex, 1, new DialogInterface.OnClickListener()
        {
            @Override
            public void onClick(DialogInterface dialog, int which)
            {
                Toast.makeText(MainActivity.this, "Gender:" + sex[which], Toast.LENGTH_SHORT).show();
            }
        });
        builder.setPositiveButton("Determine", new DialogInterface.OnClickListener()
        {
            @Override
            public void onClick(DialogInterface dialog, int which)
            {

            }
        });
        builder.setNegativeButton("cancel", new DialogInterface.OnClickListener()
        {
            @Override
            public void onClick(DialogInterface dialog, int which)
            {

            }
        });
        builder.show();
    }
});

In the process of tracing APP development, a feature is set a filter button on toolBar. There are two lists in the pop-up box, each of which is a radio. Here I use the custom layout of Zone 2, and design the layout of the second part as follows:

<!--dialog_selector_layout.xml-->
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="match_parent">
    <LinearLayout
        android:orientation="horizontal"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginLeft="30dp">
        <ImageView
            android:layout_width="20dp"
            android:layout_height="20dp"
            android:src="@drawable/selectsex"/>
        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:textColor="@color/ff_white"
            android:text="Gender:"/>
        <CheckBox
            android:id="@+id/sex_all"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:button="@drawable/check_state"
            />
        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:textColor="@color/ff_white"
            android:text="whole "/>
        <CheckBox
            android:id="@+id/sex_man"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:button="@drawable/check_state"
            />
        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:textColor="@color/ff_white"
            android:text="male "/>
        <CheckBox
            android:id="@+id/sex_woman"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:button="@drawable/check_state"
            />
        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:textColor="@color/ff_white"
            android:text="female"/>
    </LinearLayout>

    <LinearLayout
        android:orientation="horizontal"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginLeft="30dp"
        android:layout_marginTop="20dp">
        <ImageView
            android:layout_width="20dp"
            android:layout_height="20dp"
            android:src="@drawable/selectpeople"/>
        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:textColor="@color/ff_white"
            android:text="Crowd:"/>
        <CheckBox
            android:id="@+id/people_all"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:button="@drawable/check_state"
            />
        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:textColor="@color/ff_white"
            android:text="whole "/>
        <CheckBox
            android:id="@+id/people_photogragher"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:button="@drawable/check_state"
            />
        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:textColor="@color/ff_white"
            android:text="Photographer "/>
        <CheckBox
            android:id="@+id/people_model"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:button="@drawable/check_state"
            />
        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:textColor="@color/ff_white"
            android:text="model"/>
    </LinearLayout>
</LinearLayout>

Then create a Dialog in the following way:

AlertDialog builder = new AlertDialog.Builder(MainActivity.this, R.style.AlertDialog).create();
// Loading an xml layout file as a View object through LayoutInflater
view = LayoutInflater.from(MainActivity.this).inflate(R.layout.dialog_selector_layout, null);
// Set up our own defined layout file as the Content for the pop-up box
builder.setView(view);

Another situation with regard to Alert Dialog is to select whether the following Button should close the pop-up box after clicking according to the user's settings. If the user has not done the corresponding operation according to the prompt information, the pop-up box will not close after clicking the confirmation button, and give the Toast prompt.

I used the following implementation:

AlertDialog.Builder builder = new AlertDialog.Builder(getContext(), R.style.AlertDialog);
builder.setPositiveButton("Determine", null);
// Loading an xml layout file as a View object through LayoutInflater
view = LayoutInflater.from(getContext()).inflate(R.layout.dialog_selector_category, null);

// Set up our own defined layout file as the Content for the pop-up box
builder.setView(view);
final AlertDialog alertDialog = builder.create();
alertDialog.show();

alertDialog.getButton(AlertDialog.BUTTON_POSITIVE).setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View view) {
        //The omitted part of ____________
        if ((!checkbox_people_all.isChecked()) && (!checkbox_people_model.isChecked()) &&
                (!checkbox_people_photogragher.isChecked())) {
            CommonUtils commonUtils = new CommonUtils();
            commonUtils.showToast(getContext(), "Not yet chosen!");
        } else if (checkbox_people_all.isChecked()) {
            userModel.setUcategory("Both are");
            alertDialog.dismiss();
        } else if (checkbox_people_photogragher.isChecked()) {
            userModel.setUcategory("Photographer");
            alertDialog.dismiss();
        } else if (checkbox_people_model.isChecked()) {
            userModel.setUcategory("model");
            alertDialog.dismiss();
        }
    }
    //The omitted part of ____________
});
Button btnPositive = alertDialog.getButton(android.app.AlertDialog.BUTTON_POSITIVE); btnPositive.setTextColor(getResources().getColor(R.color.ff_white));
btnPositive.setTextSize(15);

The main code in the above section is the alertdialog.dismiss() method. Buttons created by using the getButton method must use the dismiss method to close the bomb box, so that you can choose whether to close the bomb box according to the logic.

Posted by richtom80 on Tue, 14 May 2019 14:11:55 -0700