user2663613
user2663613

Reputation: 52

How to set text at the center right position?

I am using a text view, and its width is matching the parent. I want to set its text to centre right position. If I use android:gravity="right" then why does it move to the upper right position?

I am using this:

 <TextView android:layout_width="match_parent"
           android:layout_height="wrap_content"
           android:layout_marginLeft="10dp"
           android:textSize="12sp"
           android:textStyle="bold" />

I have tried this many times, but I could not get good results.

Upvotes: 0

Views: 174

Answers (9)

Gowri
Gowri

Reputation: 502

Try the following. It will work.

android:gravity="center"

Upvotes: 0

user2805663
user2805663

Reputation:

If you want to center the text with respect to the layout, use android:layout_gravity="center", but if you just want the letter to center with respect to the whole word, use android:gravity="center".

Upvotes: 0

Sathya
Sathya

Reputation: 678

You can use:

android:gravity="canter"

And then use

android:paddingLeft="<your value>"

Upvotes: 0

Sunil Kumar
Sunil Kumar

Reputation: 7082

Use centre gravity like this:

android:gravity="center|right"

Upvotes: 1

Chintan Trivedi
Chintan Trivedi

Reputation: 748

Use the android:gravity property with pipeline|, setting the property to Center as well as Right position.

android:gravity="center|right"

Upvotes: 3

Nidhi
Nidhi

Reputation: 699

You can use android:padding to match your requirements. Also use android:gravity="center|right".

Upvotes: 3

AK Ali
AK Ali

Reputation: 162

You can use android:gravity="right|center_vertical"

Upvotes: 1

Lokesh
Lokesh

Reputation: 5378

Try this:

android:gravity="right|center_vertical"

want to align horizontal: android:gravity="right|center_horizontal"

Upvotes: 0

Jitender Dev
Jitender Dev

Reputation: 6925

Try this

<TextView
                        android:layout_width="match_parent"
                        android:layout_height="wrap_content"
                        android:layout_marginLeft="10dp"
                        android:textSize="12sp"
                        android:textStyle="bold" 
                        android:gravity="center_vertical|right"   />

Upvotes: 0

Related Questions