In this small tutorial, we’ll cover how to change position of an Alert Dialog on Android, and I’ll also be sharing a bit of what I do to style the Alert Dialog’s on Snap Search.


When developing Android applications, user experience is crucial. One aspect of providing a seamless experience is customizing the Alert Dialog to fit your app’s design and functionality. In this step-by-step guide, we will discuss how to change the position of an Alert Dialog on Android, giving you more control over your app’s appearance and improving user interaction.

What is an Alert Dialog?

An Alert Dialog is a small, focused window that appears in front of an app’s UI, providing users with information or choices. It is typically used to prompt users for decisions, confirmations, or to provide essential details. By default, an Alert Dialog appears in the center of the screen, but sometimes you may want to change its position to better suit your app’s design.

Why Change the Position of Alert Dialog?

  1. Aesthetic Appeal: Customizing the position of Alert Dialogs can enhance your app’s visual appeal, setting it apart from the competition.
  2. User Experience: A well-placed Alert Dialog can improve user experience by making it easier to interact with and reducing distractions.
  3. Accessibility: Adjusting the position of Alert Dialogs can improve accessibility for users with different needs or preferences.

Step 1: Create a new Android project

To begin, open Android Studio and create a new project with an empty activity. Make sure you choose “Java” as the programming language.

Step 2: Modify activity_main.xml

Open the “activity_main.xml” file and add a button that will trigger the custom Alert Dialog. Replace the existing code with the following:

<RelativeLayout xmlns:android=""

        android:text="Show Dialog"
        android:layout_centerInParent="true" />


Step 3: Modify

Open “” and add the following code to create a custom Alert Dialog with a new position:

import android.os.Bundle;
import android.view.Gravity;
import android.view.View;
import android.view.WindowManager;
import android.widget.Button;

public class MainActivity extends AppCompatActivity {

    protected void onCreate(Bundle savedInstanceState) {

        Button buttonShowDialog = findViewById(;
        buttonShowDialog.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {
                AlertDialog.Builder builder = new AlertDialog.Builder(MainActivity.this);
                builder.setMessage("This is a custom positioned Alert Dialog");

                AlertDialog alertDialog = builder.create();

                WindowManager.LayoutParams layoutParams = new WindowManager.LayoutParams();
                layoutParams.gravity = Gravity.TOP | Gravity.LEFT;
                layoutParams.x = 100; // Horizontal offset
                layoutParams.y = 200; // Vertical offset


Step 4: Testing the custom Alert Dialog

Now, run the app on an emulator or an Android device. When you click the “Show Dialog” button, you should see the custom Alert Dialog appear in the new position specified by the horizontal and vertical offset values.

Step 5: Further customization

Feel free to experiment with different values for the horizontal and vertical offsets to position the Alert Dialog as desired. You can also modify the Gravity settings, such as using Gravity.BOTTOM or Gravity.RIGHT, to place the Alert Dialog in different locations on the screen.


In this step-by-step guide, we showed you how to change the position of an Alert Dialog on Android. This customization can significantly enhance your app’s user experience, making it more visually appealing and accessible. With just a few lines of code, you can modify the Alert Dialog’s position to fit your app’s design and functionality perfectly. Keep experimenting with different positions and Gravity settings to create a unique and engaging app that stands out from the competition.

Check out this Android tutorial on adding gradient overlays using pure xml.

Leave a Reply

Your email address will not be published. Required fields are marked *