Right align text in android TextView
Asked Answered
F

21

251

I have a TextView in my application. I want to align the text in it to the right. I tried adding:

android:gravity="right"

But this doesn't work for me.

What might I be doing wrong?

Flory answered 23/1, 2012 at 9:0 Comment(0)
K
206

I think that you are doing this: android:layout_width = "wrap_content"
If this is the case, do this: android:layout_width = "match_parent"

Kinky answered 23/1, 2012 at 9:4 Comment(3)
@Flory fill will fill the remainder of the space, match will set it equal to the width of the parentWeakling
Actually, fill_parent and match_parent are the same thing. The name was changed to match_parent because apparently fill_parent was confusing people.Vacla
This answer doesn't make sence...Gatehouse
B
171

In general, android:gravity="right" is different from android:layout_gravity="right".

The first one affects the position of the text itself within the View, so if you want it to be right-aligned, then layout_width= should be either "fill_parent" or "match_parent".

The second one affects the View's position inside its parent, in other words - aligning the object itself (edit box or text view) inside the parent view.

Blanks answered 28/1, 2012 at 23:54 Comment(0)
H
102

The better solution is the one that is the most simple, and the one that does less modification in your code behaviour.

What if you can solve this problem only with 2 Properties on your TextView?

Instead of needing to change your LinearLayout Properties that maybe can alter the behaviour of LinearLayout childs?

Using this way, you do not need to change LinearLayout properties and behaviour, you only need to add the two following properties to your target TextView:

android:gravity="right"
android:textAlignment="gravity"

What would be better to change only your target to solve your solution instead of having a chance to cause another problem in the future, modifying your target father? think about it :)

Hausmann answered 20/11, 2013 at 17:30 Comment(4)
I get an error saying no resource identifier found for resource 'textAlignment' in package android.Visualize
You have sure that you are acessing a TextView properties? - You can check the property from the reference here: developer.android.com/reference/android/view/…Hausmann
textAlignment was added in API level 17 (Android 4.2). Your emulator/device will need to be running 4.2+ for this to work.Griqua
I have tried to use this textAlignment property in Android 4.1.2 and got the error "No resource identifier found for attribute 'textAlignment'". I found the answer for this error here https://mcmap.net/q/118700/-error-no-resource-identifier-found-for-attribute-39-textalignment-39-in-package-39-android-39. How could you use it in 4.0.3?Griqua
P
22

In case somebody still needs

android:gravity="end"
Porch answered 16/9, 2017 at 9:13 Comment(2)
what's that for?Batish
In LTR, this aligns the text to right and in RTL, to the leftPorch
T
21

android:layout_gravity is used to align the text view with respect to the parent layout. android:gravity is used to align the text inside the text view.

Are you sure you are trying to align the text inside the text view to the right or do you want to move the text view itself to the right with respect to the parent layout or are you trying to acheive both

Trinitrotoluene answered 23/1, 2012 at 9:13 Comment(1)
i'm trying to align the text inside the text view to the rightFlory
N
15

The following code snippet:

android:textAlignment="textEnd"
android:layout_gravity="end"

works great for me

Newmown answered 22/6, 2017 at 13:19 Comment(0)
M
9
android:gravity="right"

It is basically used in Linear Layout so you have to set your width of TextView or either EditText as

android:layout_width = "match_parent"

and

When you are working in Relative Layout, use

android:layout_alignParentRight="true"
Membranophone answered 27/12, 2016 at 9:28 Comment(0)
H
8

Make sure that you are not using android:layout_width="wrap_content" if so then it won't be able to set the gravity because you are not having enough space for the text to align. Instead use android:layout_width="fill_parent"

Helaine answered 23/1, 2012 at 9:3 Comment(0)
P
5

If it is in RelativeLayout you can use android:layout_toRightOf="@id/<id_of_desired_item>"

Or

If you want to align to the right corner of the device the place android:layout_alignParentRight="true"

Podesta answered 13/5, 2013 at 12:33 Comment(0)
O
5

Make the (LinearLayout) android:layout_width="match_parent" and the TextView's android:layout_gravity="right"

