How to Change Alert Dialog Position on Screen in Android?
Last Updated :
13 Jan, 2022
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 AlertDialog appears in the center of the screen whenever called. Below is a screenshot of an AlertDialog.
Through this article, we will show you how you could change the position of the AlertDialog on the screen. Follow the below steps once the IDE is ready.
Step by Step Implementation
Step 1: Create a New Project in Android Studio
To create a new project in Android Studio please refer to How to Create/Start a New Project in Android Studio. We demonstrated the application in Kotlin, so make sure you select Kotlin as the primary language while creating a New Project.
Step 2: Working with the activity_main.xml file
In our demonstration, upon button click, the AlertDialog shall be displayed. So we added a Button in our layout file.
XML
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
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"
tools:context=".MainActivity">
<!-- AlertDialog is displayed when
this button is clicked -->
<Button
android:id="@+id/button_1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:text="show dialog"/>
</RelativeLayout>
Step 3: Working with the MainActivity.kt file
Go to the MainActivity.kt file and refer to the following code. Below is the code for the MainActivity.kt file. Comments are added inside the code to understand the code in more detail.
Kotlin
package org.geeksforgeeks.dialogpositionchange
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.view.Gravity
import android.widget.Button
import androidx.appcompat.app.AlertDialog
class MainActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
// Declaring and initializing a constant for
// Button from the layout file (activity_main.xml)
val mButton1 = findViewById<Button>(R.id.button_1)
// Declaring and initializing a
// constant for AlertDialog Builder
val mBuilder = AlertDialog.Builder(this)
// Attributing the AlertDialog Builder
mBuilder.setTitle("TITLE")
.setMessage("MESSAGE")
.setPositiveButton("Positive"){_,_->}
.setNegativeButton("Negative"){_,_->}
// On Button click, AlertDialog is created and displayed.
// Gravity of the AlertDialog is set to TOP to display
// AlertDialog in the TOP of the screen.
mButton1.setOnClickListener {
val mAlert = mBuilder.create()
mAlert.show()
mAlert.window?.setGravity(Gravity.TOP)
// Similarly we can change the gravity
// to left right and bottom.
}
}
}
Output:
You can see that when the button is clicked, the AlertDialog appears at the top of the screen.
Similar Reads
How to Set Buttons Inside an Alert Dialog in Android? In this article, we are going to see how can we set two buttons inside an Alert Dialog. For example, we can see when an alert dialog is pop up then their two types of buttons available one is the Ok or Positive Button another one is Cancel or Negative Button. Alert dialogs are most frequently used i
4 min read
How to Close Alert Dialog Box in Android Programmatically? AlertDialog is a flash or an alert message with options that let the user proceed or deny any process or action. AlertDialog generally consists of the main title, the message, and two buttons, technically termed as a positive button and a negative button. Both positive and negative buttons can be pr
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 Create an Alert Dialog Box in Android? An Android Alert Dialog is a UI element that displays a warning or notification message and asks the user to respond with options such as Yes or No. Based on the user's response, appropriate actions are executed. Android Alert Dialog is built with the use of three fields: Title, Message area, and Ac
4 min read
How to Make an Alert Dialog Fill Majority of the Screen Size in Android? An Alert Dialog is a window that appears on the screen to give notifications to the user, make some decisions for the user, or enter some information from the user. An Alert Dialog is generally not full screen, but in this article, we'll be implementing something similar. Building an Alert Dialog:Ti
3 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