SoFunction
Updated on 2025-03-11

Example of automatic control method for Android programming to implement custom Dialog size

This article describes the automatic control method of Android programming to implement custom Dialog size. Share it for your reference, as follows:

In Android application development, it is very necessary to pop up a dialog box (Dialog) for some operation prompts, whether for functions or to increase user experience. The Android system has its own dialog boxes of various styles, but the requirements may not be able to meet the effect according to the project needs, so we need to customize the dialog boxes at the same time.

We can customize the Dialog style and display layout to create the dialog box we want, but sometimes, the dialog box we make is either too large or too small, or different page sizes are different, so we need to make a unified look! At this time, we need to control the Dialog size, and let’s briefly talk about this today. Post the code, with detailed instructions in the comments.

First, we customize the layout of the Dialog:

<LinearLayout xmlns:andro
  xmlns:tools="/tools"
  android:layout_width="match_parent"
  android:layout_height="141dp"
  android:orientation="vertical" >
  <LinearLayout
    android:layout_width="match_parent"
    android:layout_height="92dp"
    android:background="@drawable/dialogbackground"
    android:gravity="center"
    android:orientation="vertical" >
    <TextView
      android:
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:layout_gravity="center"
      android:layout_marginTop="10dp"
      android:lineSpacingExtra="3dp"
      android:lineSpacingMultiplier="1.2"
      android:textColor="#333333"
      android:textSize="15sp"
      android:visibility="gone" />
    <TextView
      android:
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:layout_gravity="center"
      android:lineSpacingExtra="3dp"
      android:lineSpacingMultiplier="1.2"
      android:layout_marginLeft="15dp"
      android:layout_marginRight="15dp"
      android:text="@string/my_bank_delete_dialog"<!--Here is the prompt text,Can be changed in code-->
      android:layout_marginTop="3dp"
      android:textColor="#333333"
      android:textSize="15sp" />
  </LinearLayout>
  <LinearLayout
    android:layout_width="match_parent"
    android:layout_height="40dp"
    android:gravity="center"
    android:orientation="horizontal" >
    <Button
      android:
      android:layout_width="wrap_content"
      android:layout_height="match_parent"
      android:layout_weight="1"
      android:background="@drawable/dialogsuccess_bg"
      android:gravity="center"
      android:text="Cancel"
      android:textColor="#007aff"
      android:textSize="17sp" />
    <Button
      android:
      android:layout_width="wrap_content"
      android:layout_height="match_parent"
      android:layout_weight="1"
      android:background="@drawable/dialogerror"
      android:gravity="center"
      android:text="Sure"
      android:textColor="#007aff"
      android:textSize="17sp" />
  </LinearLayout>
</LinearLayout>

The following is the implementation code of the dialog box:

First define it in the class

private Dialog mDialog;
//The following is the method of popping up the dialog box. Just call it where the dialog box needs to pop up. Of course, you can remove the method and write the dialog box code directly.protected void showIsDeleteDialog() {
View view = (getActivity()).inflate(.common_no_title_dialog, null);
TextView tv = (TextView) (.dialog_content_tv);
("Do you want to do the following?");//This is the prompt text mentioned above, you can modify it hereButton mCancel = (Button) ();
Button mSure= (Button) ();
// Cancel operation(new OnClickListener() {
@Override
public void onClick(View v) {
();
}
});
//Confirm the operation(new OnClickListener() {
@Override
public void onClick(View v) {
clearRecordRequest();
();
}
});
mDialog = new Dialog(getActivity(), );//Custom style, no code posted(view);
();
Window dialogWindow = ();
WindowManager m = getActivity().getWindowManager();
Display d = (); // Get screen width and height p = (); // Get the current parameter value of the dialog box = (int) (() * 0.8); // Set the height to 0.6 of the screen and adjust it according to the actual situation = (int) (() * 0.8); // Set the width to 0.65 of the screen, adjust according to actual conditions(p);
}

The code ends!

For more information about Android related content, please check out the topic of this site:Android development introduction and advanced tutorial》、《Android debugging skills and solutions to common problems》、《Summary of the usage of basic Android components》、《Android View View Tips Summary》、《Android layout layout tips summary"and"Android control usage summary

I hope this article will be helpful to everyone's Android programming design.