Open In App

How to Change the Position of AlertDialog in Android?

Last Updated : 30 Jun, 2022
Improve
Improve
Like Article
Like
Save
Share
Report

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. 

Change the Position of AlertDialog in Android

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.

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"
    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:

Change the Position of AlertDialog in Android

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 gravity of the window to
                  // top that is alert dialog will be now at
                // the topmost position
                alertDialog.getWindow().setGravity(Gravity.TOP);
            }
        });
    }
}


Output: Run on Emulator

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 gravity of the window to
                  // top that is alert dialog will be now at
                // the bottom position
                alertDialog.getWindow().setGravity(Gravity.BOTTOM);
            }
        });
    }
}


Output: Run on Emulator



Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads