NioX5199
NioX5199

Reputation: 1802

Custom Alert dialog not centered vertically on Android

I made a custom alert dialog box to be displayed at the end of my game so that the player can enter its name to save it. The problem is when I call show() on the dialog appears but it's not vertically centered! It's a bit lower than it should and no matter what properties I set in the xml or when using setGravity().

I think this is the same problem as the one mentioned here, but no one gave a proper answer.

Thanks for your help.

For more details, here is my code:

    AlertDialog.Builder builder;

    LayoutInflater inflater = (LayoutInflater)this.getSystemService(LAYOUT_INFLATER_SERVICE);
    View layout = inflater.inflate(R.layout.newrecord,(ViewGroup)findViewById(R.layout.shoot));

    builder = new AlertDialog.Builder(this);
    builder.setView(layout);

    newRecDialog = builder.create();

And here is the code of the first element of the XML layout of newrecord.xml:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_height="fill_parent"
android:layout_width="fill_parent"
android:gravity="center"
 android:padding="10dp"
android:baselineAligned="true">

Here is the output screenshot: alt text
(source: free.fr)

Upvotes: 13

Views: 19640

Answers (12)

Carl Koh Wei Hao
Carl Koh Wei Hao

Reputation: 31

Call dialog.requestWindowFeature(Window.FEATURE_NO_TITLE before setting the dialog content. Replace dialog with how you declared it. This solution works for me. There's no need to set the Gravity.

Upvotes: 0

Reejesh PK
Reejesh PK

Reputation: 4130

If you are dealing with any height and width attributes, you must make sure not to alter the height, since it will alter the position, here is a sample.

     myProgressDialog.show();           
            float widthPecent = 0.60f;
            //order matters
            DisplayMetrics displayMetrics = new DisplayMetrics();
            getWindowManager().getDefaultDisplay().getMetrics(displayMetrics);
            int displayWidth = displayMetrics.widthPixels;

        //dont do any adjustments to the height. **************************  <<<<<<<<<<  

            WindowManager.LayoutParams layoutParams = new WindowManager.LayoutParams();
    
            layoutParams.copyFrom(myProgressDialog.getWindow().getAttributes());
            int dialogWindowWidth = (int) (displayWidth * widthPecent);
            layoutParams.width = dialogWindowWidth;

 //dont do any changes to the height. **************************  <<<<<<<<<<  

            myProgressDialog.getWindow().setAttributes(layoutParams);

layoutParams.height = dialogWindowHeight; //comment or remove this line.

Upvotes: 1

Gomino
Gomino

Reputation: 12347

You need to disable the window title. First create a custom style for your dialog (Make sure to use the parent theme that fits your needs):

<style name="CustomDialog" parent="@android:style/Theme.Dialog">
   <item name="android:windowNoTitle">true</item>
</style>

Then apply your style to the dialog builder:

AlertDialog.Builder builder;

LayoutInflater inflater = (LayoutInflater)this.getSystemService(LAYOUT_INFLATER_SERVICE);
View layout = inflater.inflate(R.layout.newrecord,(ViewGroup)findViewById(R.layout.shoot));

builder = new AlertDialog.Builder( new ContextThemeWrapper(this, R.style.CustomDialog));
builder.setView(layout);

newRecDialog = builder.create();

Upvotes: 0

user2671902
user2671902

Reputation:

android:theme="@android:style/Theme.Dialog"

Upvotes: 1

KrazyMicha
KrazyMicha

Reputation: 31

If you implement your own dialog the line requestWindowFeature(Window.FEATURE_NO_TITLE) hides the title panel and the dialog is centered on the screen. Maybe it works with a AlertDialog too.

Upvotes: 3

Dmitry Zaytsev
Dmitry Zaytsev

Reputation: 23952

Try:

newRecDialog.getWindow().getAttributes().gravity = Gravity.CENTER;

Upvotes: 0

ravi
ravi

Reputation: 187

you should use the following line in the xml firstly remove that padding line from your xml and after that

android:layout_gravity="center"

after this your dialog will appear in center and if you are using the margin from left etc than remove that and also if you are using that

android:layout_width="fill_parent"

than change it with the

android:layout_width="wrap_content"

after that your dialog will be appear in the center.

Upvotes: 0

Jin35
Jin35

Reputation: 8612

Can you try using AlertDialog.Builder.setCustomTitle(View); instead of setView? We use it because alert dialog looks a bit better than dialog with empty title.

Upvotes: 0

Ujjwal Bansal
Ujjwal Bansal

Reputation: 289

You can use an Activity instead of a custom alert for this. You have to set the theme of activity as dialog in the android manifest file:

android:theme="@android:style/Theme.Dialog"

And you can adjust the activity xml layout as per your need.

Upvotes: 2

mhsmith
mhsmith

Reputation: 8091

The bug is described here. The AlertDialog is reserving space for the title/icon panel even where there is neither a title nor an icon.

The fix is, I think, quite simple: it should set the top panel layout to GONE, just as it does for the button panel in the event of there being no buttons. Until that's done, the only workaround is to implement your own Dialog subclass.

Upvotes: 3

Sean
Sean

Reputation: 1278

Not really an answer, but I was having a similar issue. It appears that it is centered, but it is assuming the AlerterDialog has a title set. In my case, I just set a title.

Upvotes: 0

Praveen
Praveen

Reputation: 91175

set the attribute on your AlertDialog as android:gravity="center" or programmatically as setGravity(Gravity.CENTER). This Gravity is for your layout only not for the display of your mobile. if you use Custom Title its did not look like center vertical.

Upvotes: 0

Related Questions