How to center layout inside of Android Dialog?
Asked Answered
N

4

12

I am trying to create a custom Dialog, and have its content centered, but it is always ending up left-aligned. Here is aboutdialog.xml:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
  xmlns:a="http://schemas.android.com/apk/res/android"
  a:orientation="vertical"
  a:id="@+id/AboutDialogLayout" 
  a:layout_width="fill_parent" 
  a:layout_height="fill_parent" 
  a:layout_gravity="center_horizontal" 
  a:gravity="center_horizontal">

    <ImageView a:id="@+id/imageView1" 
               a:src="@drawable/pricebook" 
               a:layout_height="wrap_content" 
               a:layout_width="wrap_content"/>
    <TextView a:layout_height="wrap_content"         
              a:textAppearance="?android:attr/textAppearanceLarge" 
              a:id="@+id/textView1" 
              a:text="Price Book" 
              a:layout_width="wrap_content"/>
    <TextView a:layout_height="wrap_content" 
              a:id="@+id/textView2" 
              a:layout_width="wrap_content" 
              a:text="foo"/>
</LinearLayout>

And my (relevant) code:

Dialog d = new Dialog(this);
d.setContentView(R.layout.aboutdialog);
d.setTitle(R.string.app_name);

What am I missing here? Thanks for the assistance.

Image: Screen shot of Dialog problem

Negligible answered 9/9, 2011 at 18:3 Comment(8)
That's weird. It works fine for me. Can you post a screenshot?Agrobiology
+1 for using a: instead of android: everywhere, how did you do that?Togo
Yes, I too check it its working fine.Togo
@suri: Thats just the namespace prefixMiriam
@suri: Just do a find/replace in the XML for android/a. Eclipse GUI tools will honor that if changes are made after that.Negligible
@Breakpoint means I can't get it.Togo
One other data point: This code does work for me on my Android 2.1 (v7) emulator. The problem is on my Droid X, running 2.3.3.Negligible
holy balls I didn't know you could use a:Hooks
F
11

try to edit to this: a:layout_gravity="center_horizontal|center_vertical"
OR
use Relative_Layout and align linear layout to center of parent with fill_parent for Relative wrap_content for Linear.

although it is worked with me on center

Folkways answered 9/9, 2011 at 18:28 Comment(4)
Wrapping the whole XML in a RelativeLayout did it. The layout_widths for both *Layouts needs to be fill_parent and both layout_heights need to be set to wrap_content (at least that's how I want it).Negligible
But the problem is child ViewGroup in RelativeLayout must have layout_width=fill_parent to make it work and sometime's you don't want that behavior for your content. See my solution below ;)Rois
none of this worked out for me ... any other solution?Acatalectic
I tried LinearLayout + wrap_content (both gravities were center) but it didn't work, so I think RelativeLayout is indeed the key: Use match_parent (replaced fill_parent) for both the layout_width and the layout_height and set layout_gravity and gravity to center.Heterotaxis
R
2

Modify Dialog's ViewGroup (FrameLayout) which holds your content layout. I use static method and invoke it inside onActivityCreated() in DialogFragments:

/**
 * Center dialog content inside available space
 * @param dialog
 */
public static void centerDialogContent(Dialog dialog) {
ViewGroup decorView = (ViewGroup) dialog.getWindow().getDecorView();
View content = decorView.getChildAt(0);
FrameLayout.LayoutParams contentParams = (FrameLayout.LayoutParams)content.getLayoutParams();
contentParams.gravity = Gravity.CENTER;
content.setLayoutParams(contentParams);
}
Rois answered 29/6, 2012 at 7:13 Comment(0)
S
0

You're doing layout_gravity which is good, but since you do it to the outmost layout, it can't position itself relative to what's around it.

I think that wrapping the above layout inside another layout would do it

Sanctity answered 9/9, 2011 at 18:17 Comment(1)
Wrapping the above XML in another LinearLayout doesn't make a difference.Negligible
R
0

Can you try changing your LinearLayout layout_gravity to just gravity? See the difference explained here: Gravity and layout_gravity on Android

Repulse answered 9/9, 2011 at 18:33 Comment(1)
How about setting a:layout_gravity="center" on each view inside your LinearLayout?Repulse

© 2022 - 2024 — McMap. All rights reserved.