Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Center content of TextView vertically & horizontally - LinearLayout

I am trying to center the context of a TextView vertically and horizontally. Please see the below screenshot.

enter image description here

The TextViews that say 1-5 and 100+ Points are the ones I want centered vertically and horizontally. As you can see, they are center horizontally but not vertically. Below is the code I am using.

    <LinearLayout
        android:id="@+id/linearlayout2"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical" >

        <LinearLayout
            android:layout_width="fill_parent"   
            android:layout_height="wrap_content"
            android:orientation="horizontal" >

            <TextView 
                android:id="@+id/l1"
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:layout_weight="2"
                android:paddingTop="5dp"
                android:paddingLeft="5dp"
                android:paddingRight="5dp"
                android:paddingBottom="5dp"
                android:textSize="12sp"
                android:gravity="center_vertical|center_horizontal" /> 

            <TextView 
                android:id="@+id/g1"
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:layout_weight="4"
                android:paddingTop="5dp"
                android:paddingLeft="5dp"
                android:paddingRight="5dp"
                android:paddingBottom="5dp"
                android:textSize="12sp"
                android:gravity="center_vertical|center_horizontal" /> 

            <TextView 
                android:id="@+id/c1"
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:layout_weight="4"
                android:paddingTop="5dp"
                android:paddingLeft="5dp"
                android:paddingRight="5dp"
                android:paddingBottom="5dp"
                android:textSize="12sp"
                android:gravity="center_vertical|center_horizontal" />
        </LinearLayout>

Again, this question is how to center the content of the TextViews, not how to center the TextViews in the LinearLayout. So, how do I center the content both ways in the TextViews?

like image 672
Matt Avatar asked Nov 18 '13 19:11

Matt


People also ask

How do you center TextView vertically?

android:gravity="center" for text center in TextView. android:gravity="center_horizontal" inner text if you want horizontally centered. android:gravity="center_vertical" inner text if you want vertically centered. android:layout_centerInParent="true" if you want TextView in center position of parent view.

How do I center a TextView layout?

Instead, to center the TextView set android:gravity="center" for the RelativeLayout itself. Because the android:gravity attribute does not start with layout_ it refers to the RelativeLayout contents, in this case the TextView.

How do I make text vertical on android?

Implement vertical TextView by calling setRotation() We can call the method of TextView object to make it display in vertical. Java code, call setRotation() to display the TextView (verticalTextView) in vertical. remark: Have to modify AndroidManifest. xml to set android:minSdkVersion="11" or higher.


2 Answers

This is your problem:

android:layout_height="wrap_content"

Your TextView's are set to wrap_content, which means there is no space to be aligned vertically. To fix this, set a fixed row-height. In my example below, I used 50dp; however, it can be set to anything you wish.

P.S - android:gravity="center" handles both center_vertical and center_horizontal.

Throw this code in and check it out!

<LinearLayout
    android:id="@+id/linearlayout2"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="vertical" >

    <LinearLayout
        android:layout_width="fill_parent"   
        android:layout_height="50dp"
        android:orientation="horizontal" >

        <TextView 
            android:id="@+id/l1"
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_weight="2"
            android:paddingTop="5dp"
            android:paddingLeft="5dp"
            android:paddingRight="5dp"
            android:paddingBottom="5dp"
            android:textSize="12sp"
            android:gravity="center" /> 

        <TextView 
            android:id="@+id/g1"
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_weight="4"
            android:paddingTop="5dp"
            android:paddingLeft="5dp"
            android:paddingRight="5dp"
            android:paddingBottom="5dp"
            android:textSize="12sp"
            android:gravity="center" /> 

        <TextView 
            android:id="@+id/c1"
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_weight="4"
            android:paddingTop="5dp"
            android:paddingLeft="5dp"
            android:paddingRight="5dp"
            android:paddingBottom="5dp"
            android:textSize="12sp"
            android:gravity="center" />
    </LinearLayout>
like image 102
Evan Bashir Avatar answered Oct 14 '22 04:10

Evan Bashir


It looks like android is centering the baseline of the text, not the text istself.

Here's a nice blog post about this annoying problem: http://www.doubleencore.com/2013/10/shifty-baseline-alignment/

In summay, linear layout will attempt to align child views by their text baseline, if possible. There is an xml flag to disable this behavior: android:baselineAligned, which you can try setting to false.

like image 34
Collin Flynn Avatar answered Oct 14 '22 03:10

Collin Flynn