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?
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?
android_layout_gravity
behavesfloat:left|right|top|bottom
Whereas
android_gravity
work astext_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).
You can use:
It is basically used in Linear Layout so you have to set your width of TextView or either EditText as
and
When you are working in Relative Layout, use
In my case, I was using
Relative Layout
for aemptyString
After struggling on this for an hour. Only this worked for me:
layout_toRightOf
orlayout_toEndOf
both works, but to support it better, I used both.To make it more clear:
This was what I was trying to do:
And this was the emulator's output
Layout:
Notice that:
android:layout_width="wrap_content"
worksGravity
is not usedMake 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 useandroid:layout_width="fill_parent"
I think that you are doing this:
android:layout_width = "wrap_content"
If this is the case, do this:
android:layout_width = "match_parent"