Skip to content
Related Articles
Open in App
Not now

Related Articles

How to Customize AlertDialog Dimensions in Android?

Improve Article
Save Article
  • Last Updated : 11 Aug, 2021
Improve Article
Save Article

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 by Step Implementation

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 version="1.0" encoding="utf-8"?>
    <!-- Click this button to show alert dialog -->
        android:text="Show Alert Dialog"

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.


import android.os.Build
import android.os.Bundle
import android.view.WindowManager
import android.widget.Button
import androidx.annotation.RequiresApi
class MainActivity : AppCompatActivity() {
    override fun onCreate(savedInstanceState: Bundle?) {
        // Declaring and initializing button from the layout
        val mButton = findViewById<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()
            // 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


You can see that the AlertDialog dimensions are now customized.

My Personal Notes arrow_drop_up
Related Articles

Start Your Coding Journey Now!