Android - Center TextView Horizontally in LinearLayout

170,953

Solution 1

What's happening is that since the the TextView is filling the whole width of the inner LinearLayout it is already in the horizontal center of the layout. When you use android:layout_gravity it places the widget, as a whole, in the gravity specified. Instead of placing the whole widget center what you're really trying to do is place the content in the center which can be accomplished with android:gravity="center_horizontal" and the android:layout_gravity attribute can be removed.

Solution 2

If you set <TextView> in center in <Linearlayout> then first put android:layout_width="fill_parent" compulsory
No need of using any other gravity

    <LinearLayout
            android:layout_toRightOf="@+id/linear_profile" 
            android:layout_height="wrap_content"
            android:layout_width="fill_parent"
            android:orientation="vertical"
            android:gravity="center_horizontal">
            <TextView 
                android:layout_height="wrap_content"
                android:layout_width="wrap_content"
                android:text="It's.hhhhhhhh...."
                android:textColor="@color/Black"

                />
    </LinearLayout>

Solution 3

<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent">

<LinearLayout
android:orientation="horizontal"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:background="@drawable/title_bar_background">

<TextView
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:gravity="center"
    android:textAppearance="?android:attr/textAppearanceLarge"
    android:padding="10dp"
    android:text="HELLO WORLD" />

</LinearLayout>

Solution 4

Use android:gravity="center" in TextView instead of layout_gravity.

Solution 5

Just use: android:layout_centerHorizontal="true"

It will put the whole textview in the center

Share:
170,953

Related videos on Youtube

Jake Wilson
Author by

Jake Wilson

Experienced in developing tools for 3D animation, motion capture, video game and movie production, web development, Android development, responsive design, etc...

Updated on July 08, 2022

Comments

  • Jake Wilson
    Jake Wilson almost 2 years

    I have the following basic layout

    <LinearLayout
        xmlns:android="http://schemas.android.com/apk/res/android"
        android:orientation="vertical"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent">
    
    <LinearLayout
        android:orientation="horizontal"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:background="@drawable/title_bar_background">
    
        <TextView
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:layout_gravity="center_horizontal"
            android:textAppearance="?android:attr/textAppearanceLarge"
            android:padding="10dp"
            android:text="HELLO WORLD" />
    
        </LinearLayout>
    <LinearLayout>
    

    It seems like the xml is correct but the text is aligned to the left. The textview takes up the entire width of the parent and the textview is set to be centered. Not sure what the problem is...

    • user370305
      user370305 over 12 years
      try android:gravity="center" for your textview
  • Paul Brewczynski
    Paul Brewczynski over 10 years
    If I understand correctly he could change "android:layout_width="fill_parent" to "wrap_content" and then use android:layout_gravity="center_horizontal". Am I right ?
  • Dan S
    Dan S over 10 years
    @bluesm No, the inner LinearLayout doesn't allow itself to have space that is not filled with a View (not considering the case of an empty LinearLayout). Thus the the android:layout_width will have the same value (after layout). Since the width of the TextView is equal to the with of the inner LinearLayout the TextView effectively has the android:layout_gravity values of left, right, and center at the same time.
  • nurnachman
    nurnachman almost 9 years
    Short and precise answer. Thank you
  • Atul
    Atul almost 8 years
    For ImageView its layout_gravityand for TextView its gravity is what only works. Android is great ! Thanks to all beautiful SO posts without which development was just impossible.
  • Choletski
    Choletski over 7 years
    don't forget about android:layout_width="match_parent"