SoFunction
Updated on 2025-04-07

Android Dialog dialog example code explanation

Basic Dialog Methods

//Create Dialog builder = new ();
//Set the title icon(.ic_launcher);
//Set the title("This is a dialog box");
//Setting information("Do you want to jump?");
//OK buttonsetPositiveButton(CharSequence text,  listener)
//Cancel buttonsetNegativeButton (CharSequence text,  listener)
//neglectsetNeutralButton (CharSequence text,  listener)
//Show dialog boxshow();

System Style

1. Drop-down list

 builder = new ();
     (.ic_launcher);
     ("Choose a city");
     //The data source of the drop-down list     final String[] cities = {"Beijing", "Shanghai", "Guangzhou", "Shenzhen", "Hangzhou"};
     (cities, new (){
       @Override
       public void onClick(DialogInterface dialog, int which){
         (, "The city you choose is:" + cities[which], Toast.LENGTH_SHORT).show();
       }
 });
 ();

2. Radio box list

 builder = new ();
    (.ic_launcher);
    ("Please choose gender");
    final String[] sex = {"male", "female"};
    //The second parameter specifies which radio box is checked by default    (sex, 1, new (){
      @Override
      public void onClick(DialogInterface dialog, int which){
          (, "Gender is:" + sex[which], Toast.LENGTH_SHORT).show();
      }
    });
    ("Sure", new (){
      @Override
      public void onClick(DialogInterface dialog, int which){
            
      }
    });
    ("Cancel", new (){
      @Override
      public void onClick(DialogInterface dialog, int which){
            
      }
    });
();

3. Multiple selection box list

String str;
 builder = new ();
(.ic_launcher);
("Choose the TV series you want to watch");
final String[] hobbies = {"Story of Tingxi", "Fuyao", "Ashes of sweetness", "Ruyi's Royal Love in the Palace"};
//What options are selected for the second parameter? You need to pass a boolean[] array into it. The length must be the same as the length of the first parameter. If null is used, it will not be selected.(hobbies, null, new (){
  @Override
public void onClick(DialogInterface dialog, int which, boolean isChecked){
  if(isChecked){
  (hobbies[which] + ", ");
  }
  (, "The selected one is:" + (), Toast.LENGTH_SHORT).show();
  }
 });
("Sure", new (){
@Override
public void onClick(DialogInterface dialog, int which){

  }
});
("Cancel", new (){
@Override
public void onClick(DialogInterface dialog, int which){

  }
});
();

4. Wait for the dialog box

ProgressDialog waitingDialog= new ProgressDialog();
("Waiting for loading, please wait...");
("Waiting...");
(true);//Use uncertain progress bar(false);//Click external to not cancel the dialog box();

5. Progress bar dialog box

int MAXPD = 100;
ProgressDialog progressDialog = new ProgressDialog();
(0);//Set the default value("Downloading");
(ProgressDialog.STYLE_HORIZONTAL);//Progress bar style(MAXPD);//Set the maximum value();

Custom Dialog

1. Inherit Dialog

public class CustomDialog extends Dialog {
//titleprotected TextView hintTv;
//The button on the leftprotected Button doubleLeftBtn;
//The button on the rightprotected Button doubleRightBtn;
//Input boxpublic EditText editText;
public CustomDialog(Context context) {
  super(context, );
}
@Override
protected void onCreate(Bundle savedInstanceState) {
  (savedInstanceState);
  (false); // Is it possible to cancel  setContentView(.dialog_custom);
  hintTv = findViewById(.tv_common_dialog_hint);
  doubleLeftBtn = findViewById(.btn_common_dialog_double_left);
  doubleRightBtn = findViewById(.btn_common_dialog_double_right);
  editText = findViewById(.et_common_dialog);
}
//Right-click text and click eventspublic void setRightButton(String rightStr,  clickListener) {
  (clickListener);
  (rightStr);
}
//Set left-click text and click eventpublic void setLeftButton(String leftStr,  clickListener) {
  (clickListener);
  (leftStr);
}
//Set the prompt contentpublic void setHintText(String str) {
  (str);
  ();
}
//Give two buttons to set textpublic void setBtnText(String leftStr, String rightStr) {
  (leftStr);
  (rightStr);
}
}

