How to Change the Position of AlertDialog in Android? - GeeksforGeeks

21 Dec, 2020
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 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 the Java language. 
Step 1: Create an empty activity project
Create an empty activity android studio project. And select Java 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.
XML
<?xml version="1.0" encoding="utf-8"?><RelativeLayout     xmlns:android="http://schemas.android.com/apk/res/android"    xmlns:tools="http://schemas.android.com/tools"    android:layout_width="match_parent"    android:layout_height="match_parent"    tools:context=".MainActivity"    tools:ignore="HardcodedText">      <Button        android:id="@+id/showAlertDialogButton"        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:layout_centerInParent="true"        android:backgroundTint="@color/colorPrimary"        android:text="SHOW ALERT DIALOG"        android:textColor="@android:color/white"        android:textSize="18sp" />  </RelativeLayout>
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.
Java
import androidx.appcompat.app.AlertDialog;import androidx.appcompat.app.AppCompatActivity;  import android.content.DialogInterface;import android.os.Bundle;import android.view.Gravity;import android.view.View;import android.widget.Button;import android.widget.Toast;  public class MainActivity extends AppCompatActivity {      @Override    protected void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentView(R.layout.activity_main);          // register the button with it's appropriate ID        Button bShowAlertDialog = findViewById(R.id.showAlertDialogButton);          // handle the SHOW ALERT DIALOG BUTTON        bShowAlertDialog.setOnClickListener(new View.OnClickListener() {            @Override            public void onClick(View v) {                  // instance of alert dialog to build alert dialog                AlertDialog.Builder builder = new AlertDialog.Builder(MainActivity.this);                builder.setIcon(R.drawable.logo);                builder.setTitle("This is Alert Dialog");                builder.setMessage("Bottom Alert dialog");                  // set the neutral button to do some actions                builder.setNeutralButton("DISMISS", new DialogInterface.OnClickListener() {                    @Override                    public void onClick(DialogInterface dialog, int which) {                        Toast.makeText(MainActivity.this, "Alert Dialog Dismissed", Toast.LENGTH_SHORT).show();                    }                });                  // set the positive button to do some actions                builder.setPositiveButton("OKAY", new DialogInterface.OnClickListener() {                    @Override                    public void onClick(DialogInterface dialog, int which) {                        Toast.makeText(MainActivity.this, "OKAY", Toast.LENGTH_SHORT).show();                    }                });                  // set the negative button to do some actions                builder.setNegativeButton("CANCEL", new DialogInterface.OnClickListener() {                    @Override                    public void onClick(DialogInterface dialog, int which) {                        Toast.makeText(MainActivity.this, "CANCEL", Toast.LENGTH_SHORT).show();                    }                });                  // show the alert dialog                AlertDialog alertDialog = builder.create();                alertDialog.show();                  // get the top most window of the android                   // screen using getWindow() method                // and set the the gravity of the window to                   // top that is alert dialog will be now at                // the topmost position                alertDialog.getWindow().setGravity(Gravity.TOP);            }        });    }}
For changing the alert dialog position at the bottom of the Android window, invoke the following code to implement. Comments are added for better understanding.
Java
import androidx.appcompat.app.AlertDialog;import androidx.appcompat.app.AppCompatActivity;  import android.content.DialogInterface;import android.os.Bundle;import android.view.Gravity;import android.view.View;import android.widget.Button;import android.widget.Toast;  public class MainActivity extends AppCompatActivity {      @Override    protected void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentView(R.layout.activity_main);          // register the button with it's appropriate ID        Button bShowAlertDialog = findViewById(R.id.showAlertDialogButton);          // handle the SHOW ALERT DIALOG BUTTON        bShowAlertDialog.setOnClickListener(new View.OnClickListener() {            @Override            public void onClick(View v) {                  // instance of alert dialog to build alert dialog                AlertDialog.Builder builder = new AlertDialog.Builder(MainActivity.this);                builder.setIcon(R.drawable.logo);                builder.setTitle("This is Alert Dialog");                builder.setMessage("Bottom Alert dialog");                  // set the neutral button to do some actions                builder.setNeutralButton("DISMISS", new DialogInterface.OnClickListener() {                    @Override                    public void onClick(DialogInterface dialog, int which) {                        Toast.makeText(MainActivity.this, "Alert Dialog Dismissed", Toast.LENGTH_SHORT).show();                    }                });                  // set the positive button to do some actions                builder.setPositiveButton("OKAY", new DialogInterface.OnClickListener() {                    @Override                    public void onClick(DialogInterface dialog, int which) {                        Toast.makeText(MainActivity.this, "OKAY", Toast.LENGTH_SHORT).show();                    }                });                  // set the negative button to do some actions                builder.setNegativeButton("CANCEL", new DialogInterface.OnClickListener() {                    @Override                    public void onClick(DialogInterface dialog, int which) {                        Toast.makeText(MainActivity.this, "CANCEL", Toast.LENGTH_SHORT).show();                    }                });                  // show the alert dialog                AlertDialog alertDialog = builder.create();                alertDialog.show();                  // get the top most window of the android                   // scree using getWindow() method                // and set the the gravity of the window to                   // top that is alert dialog will be now at                // the bottom position                alertDialog.getWindow().setGravity(Gravity.BOTTOM);            }        });    }}
android
Technical Scripter 2020
Android
Java
Technical Scripter
Java
Android
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Resource Raw Folder in Android Studio
Flutter - Custom Bottom Navigation Bar
How to Read Data from SQLite Database in Android?
How to Post Data to API using Retrofit in Android?
Retrofit with Kotlin Coroutine in Android
Arrays in Java
Split() String method in Java with examples
For-each loop in Java
Object Oriented Programming (OOPs) Concept in Java
Arrays.sort() in Java with examples