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
- Create an empty activity android studio project. Select Java or Kotlin as the programming language.
- Refer to Android | How to Create/Start a New Project in Android Studio? to know how to create a project with the empty activity android studio 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 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:
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);
}
}
}
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)
}
}
}