how to align text vertically center in android

112,455

Solution 1

Your TextView Attributes need to be something like,

<TextView ... 
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:gravity="center_vertical|right" ../>

Now, Description why these need to be done,

 android:layout_width="match_parent"
 android:layout_height="match_parent"

Makes your TextView to match_parent or fill_parent if You don't want to be it like, match_parent you have to give some specified values to layout_height so it get space for vertical center gravity. android:layout_width="match_parent" necessary because it align your TextView in Right side so you can recognize respect to Parent Layout of TextView.

Now, its about android:gravity which makes the content of Your TextView alignment. android:layout_gravity makes alignment of TextView respected to its Parent Layout.

Update:

As below comment says use fill_parent instead of match_parent. (Problem in some device.)

Solution 2

The problem is the padding of the font on the textview. Just add to your textview:

android:includeFontPadding="false"

Solution 3

just use like this to make anything to center

 android:layout_gravity="center"
 android:gravity="center"

updated :

android:layout_gravity="center|right"
android:gravity="center|right"

Updated : Just remove MarginBottom from your textview.. Do like this.. for your textView

<LinearLayout
        android:id="@+id/linearLayout5"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_gravity="center"  >

        <TextView
            android:id="@+id/textView2"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:layout_gravity="center" 
            android:gravity="center|right"
            android:text="hello" 
            android:textSize="20dp" />
    </LinearLayout>

Solution 4

Try to put android:gravity="center_vertical|right" inside parent LinearLayout else as you are inside RelativeLayout you can put android:layout_centerInParent="true" inside your scrollView.

Share:
112,455
newday
Author by

newday

merge keep

Updated on July 11, 2022

