[android] how to align text vertically center in android

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

This question is related to android android-layout text

The answer is


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

android:includeFontPadding="false"

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>

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.


In relative layout you need specify textview height:

android:layout_height="100dp"

Or specify lines attribute:

android:lines="3"

Examples related to android

Under what circumstances can I call findViewById with an Options Menu / Action Bar item? How to implement a simple scenario the OO way My eclipse won't open, i download the bundle pack it keeps saying error log getting " (1) no such column: _id10 " error java doesn't run if structure inside of onclick listener Cannot retrieve string(s) from preferences (settings) strange error in my Animation Drawable how to put image in a bundle and pass it to another activity FragmentActivity to Fragment A failure occurred while executing com.android.build.gradle.internal.tasks

Examples related to android-layout

How to check if a key exists in Json Object and get its value How to center the elements in ConstraintLayout Android - how to make a scrollable constraintlayout? Add ripple effect to my button with button background color? This view is not constrained vertically. At runtime it will jump to the left unless you add a vertical constraint Is it possible to put a ConstraintLayout inside a ScrollView? Differences between ConstraintLayout and RelativeLayout How to remove title bar from the android activity? How to have EditText with border in Android Lollipop Android: ScrollView vs NestedScrollView

Examples related to text

Difference between opening a file in binary vs text How do I center text vertically and horizontally in Flutter? How to `wget` a list of URLs in a text file? Convert txt to csv python script Reading local text file into a JavaScript array Python: How to increase/reduce the fontsize of x and y tick labels? How can I insert a line break into a <Text> component in React Native? How to split large text file in windows? Copy text from nano editor to shell Atom menu is missing. How do I re-enable