2. Custom Style

<style name="CustomDialogStyle" parent="@android:style/">
    <!-- frame -->
    <item name="android:windowFrame">@null</item>
    <!-- Transparent background -->
    <item name="android:windowBackground">@color/transparent</item>
    <!-- Untitled -->
    <item name="android:windowNoTitle">true</item>
    <!-- Does it appearactivityAbove -->
    <item name="android:windowIsFloating">true</item>
    <!-- translucent -->
    <item name="android:windowIsTranslucent">false</item>
    <!-- Blurred background -->
    <item name="android:windowContentOverlay">@null</item>
    <!-- Allows the background of the dialog box to darken -->
    <item name="android:backgroundDimEnabled">true</item>
    <!-- Font color -->
    <item name="android:textColor">@color/white</item>
    <item name="android:editTextColor">@color/white</item>
 </style>

3. Custom layout

<LinearLayout
  xmlns:andro
  xmlns:tools="/tools"
  android:
  android:layout_width="500dp"
  android:layout_height="250dp"
  android:layout_margin="5dp"
  android:background="@drawable/background_info"
  android:orientation="vertical"
  android:gravity="center">

  <!--Prompt information-->
  <TextView
    android:
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:gravity="center"
    android:textColor="@color/white"
    android:textSize="27sp"/>
  <EditText
    android:
    android:layout_width="300dp"
    android:layout_height="wrap_content"
    android:textColor="@color/back"
    android:inputType="text|textNoSuggestions"
    tools:ignore="LabelFor"
    android:hint="Please enter your password"/>
  
  <!--Bottom button-->
  <LinearLayout
    android:
    android:layout_width="360dp"
    android:layout_height="60dp"
    android:layout_margin="20dp"
    android:orientation="horizontal">

    <Button
      android:
      android:layout_width="0dp"
      android:layout_height="match_parent"
      android:layout_weight="1"
      android:background="@drawable/btnselector"
      android:gravity="center"
      android:textColor="@color/white"
      android:textSize="27dp"/>

    <Button
      android:
      android:layout_width="0dp"
      android:layout_height="match_parent"
      android:layout_weight="1"
      android:background="@drawable/btnselector"
      android:gravity="center"
      android:textColor="@color/white"
      android:textSize="27dp"/>
  </LinearLayout>
</LinearLayout>

Hide the bottom virtual buttons

//Hide the bottom virtual button when the dialog pops up().getDecorView().setSystemUiVisibility(View.SYSTEM_UI_FLAG_HIDE_NAVIGATION);
().getDecorView().setOnSystemUiVisibilityChangeListener(new () {
  @Override
  public void onSystemUiVisibilityChange(int visibility) {
    int uiOptions =View.SYSTEM_UI_FLAG_HIDE_NAVIGATION;
    if (.SDK_INT >= 19) {
      uiOptions |= 0x00001000;
    } else {
      uiOptions |= View.SYSTEM_UI_FLAG_LOW_PROFILE;
    }
    ().getDecorView().setSystemUiVisibility(uiOptions);
  }
});

5. Use a custom Dialog

CustomDialog dialog = new CustomDialog(this);
("Please enter your password");
("Cancel", new () {
    @Override
    public void onClick(View v) {
      ();
    }
});
("Sure", new () {
    @Override
    public void onClick(View v) {
        
    }
});
();

Summarize

The above is an explanation of the Android Dialog dialog example code introduced by the editor. I hope it will be helpful to everyone. If you have any questions, please leave me a message and the editor will reply to everyone in time. Thank you very much for your support for my website!