Comments

  • newday
    newday almost 2 years

    I have arabic text, therefore I set gravity to right in order to start text from right side. Text starts from right now. But another issue is text starts to render from the top of the page. But I need to vertically center the text. Although I tried several variations I couldnt make it vertically center.

    Here is the sample of my xml file.

    <LinearLayout
                android:id="@+id/linearLayout5"
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"
                android:gravity="right"
                android:orientation="vertical" >
    
                <TextView
                    android:id="@+id/textView2"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_gravity="center_vertical"
                    android:layout_marginBottom="23dp"
                    android:gravity="right"
                    android:padding="@dimen/padding_maintextview"
                    android:text="@string/text"
                    android:textAppearance="?android:attr/textAppearanceMedium"
                    android:textSize="20sp" />
            </LinearLayout>
    

    Problem is with above textview.

    Here I have put whole xml file.

    <?xml version="1.0" encoding="utf-8"?>
    <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:background="@drawable/page1background"
        android:paddingRight="@dimen/padding_large" >
    
        <TextView
            android:id="@+id/textView1"
            android:layout_width="196dp"
            android:layout_height="wrap_content"
            android:layout_centerHorizontal="true"
            android:gravity="center_horizontal"
            android:paddingTop="@dimen/padding_Title_Top"
            android:text="@string/text"
            android:textAppearance="?android:attr/textAppearanceMedium"
            android:textSize="20sp" />
    
        <LinearLayout
            android:id="@+id/linearLayout1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_below="@id/textView1"
            android:gravity="center_horizontal"
            android:orientation="vertical" >
    
            <View
                android:id="@+id/view1"
                android:layout_width="fill_parent"
                android:layout_height="5dp" />
        </LinearLayout>
    
        <ScrollView
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:layout_above="@id/linearLayout2"
            android:layout_below="@id/linearLayout1"
            android:layout_gravity="center"
            android:padding="@dimen/padding_maintextview" >
    
            <LinearLayout
                android:id="@+id/linearLayout5"
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"
                android:gravity="right"
                android:orientation="vertical" >
    
                <TextView
                    android:id="@+id/textView2"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_gravity="center_vertical"
                    android:layout_marginBottom="23dp"
                    android:gravity="right"
                    android:padding="@dimen/padding_maintextview"
                    android:text="@string/text"
                    android:textAppearance="?android:attr/textAppearanceMedium"
                    android:textSize="20sp" />
            </LinearLayout>
        </ScrollView>
    
        <LinearLayout
            android:id="@+id/linearLayout2"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentBottom="true"
            android:layout_centerHorizontal="true" >
    
            <View
                android:id="@+id/view2"
                android:layout_width="fill_parent"
                android:layout_height="100dp" />
        </LinearLayout>
    
        <LinearLayout
            android:id="@+id/linearLayout3"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentBottom="true"
            android:layout_centerHorizontal="true" >
    
            <ImageButton
                android:id="@+id/back_arrow"
                android:layout_width="0dip"
                android:layout_height="wrap_content"
                android:layout_marginBottom="30dp"
                android:layout_marginRight="45dp"
                android:layout_weight=".5"
                android:background="@drawable/backbut"
                android:contentDescription="@string/Description"
                android:onClick="onClickBtn"
                android:src="@drawable/backarrowpress" />
    
            <ImageButton
                android:id="@+id/copyButton"
                android:layout_width="0dip"
                android:layout_height="wrap_content"
                android:layout_marginLeft="45dp"
                android:layout_weight=".5"
                android:background="@drawable/copy"
                android:contentDescription="@string/Description"
                android:onClick="onClickBtn" />
        </LinearLayout>
    
    </RelativeLayout>
    

    Can anybody show me where I have done the mistake? I think problem is clear. If not tell me in comments.

    Herewith I have appended updated code after review your answers.

    <?xml version="1.0" encoding="utf-8"?>
    <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:background="@drawable/page1background"
        android:paddingRight="@dimen/padding_large" >
    
        <TextView
            android:id="@+id/textView1"
            android:layout_width="196dp"
            android:layout_height="wrap_content"
            android:layout_centerHorizontal="true"
            android:gravity="center_horizontal"
            android:paddingTop="@dimen/padding_Title_Top"
            android:text="@string/text"
            android:textAppearance="?android:attr/textAppearanceMedium"
            android:textSize="20sp" />
    
        <LinearLayout
            android:id="@+id/linearLayout1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_below="@id/textView1"
            android:gravity="center_horizontal"
            android:orientation="vertical" >
    
            <View
                android:id="@+id/view1"
                android:layout_width="fill_parent"
                android:layout_height="5dp" />
        </LinearLayout>
    
        <ScrollView
            android:layout_width="fill_parent"
            android:layout_height="fill_parent"
            android:layout_above="@id/linearLayout2"
            android:layout_below="@id/linearLayout1"
            android:layout_gravity="center"
            android:layout_centerInParent="true"
            android:padding="@dimen/padding_maintextview" >
    
            <LinearLayout
                android:id="@+id/linearLayout5"
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"
                android:gravity="right"
                android:orientation="vertical" >
    
                <TextView
                    android:id="@+id/textView2"
                    android:layout_width="fill_parent"
                    android:layout_height="fill_parent"
                    android:layout_gravity="center_vertical"
                    android:layout_marginBottom="23dp"
                    android:gravity="center_vertical|right"
                    android:padding="@dimen/padding_maintextview"
                    android:text="@string/text"
                    android:textAppearance="?android:attr/textAppearanceMedium"
                    android:textSize="20sp" />
            </LinearLayout>
        </ScrollView>
    
        <LinearLayout
            android:id="@+id/linearLayout2"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentBottom="true"
            android:layout_centerHorizontal="true" >
    
            <View
                android:id="@+id/view2"
                android:layout_width="fill_parent"
                android:layout_height="100dp" />
        </LinearLayout>
    
        <LinearLayout
            android:id="@+id/linearLayout3"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentBottom="true"
            android:layout_centerHorizontal="true" >
    
            <ImageButton
                android:id="@+id/back_arrow"
                android:layout_width="0dip"
                android:layout_height="wrap_content"
                android:layout_marginBottom="30dp"
                android:layout_marginRight="45dp"
                android:layout_weight=".5"
                android:background="@drawable/backbut"
                android:contentDescription="@string/Description"
                android:onClick="onClickBtn"
                android:src="@drawable/backarrowpress" />
    
            <ImageButton
                android:id="@+id/copyButton"
                android:layout_width="0dip"
                android:layout_height="wrap_content"
                android:layout_marginLeft="45dp"
                android:layout_weight=".5"
                android:background="@drawable/copy"
                android:contentDescription="@string/Description"
                android:onClick="onClickBtn" />
        </LinearLayout>
    
    </RelativeLayout>
    

    But I am in same situation. No text is vertically centered

  • Devu Soman
    Devu Soman over 11 years
    Please avoid use of 'match_parent'.Use fill_parent or wrap_content instead.Because match _parent did not support in some devices.
  • newday
    newday over 11 years
    I tried your answer with match_parent and fill_parent too since my app supports older versions but no luck. I still face the same issue. Text is still shows on top.
  • user370305
    user370305 over 11 years
    Just for debug and test purpose can you write hard coded values at your LinearLayout android:layout_height="200dp" parent of TextView.
  • user370305
    user370305 over 11 years
    Both Parent and Child View has a Same android:layout_height="wrap_content". So the change doesn't reflected. (User can't recognize as gravity is set to center_vertical).
  • Rahul Baradia
    Rahul Baradia over 11 years
    have you tried making changes to your LinearLayout. Try that It ll work. updating the answer
  • Rahul Baradia
    Rahul Baradia over 11 years
    make both changes to textView and LinearLayout
  • newday
    newday over 11 years
    i did but there is still same issue.
  • Rahul Baradia
    Rahul Baradia over 11 years
    got it remove marginBottom from your textView. It ll work.. Updating the answer
  • newday
    newday over 11 years
    can brief why you ask me to remove margin bottom. I have set it to a purpose. but I can't see clear reason to remove it.. :(
  • Rahul Baradia
    Rahul Baradia over 11 years
    if you remove margin bottom then you can see textview in center.. or put margin top also to see your textview center
  • foxanna
    foxanna almost 8 years
    Having a TextView with android:layout_height="24dp" and android:textSize="20dp" and android:gravity="center_vertical" I observed that text was still aligned bottom. android:includeFontPadding="false" fixed my problem!
  • hardanger
    hardanger over 6 years
    Often better than android:lines="3" is android:maxLines="3", providing a more flexible layout whilst still fixing the 'bug' in question.