How to align LinearLayout at the center of its parent?

AndroidAndroid LayoutAndroid Ui

Android Problem Overview


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>

Android Solutions


Solution 1 - Android

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...

Solution 2 - Android

Please try this in your linear layout

android:layout_centerHorizontal="true"
android:layout_centerVertical="true"

Solution 3 - Android

<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.

Solution 4 - Android

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.

Solution 5 - Android

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>

Solution 6 - Android

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 !

Solution 7 - Android

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>

Solution 8 - Android

Try <TableRow android:gravity="center_horizontal"> This will center the inner LinearLayout within the tablerow.

Solution 9 - Android

I have faced the same situation while designing custom notification. I have tried with the following attribute set with true.

 android:layout_centerInParent.

Solution 10 - Android

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

Solution 11 - Android

@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.

Solution 12 - Android

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.

Solution 13 - Android

Try this:

android:layout_gravity="center_horizontal"

Solution 14 - Android

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).

Solution 15 - Android

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.

Solution 16 - Android

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>

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionViolet GiraffeView Question on Stackoverflow
Solution 1 - AndroidJonathan SchneiderView Answer on Stackoverflow
Solution 2 - AndroidLakshmananView Answer on Stackoverflow
Solution 3 - AndroidYashwanth KumarView Answer on Stackoverflow
Solution 4 - AndroidEmmanuel SysView Answer on Stackoverflow
Solution 5 - Androidgitesh devhaneView Answer on Stackoverflow
Solution 6 - AndroidLalView Answer on Stackoverflow
Solution 7 - AndroidHiral VadodariaView Answer on Stackoverflow
Solution 8 - AndroidtriggsView Answer on Stackoverflow
Solution 9 - AndroidKarthikeyan VeView Answer on Stackoverflow
Solution 10 - AndroidpcodexView Answer on Stackoverflow
Solution 11 - AndroidPawanView Answer on Stackoverflow
Solution 12 - AndroidCrash5998View Answer on Stackoverflow
Solution 13 - AndroidtheAshutoshAJView Answer on Stackoverflow
Solution 14 - Androidbastami82View Answer on Stackoverflow
Solution 15 - AndroidMarin KaradzhovView Answer on Stackoverflow
Solution 16 - AndroidKalpesh PatilView Answer on Stackoverflow