How to Customize AlertDialog Dimensions in Android? - GeeksforGeeks

11 Aug, 2021
AlertDialog in Android is a kind of pop-up message that alerts the user about the activity usages. This is specifically developed by a developer to perform any operations or ask for any permissions and not an Android OS call. Alert Dialog in general has no fixed dimension values and rather varies from device to device depending upon the application screen size.
In this article, we will show you how you could customize the Alert Dialog dimensions to a scale in your Android application.
Step 1: Create a New Project in Android Studio
To create a new project in Android Studio please refer to How to Create/Start a New Project in Android Studio. We demonstrated the application in Kotlin, so make sure you select Kotlin as the primary language while creating a New Project.
Step 2: Working with the activity_main.xml file
Navigate to the app > res > layout > activity_main.xml and add the below code to that file. Below is the code for the activity_main.xml file. Add a Button in the layout file. Create a Button that on click will generate an AlertDialog.
XML
<?xml version="1.0" encoding="utf-8"?><RelativeLayout     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">      <!-- Click this button to show alert dialog -->    <Button        android:id="@+id/button"        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:text="Show Alert Dialog"        android:layout_centerInParent="true"/>  </RelativeLayout>
Step 3: Working with the MainActivity.kt file
Go to the MainActivity.kt file and refer to the following code. Below is the code for the MainActivity.kt file. Comments are added inside the code to understand the code in more detail.
Kotlin
import android.os.Buildimport androidx.appcompat.app.AppCompatActivityimport android.os.Bundleimport android.view.WindowManagerimport android.widget.Buttonimport androidx.annotation.RequiresApiimport androidx.appcompat.app.AlertDialog  class MainActivity : AppCompatActivity() {    @RequiresApi(Build.VERSION_CODES.R)    override fun onCreate(savedInstanceState: Bundle?) {        super.onCreate(savedInstanceState)        setContentView(R.layout.activity_main)          // Declaring and initializing button from the layout        val mButton = findViewById<Button>(R.id.button)          // When button is clicked        mButton.setOnClickListener {              // Build an alert dialog, declare its attributes            val mDialogBuilder = AlertDialog.Builder(this)            mDialogBuilder.apply {                setTitle("This is Title")                setMessage("This is Message: \n\nWidth & Height set to 50% of app screen size")                setPositiveButton("Positive Button", null)                setNegativeButton("Negative Button", null)            }              // Create and show the dialog            val mDialog = mDialogBuilder.create()            mDialog.show()              // Get the current app screen width and height            val mDisplayMetrics = windowManager.currentWindowMetrics            val mDisplayWidth = mDisplayMetrics.bounds.width()            val mDisplayHeight = mDisplayMetrics.bounds.height()              // Generate custom width and height and             // add to the dialog attributes            // we multiplied the width and height by 0.5,            // meaning reducing the size to 50%            val mLayoutParams = WindowManager.LayoutParams()            mLayoutParams.width = (mDisplayWidth * 0.5f).toInt()            mLayoutParams.height = (mDisplayHeight * 0.5f).toInt()            mDialog.window?.attributes = mLayoutParams        }    }}
Output:
You can see that the AlertDialog dimensions are now customized.
Android
Kotlin
Android
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Resource Raw Folder in Android Studio
Flutter - Custom Bottom Navigation Bar
How to Read Data from SQLite Database in Android?
Flexbox-Layout in Android
Retrofit with Kotlin Coroutine in Android
Android UI Layouts
Kotlin Array
Retrofit with Kotlin Coroutine in Android
Kotlin Setters and Getters
Kotlin when expression