Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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)

like image 887
NioX5199 Avatar asked Jul 19 '10 12:07

NioX5199


2 Answers

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.

like image 183
mhsmith Avatar answered Oct 23 '22 12:10

mhsmith


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.

like image 3
KrazyMicha Avatar answered Oct 23 '22 12:10

KrazyMicha