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.
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: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.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.
Similar Reads
How to Create a Custom AlertDialog in Android? In some cases for the AlertDialog, there is a need to get input from the user or customize it according to our requirements. That's where custom AlertDialogs comes in handy. In this article we are going to discuss on how to customize the AlertDialogs and take user input.Example: Steps to Implement o
2 min read
How to Create a Full Screen AlertDialog in Android? AlertDialog in Android is an alert message programmatically displayed to the user over the change of course of action. This appears as a pop-up and has four elements i.e., a title, a message, a positive button, and a negative button. Typically, AlertDialog is non-customized and appears as an overlay
2 min read
How to Create an Alert Dialog Box in Android? An Android Alert Dialog is a UI element that displays a warning or notification message and asks the user to respond with options such as Yes or No. Based on the user's response, appropriate actions are executed. Android Alert Dialog is built with the use of three fields: Title, Message area, and Ac
4 min read
How to Prevent AlertDialog Box From Closing in Android? In Android, AlertDialog is a simple alert message that appears in the form of a pop-up that consists of a title, a message, and two buttons namely positive and negative buttons. The user basically has to click on one of the two buttons to reply to the message in the AlertDialog. The negative button
3 min read
AlertDialog in Android using Jetpack Compose AlertDialog shows the Alert message and gives the answer in the form of yes or no. Alert Dialog displays the message to warn you and then according to your response, the next step is processed. Android Alert Dialog is built with the use of three fields: Title, Message area, and Action Button.We have
3 min read