How to Change the Position of AlertDialog in Android?
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.
Steps to implement the alert dialog with different position
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.
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.
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.