Onions answered 18/8, 2013 at 18:36 Comment(0)
E
5

Below works for me:

<TextView
    android:id="@+id/tv_username"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="Sarah" />

<TextView
    android:id="@+id/tv_distance"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignBottom="@+id/tv_username"
    android:layout_alignParentEnd="true"
    android:layout_toEndOf="@+id/tv_username"
    android:text="San Marco"
    android:textAlignment="viewEnd" />

Please note the android:layout_alignParentEnd and android:textAlignment settings of the second TextView.

Experienced answered 26/2, 2016 at 1:32 Comment(0)
M
4

Let me explain gravity concept using WEB because a lot of developer are aware of it.

android_layout_gravity behaves float:left|right|top|bottom

Whereas android_gravity work as text_align:centre|left|right

In Android float is android_layout_gravity and text_align: is android_gravity.

And android_layout_gravity applies relative to parent. where as android_gravity applies to its child or inner content.

Note android_gravity only works when its View is match_parent(when it have space to center).

Mandiemandingo answered 2/5, 2016 at 11:58 Comment(0)
S
2

You can use:

    android:layout_alignParentRight="true"
    android:layout_centerVertical="true"
Sellma answered 1/6, 2017 at 16:4 Comment(0)
G
2

this should serve the purpose

android:textAlignment="textStart"

Guevara answered 9/6, 2017 at 8:33 Comment(0)
C
2

In my case, I was using Relative Layout for a emptyString

After struggling on this for an hour. Only this worked for me:

android:layout_toRightOf="@id/welcome"
android:layout_toEndOf="@id/welcome"
android:layout_alignBaseline="@id/welcome"

layout_toRightOf or layout_toEndOf both works, but to support it better, I used both.

To make it more clear:

This was what I was trying to do:

enter image description here

And this was the emulator's output

enter image description here

Layout:

<TextView
    android:id="@+id/welcome"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_marginStart="16dp"
    android:layout_marginTop="16dp"
    android:text="Welcome "
    android:textSize="16sp" />
<TextView
    android:id="@+id/username"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignBaseline="@id/welcome"
    android:layout_toRightOf="@id/welcome"
    android:text="@string/emptyString"
    android:textSize="16sp" />

Notice that:

  1. android:layout_width="wrap_content" works
  2. Gravity is not used
Castorina answered 25/2, 2018 at 6:17 Comment(0)
P
1

I also faced the same problem and figured the problem was happening as the layout_width of the TextView was having wrap_content. You need to have layout_width as match_parent and the android:gravity = "gravity"

Principled answered 26/9, 2016 at 1:29 Comment(0)
E
1
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
     android:layout_width="fill_parent"
     android:layout_height="fill_parent">

  <TextView
    android:id="@+id/tct_postpone_hour"
    android:layout_width="match_parent"
    android:layout_height="50dp"
    android:layout_centerHorizontal="true"
    android:layout_centerVertical="true"
    android:layout_gravity="center"
    android:gravity="center_vertical|center_horizontal"
    android:text="1"
    android:textSize="24dp" />
</RelativeLayout>

makes number one align in center both horizontal and vertical.

Ere answered 31/7, 2017 at 12:11 Comment(0)
L
1

Try using :-

android:justificationMode="inter_word"

this will justify your text.

Lucielucien answered 15/10, 2022 at 9:35 Comment(0)
D
0

try to add android:gravity="center" into TextView

Deluna answered 5/8, 2014 at 11:15 Comment(0)
T
0

I have used the attribute "layout_alignRight" in my case, in text its :

android:layout_alignRight="@+id/myComponentId"

If you want to add some right margin, just set the attribute "Layout_Margin Right" to "20dp" for exemple, in text its :

android:layout_marginRight="20dp"
Toritorie answered 8/5, 2019 at 9:26 Comment(0)
N
0

Justification Alignment has been supported for TextView from Android API 26+

<TextView ...
    ... android:justificationMode="inter_word" />

By default, value is none.

Android TextView JustificationMode none vs inter_word

Android TextView Docs

Question on Android TextView Justify Text

Nicolasanicolau answered 29/11, 2021 at 3:16 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.