How to Change the Position of AlertDialog in Android?
Last Updated :
10 Feb, 2025
AlertDialog in android is one of the UI widgets which immediately pops up to confirm the user interaction or to confirm the action which is done by the user. In most of the applications, the position of the alert dialog is in the center. In this article, it's been discussed how to change the position of the alert dialog in android. Have a look at the following image to differentiate the normal alert dialog with the center position and the alert dialog with the changed position.
Note that we are going to implement this project using both the language Java and Kotlin .
Steps to implement the Alert Dialog with Different Position Step 1: Create an empty activity project Step 2: Working with the activity_main.xml file The main layout of the application contains one button which is used to build show the alert dialog at the specified position. Invoke the following code inside the activity_main.xml file to implement the UI. activity_main.xml:
XML
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout
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"
android:orientation= "vertical"
android:backgroundTint= "@color/white"
tools:context= ".MainActivity" >
<Button
android:id= "@+id/topAlert"
android:layout_width= "wrap_content"
android:layout_height= "wrap_content"
android:backgroundTint= "@android:color/holo_green_dark"
android:text= "OPEN TOP ALERT"
android:textSize= "18sp"
app:layout_constraintBottom_toTopOf= "@+id/centerAlert"
app:layout_constraintEnd_toEndOf= "parent"
app:layout_constraintHorizontal_bias= "0.5"
app:layout_constraintStart_toStartOf= "parent"
app:layout_constraintTop_toTopOf= "parent"
app:layout_constraintVertical_chainStyle= "packed" />
<Button
android:id= "@+id/centerAlert"
android:layout_width= "wrap_content"
android:layout_height= "wrap_content"
android:layout_marginTop= "16dp"
android:layout_marginBottom= "16dp"
android:backgroundTint= "@android:color/holo_green_dark"
android:text= "OPEN CENTER ALERT"
android:textSize= "18sp"
app:layout_constraintBottom_toTopOf= "@+id/bottomAlert"
app:layout_constraintEnd_toEndOf= "parent"
app:layout_constraintHorizontal_bias= "0.5"
app:layout_constraintStart_toStartOf= "parent"
app:layout_constraintTop_toBottomOf= "@+id/topAlert" />
<Button
android:id= "@+id/bottomAlert"
android:layout_width= "wrap_content"
android:layout_height= "wrap_content"
android:backgroundTint= "@android:color/holo_green_dark"
android:text= "OPEN BOTTOM ALERT"
android:textSize= "18sp"
app:layout_constraintBottom_toBottomOf= "parent"
app:layout_constraintEnd_toEndOf= "parent"
app:layout_constraintHorizontal_bias= "0.5"
app:layout_constraintStart_toStartOf= "parent"
app:layout_constraintTop_toBottomOf= "@+id/centerAlert" />
</androidx.constraintlayout.widget.ConstraintLayout>
Output UI:
Step 3: Working with the MainActivity.java file For changing the alert dialog position at the top of the Android window, invoke the following code to implement. Comments are added for better understanding.
MainActivity File:
Java
package org.geeksforgeeks.demo ;
import android.os.Bundle ;
import android.view.Gravity ;
import android.view.Window ;
import android.widget.Button ;
import androidx.appcompat.app.AlertDialog ;
import androidx.appcompat.app.AppCompatActivity ;
public class MainActivity extends AppCompatActivity {
// Creating Button instance
private Button topAlert , centerAlert , bottomAlert ;
// Overriding the onCreate Method
@Override
protected void onCreate ( Bundle savedInstanceState ) {
super . onCreate ( savedInstanceState );
setContentView ( R . layout . activity_main );
// Register buttons with their respective IDs
topAlert = findViewById ( R . id . topAlert );
centerAlert = findViewById ( R . id . centerAlert );
bottomAlert = findViewById ( R . id . bottomAlert );
// Handle the "Top Alert" button click
topAlert . setOnClickListener ( v -> {
AlertDialog alertDialog = createAlertDialog ( "Top Alert dialog" );
showAlertWithGravity ( alertDialog , Gravity . TOP );
});
// Handle the "Center Alert" button click
centerAlert . setOnClickListener ( v -> {
AlertDialog alertDialog = createAlertDialog ( "Center Alert dialog" );
showAlertWithGravity ( alertDialog , Gravity . CENTER );
});
// Handle the "Bottom Alert" button click
bottomAlert . setOnClickListener ( v -> {
AlertDialog alertDialog = createAlertDialog ( "Bottom Alert dialog" );
showAlertWithGravity ( alertDialog , Gravity . BOTTOM );
});
}
/**
* Helper method to create an AlertDialog with a specified message.
*/
private AlertDialog createAlertDialog ( String message ) {
AlertDialog . Builder builder = new AlertDialog . Builder ( this )
. setIcon ( R . drawable . gfg_logo )
. setTitle ( "This is Alert Dialog" )
. setMessage ( message )
. setNeutralButton ( "DISMISS" , ( dialog , which ) -> dialog . dismiss ());
return builder . create ();
}
/**
* Helper method to show an AlertDialog and set its gravity.
*/
private void showAlertWithGravity ( AlertDialog alertDialog , int gravity ) {
alertDialog . show ();
Window window = alertDialog . getWindow ();
if ( window != null ) {
window . setGravity ( gravity );
}
}
}
Kotlin
package org.geeksforgeeks.demo
import android.os.Bundle
import android.view.Gravity
import android.widget.Button
import android.widget.Toast
import androidx.appcompat.app.AlertDialog
import androidx.appcompat.app.AppCompatActivity
class MainActivity : AppCompatActivity ()
{
override fun onCreate ( savedInstanceState : Bundle?) {
super . onCreate ( savedInstanceState )
setContentView ( R . layout . activity_main )
// register the button with it's appropriate ID
val topAlert : Button = findViewById ( R . id . topAlert )
val centerAlert : Button = findViewById ( R . id . centerAlert )
val bottomAlert : Button = findViewById ( R . id . bottomAlert )
// handle the SHOW ALERT DIALOG BUTTON
topAlert . setOnClickListener {
val builder = AlertDialog . Builder ( this )
. setIcon ( R . drawable . gfg_logo )
. setTitle ( "This is Alert Dialog" )
. setMessage ( "Top Alert dialog" )
// setup neutral button
builder . setNeutralButton ( "DISMISS" ) { _ , _ -> }
// show the alert dialog
val alertDialog = builder . create ()
alertDialog . show ()
// set the gravity of the window to the top
alertDialog . window !! . setGravity ( Gravity . TOP )
}
centerAlert . setOnClickListener {
val builder = AlertDialog . Builder ( this )
. setIcon ( R . drawable . gfg_logo )
. setTitle ( "This is Alert Dialog" )
. setMessage ( "Center Alert dialog" )
// setup neutral button
builder . setNeutralButton ( "DISMISS" ) { _ , _ -> }
// show the alert dialog
val alertDialog = builder . create ()
alertDialog . show ()
// set the gravity of the window to the top
alertDialog . window !! . setGravity ( Gravity . CENTER )
}
bottomAlert . setOnClickListener {
val builder = AlertDialog . Builder ( this )
. setIcon ( R . drawable . gfg_logo )
. setTitle ( "This is Alert Dialog" )
. setMessage ( "Bottom Alert dialog" )
// setup neutral button
builder . setNeutralButton ( "DISMISS" ) { _ , _ -> }
// show the alert dialog
val alertDialog = builder . create ()
alertDialog . show ()
// set the gravity of the window to the top
alertDialog . window !! . setGravity ( Gravity . BOTTOM )
}
}
}
Output:
Similar Reads
How to Change Alert Dialog Position on Screen in Android? AlertDialog in Android is an alert message that appears in the form of a pop-up consisting of four elements namely a title, a message, a positive button, and a negative button. The positive and the negative buttons are clickable and can be programmed for performing an action. However, the AlertDialo
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 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
How to Implement Loading AlertDialog in Android? AlertDialog is defined as the small window that shows a particular message to the user when the user performs or commits certain action. In this article, we are going to build a simple android application in which we learn how to implement a Loading AlertDialog that means whenever the user clicks on
6 min read
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 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