Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

like image 560
newday Avatar asked Jan 01 '13 06:01

newday


People also ask

How do you center vertically on android?

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

How can I center my text in android?

To align text in TextView at the center vertically and horizontally we need to make use of gravity attribute. Example 1 : You can use center_vertical | center_horizontal value to set the text at the center of the TextView area.


1 Answers

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

like image 131
user370305 Avatar answered Sep 23 '22 09:09

user370305