Skip to content
Related Articles

Related Articles

Improve Article

How to Customize AlertDialog Dimensions in Android?

  • Last Updated : 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.

Want a more fast-paced & competitive environment to learn the fundamentals of Android?
Click here to head to a guide uniquely curated by our experts with the aim to make you industry ready in no time!

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




<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 
    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.Build
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.view.WindowManager
import android.widget.Button
import androidx.annotation.RequiresApi
import 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.




My Personal Notes arrow_drop_up
Recommended Articles
Page :