AlertDialog dialog box to prompt some important information or to display some content requires additional user interaction. It is usually in the form of a small window on the display screen. AlertDialog created using the dialog box typically contains the title, content and button three regions.
Creating AlertDialog dialog steps:
- Call AlertDialog static inner classes Builder to create objects of AlertDialog.Builder.
- Call setTitle AlertDialog.Builder of () and setIcon () method set the title name and icon are AlertDialog dialog.
- Call AlertDialog.Builder of setMessage (), setSingleChoiceItems () or setMultiChoiceItems () method to set AlertDialog dialog content is simple text, a list of radio or multi-select list.
- Call AlertDialog.Builder of setPositiveButton () and setNegativeButton () method to determine the set AlertDialog dialog and Cancel buttons.
- The call to create AlertDialog.Builder () method creates AlertDialog object.
- Call AlertDialog object show () method to display the dialog box.
- Call AlertDialog object dismiss () method to cancel the dialog box.
Case I: Common Dialog
package com.example.commondialog;
import androidx.appcompat.app.AlertDialog;
import androidx.appcompat.app.AppCompatActivity;
import android.content.DialogInterface;
import android.os.Bundle;
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate (savedInstanceState);
setContentView(R.layout.activity_main);
}
@Override
public void onBackPressed() {
Alert Dialog dialogue;
AlertDialog.Builder builder = new AlertDialog.Builder(this)
.setIcon(R.mipmap.ic_launcher)
.setTitle ( "common dialog")
.setMessage ( "whether to exit the application?")
.setPositiveButton("确定", new DialogInterface.OnClickListener() {
@Override
public void onClick (DialogInterface dialog, int i) {// Note that this dialog parameter is not above a private variable dialog
dialog.dismiss();
MainActivity.this.finish();
}
})
.setNegativeButton("取消", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int i) {
dialog.dismiss();
}
});
dialog = builder.create();
dialog.show();
}
}
operation result:
Case II: radio Dialog
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity"
android:orientation="vertical">
<TextView
android:id="@+id/tv"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android: text = "radio box"
android:layout_marginTop="10dp"
android:textSize="20sp"
android:textColor="#FFDB37"
/>
<Button
android:id="@+id/bt"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android: text = "Set the font size."
android:layout_marginTop="20dp"
android:layout_gravity="center"/>
</LinearLayout>
MainActivty.java
package com.example.singlechoicedialog;
import androidx.appcompat.app.AlertDialog;
import androidx.appcompat.app.AppCompatActivity;
import android.content.DialogInterface;
import android.os.Bundle;
import android.view.View;
import android.widget.TextView;
public class MainActivity extends AppCompatActivity implements View.OnClickListener{
private TextView textView;
private int[] textSizeArr = {10,20,25,30,40};
int textSize = 1;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate (savedInstanceState);
setContentView(R.layout.activity_main);
findViewById(R.id.bt).setOnClickListener(this);
textView =(TextView) findViewById(R.id.tv);
}
@Override
public void onClick(View view) {
final AlertDialog dialog;
AlertDialog.Builder builder = new AlertDialog.Builder(this)
.setTitle ( "set the font size")
.setIcon(R.mipmap.ic_launcher)
.setSingleChoiceItems (new String [] { "S", "Default", "medium", "large", "large"},
textSize, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialogInterface, int i) {
textSize = i;
}
})
.setPositiveButton("确定", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int i) {
textView.setTextSize(textSizeArr[textSize]);
dialog.dismiss();
}
})
.setNegativeButton("取消", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int i) {
dialog.dismiss();
}
});
dialog = builder.create();
dialog.show();
}
}
operation result:
Case 3: multiple choice box
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<Button
android:id="@+id/bt"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android: text = "open small hobbies investigation">
</Button>
</LinearLayout>
MainActivity.java
package com.example.multichoicedialog;
import androidx.appcompat.app.AlertDialog;
import androidx.appcompat.app.AppCompatActivity;
import android.content.DialogInterface;
import android.os.Bundle;
import android.view.View;
import android.widget.Toast;
public class MainActivity extends AppCompatActivity implements View.OnClickListener{
private CharSequence [] items = new CharSequence [] { "tourism", "food", "movie", "sports"};
private boolean[] checkedItems = new boolean[]{false,true,false,false};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate (savedInstanceState);
setContentView(R.layout.activity_main);
findViewById(R.id.bt).setOnClickListener(this);
}
@Override
public void onClick(View view) {
final AlertDialog dialog;
AlertDialog.Builder builder = new AlertDialog.Builder(this)
.setTitle ( "add interests")
.setIcon(R.mipmap.ic_launcher)
.setMultiChoiceItems(items, checkedItems, new DialogInterface.OnMultiChoiceClickListener() {
@Override
public void onClick(DialogInterface dialogInterface, int i, boolean b) {
checkedItems[i] = b;
}
})
.setPositiveButton("确定", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialogInterface, int which) {
StringBuffer stringBuffer = new StringBuffer();
for(int i =0;i<=checkedItems.length-1;i++){
if(checkedItems[i]){
stringBuffer.append(items[i]).append(" ");
}
}
if(stringBuffer != null){
Toast.makeText(MainActivity.this,
""+stringBuffer,Toast.LENGTH_SHORT).show();
}
dialogInterface.dismiss();
}
})
.setNegativeButton("取消", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialogInterface, int i) {
dialogInterface.dismiss();
}
});
dialog = builder.create();
dialog.show();
}
}
operation result:
Case Four: Customize Dialog
custom_dialog.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="match_parent"
android:orientation="vertical">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:paddingTop="16dp"
android:orientation="vertical">
<TextView
android:id="@+id/title"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:visibility="visible"
android:textColor="#333333"
android:textSize="18sp"
android:layout_marginBottom="16dp"/>
<TextView
android:id="@+id/message"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:layout_marginLeft="20dp"
android:layout_marginRight="20dp"
android:textSize="14sp"
android:textColor="#999999"/>
<View
android:layout_width="match_parent"
android:layout_height="2px"
android:layout_marginTop="16dp"
android:background="#E8E8E8"/>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<Button
android:id="@+id/negtive"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginLeft="10dp"
android:paddingTop="16dp"
android:paddingBottom="16dp"
android:layout_weight="1"
android:background="@null"
android:layout_gravity="center"
android:singleLine="true"
android:textColor="#999999"
android:textSize="16sp"/>
<View
android:id="@+id/colum_line"
android:layout_width="2px"
android:layout_height="match_parent"
android:background="#E8E8E8"/>
<Button
android:id="@+id/positive"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:layout_marginRight="10dp"
android:paddingTop="16dp"
android:paddingBottom="16dp"
android:background="@null"
android:gravity="center"
android:textColor="#38ADFF"
android:textSize="16sp" />
</LinearLayout>
</LinearLayout>
</LinearLayout>
CommonDialog.java
package com.example.customdialog;
import android.app.AlertDialog;
import android.content.Context;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
public class CommonDialog extends AlertDialog {
private TextView titleTv; // display the title
private TextView messageTv; // display a message
private Button negtiveBn, positiveBn; // Confirm and Cancel buttons
public CommonDialog(Context context) {
super(context);
}
private String message;
private String title;
private String positive,negtive;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate (savedInstanceState);
setContentView(R.layout.custom_dialog);
initView (); // initialization interface controls
initEvent (); // initialization interface controls click event
}
// Initialize interface controls
private void initView() {
negtiveBn = (Button) findViewById(R.id.negtive);
positiveBn = (Button) findViewById(R.id.positive);
titleTv = (TextView) findViewById(R.id.title);
messageTv = (TextView) findViewById(R.id.message);
}
// initialize display data interface controls
private void refreshView() {
// If you customize the title and message will display customized information, or do not display the information of title and message
if (!TextUtils.isEmpty(title)) {
titleTv.setText (title); // set the title text for the control of a custom title
titleTv.setVisibility (View.VISIBLE); // header control is set to display status
}else {
titleTv.setVisibility (View.GONE); // header control is set to hidden
}
if (!TextUtils.isEmpty(message)) {
messageTv.setText (message); message control information // setup message text custom
}
// If you customize the text of the button, the button text to customize. Otherwise, the button displays "OK" or "Cancel" Text
if (!TextUtils.isEmpty(positive)) {
positiveBn.setText (positive); // set the button text information customized text
}else {
positiveBn.setText ( "OK"); // set the button text to "OK"
}
if (!TextUtils.isEmpty(negtive)) {
negtiveBn.setText(negtive);
}else {
negtiveBn.setText("取消");
}
}
// Initialize interface OK and Cancel listeners
private void initEvent() {
// Set the OK button click event listener
positiveBn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (onClickBottomListener!= null) {
onClickBottomListener.onPositiveClick();
}
}
});
// Set listeners cancel button click event
negtiveBn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if ( onClickBottomListener!= null) {
onClickBottomListener.onNegtiveClick();
}
}
});
}
@Override
public void show() {
super.show();
refreshView();
}
public interface OnClickBottomListener{
void onPositiveClick (); // realize the OK button click event method
void onNegtiveClick (); // method to achieve the Cancel button click event
}
// Set OK Cancel button callback
public OnClickBottomListener onClickBottomListener;
public CommonDialog setOnClickBottomListener(OnClickBottomListener
onClickBottomListener){
this.onClickBottomListener = onClickBottomListener;
return this;
}
public CommonDialog setMessage(String message) {
this.message = message;
return this ;
}
public CommonDialog setTitle(String title) {
this.title = title;
return this ;
}
public CommonDialog setPositive(String positive) {
this.positive = positive;
return this ;
}
public CommonDialog setNegtive(String negtive) {
this.negtive = negtive;
return this ;
}
}
activty_main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity"
android:gravity="center">
<Button
android:id="@+id/btn_dialog"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android: text = "custom dialog box pops up" />
</LinearLayout>
MainActivty.java
package com.example.customdialog;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate (savedInstanceState);
setContentView(R.layout.activity_main);
init();
}
private void init() {
Button btn_dialog = findViewById(R.id.btn_dialog);
btn_dialog.setOnClickListener(new View.OnClickListener(){
@Override
public void onClick(View view) {
final CommonDialog dialog = new CommonDialog(MainActivity.this);
dialog.setTitle ( "Tip");
dialog.setMessage ( "Are you sure you want to delete the information?");
dialog.setNegtive ( "Cancel");
dialog.setPositive ( "OK");
dialog.setOnClickBottomListener(new CommonDialog.
OnClickBottomListener() {
@Override
public void onPositiveClick () {// OK button click event
dialog.dismiss();
}
@Override
public void onNegtiveClick () {// Cancel button click event
dialog.dismiss();
}
});
dialog.show();
}
});
}
}
operation result: