Skip to content
Related Articles

Related Articles

How to Change the Position of AlertDialog in Android?
  • Last Updated : 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. 

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



filter_none

edit
close

play_arrow

link
brightness_4
code

<?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>

chevron_right


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

filter_none

edit
close

play_arrow

link
brightness_4
code

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);
            }
        });
    }
}

chevron_right


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

filter_none

edit
close

play_arrow

link
brightness_4
code

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);
            }
        });
    }
}

chevron_right


Output: Run on Emulator


Attention reader! Don’t stop learning now. Get hold of all the important Java Foundation and Collections concepts with the Fundamentals of Java and Java Collections Course at a student-friendly price and become industry ready.

My Personal Notes arrow_drop_up
Recommended Articles
Page :