Reputation: 33579
I've looked through numerous similar questions here at SO, but nothing helps.
I have a hierarchy of different nested layouts, all have android:layout_width="fill_parent"
, and the inner-most layout has android:layout_width="wrap_content
- I need to align it at the center (horizontally). How do I do that?
Update:: I've found the cause of the problem - if I put the inner LinearLayout into RelativeLayout with android:layout_width="fill_parent"
, it still wraps it's content. The TableRow, however, is really filling the screen.
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<FrameLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content" >
<TableLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content" >
<TableRow >
<LinearLayout
android:orientation="horizontal"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<TextView
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:text="1"
android:textAppearance="?android:attr/textAppearanceLarge" />
<TextView
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:text="2"
android:textAppearance="?android:attr/textAppearanceLarge" />
</LinearLayout>
</TableRow>
</TableLayout>
</FrameLayout>
</LinearLayout>
Upvotes: 90
Views: 187695
Reputation: 171
If your parent is a LinearLayout, you can add to it :
android:orientation="vertical"
android:gravity="center"
And to it's child
android:gravity="center"
It should at least center horizontally !
Upvotes: 5
Reputation: 346
I experienced this while working with nested RelativeLayouts. My solution ended up being simple, but it's a little gotcha that's worth being aware of.
My layout was defined with...
android:layout_height="fill_parent"
android:layout_below="@id/someLayout
android:gravity="center"
...however, I didn't think that the layout below this one was placed on top of it. Everything inside this layout was shifted towards the bottom instead of centered. Adding...
android:layout_above="@id/bottomLayout"
...fixed my issue.
Upvotes: 1
Reputation: 1
Below is code to put your Linearlayout at bottom and put its content at center. You have to use RelativeLayout to set Layout at bottom.
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="50dp"
android:background="#97611F"
android:gravity="center_horizontal"
android:layout_alignParentBottom="true"
android:orientation="horizontal">
<TextView
android:layout_width="wrap_content"
android:layout_height="50dp"
android:text="View Saved Searches"
android:textSize="15dp"
android:textColor="#fff"
android:gravity="center_vertical"
android:visibility="visible" />
<TextView
android:layout_width="30dp"
android:layout_height="24dp"
android:text="12"
android:textSize="12dp"
android:textColor="#fff"
android:gravity="center_horizontal"
android:padding="1dp"
android:visibility="visible" />
</LinearLayout>
</RelativeLayout>
Upvotes: 0
Reputation: 1940
this worked for me.
<LinearLayout>
.
.
.
android:gravity="center"
.
.>
<TextView
android:layout_gravity = "center"
/>
<Button
android:layout_gravity="center"
/>
</LinearLayout>
so you're designing the Linear Layout to place all its contents(TextView and Button) in its center and then the TextView and Button are placed relative to the center of the Linear Layout
Upvotes: 3
Reputation: 63
The problem is that the root's (the main layout you store the other elements) gravity is not set. If you change it to center the other elements' gravity must work just fine.
Upvotes: 0
Reputation: 6153
for ImageView or others views who layout-gravity or gravity does not exist use :
android:layout_centerInParent="true"
in the child
(I had a relative layout with an ImageView as a child).
Upvotes: 0
Reputation: 41
This worked for me.. adding empty view ..
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:orientation="horizontal"
>
<View
android:layout_width="0dp"
android:layout_height="0dp"
android:layout_weight="1"
/>
<com.google.android.gms.ads.AdView
android:id="@+id/adView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
ads:adSize="BANNER"
ads:adUnitId="@string/banner_ad_unit_id" >
</com.google.android.gms.ads.AdView>
<View
android:layout_width="0dp"
android:layout_height="0dp"
android:layout_weight="1"
/>
</LinearLayout>
Upvotes: 4
Reputation: 1669
Please try this in your linear layout
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
Upvotes: 14
Reputation: 27727
These two attributes are commonly confused:
android:gravity
sets the gravity of the content of the View it's
used on. android:layout_gravity
sets the gravity of the View or
Layout relative to its parent.So either put android:gravity="center"
on the parent or android:layout_gravity="center"
on the LinearLayout itself.
I have caught myself a number of times mixing them up and wondering why things weren't centering properly...
Upvotes: 245
Reputation: 2539
I have faced the same situation while designing custom notification. I have tried with the following attribute set with true.
android:layout_centerInParent.
Upvotes: 2
Reputation: 5900
Try <TableRow android:gravity="center_horizontal">
This will center the inner LinearLayout within the tablerow.
Upvotes: 2
Reputation: 19250
Try this:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical"
>
<FrameLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content" >
<TableLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content" >
<TableRow >
<LinearLayout
android:orientation="horizontal"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center_horizontal"
android:layout_gravity="center_horizontal" >
<TextView
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:text="1"
android:textAppearance="?android:attr/textAppearanceLarge" />
<TextView
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:text="2"
android:textAppearance="?android:attr/textAppearanceLarge" />
</LinearLayout>
</TableRow>
</TableLayout>
</FrameLayout>
</LinearLayout>
Upvotes: 3
Reputation: 29121
<RelativeLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
>
<LinearLayout
android:orientation="horizontal"
android:layout_centerHorizontal="true"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<TextView
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:text="1"
android:textAppearance="?android:attr/textAppearanceLarge" />
<TextView
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:text="2"
android:textAppearance="?android:attr/textAppearanceLarge" />
</LinearLayout>
</RelativeLayout>
consider wrapping relativeLayout over LinearLayout. android:layout_centerHorizontal="true"
will position the view center horizontal.
Upvotes: 11
Reputation: 1523
@jksschneider explation is almost right. Make sure that you haven't set any gravity
to parent layout, and then set layout_gravity="center"
to your view or layout.
Upvotes: 1
Reputation: 815
add layout_gravity="center"
or "center_horizontal"
to the parent layout.
On a side note, your LinearLayout
inside your TableRow
seems un-necessary, as a TableRow
is already an horizontal LinearLayout
.
Upvotes: 5