Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Make text float-right in Android

I'm trying to achieve some text layout in Android a little like float="right" in HTML. I have two items of text to go in a certain area of my screen. I'd like one of them to be left-aligned in a TextView, and one of them to be right-aligned, like this (where the | at either end represent the enclosing container):-

| First text item      label |

So far this sounds easy: I could do it with two TextViews in a RelativeLayout, the right-hand one set to layout_alignParentRight="true" or layout_gravity="right". But there's also the case where the left-hand text is too long to fit in the remaining space. With a RelativeLayout it would overlap the right-hand text, but I want it to break into two lines in this case:-

| This text is way too long  |
|                      label |

and similarly, if the left-hand text flows onto multiple lines, I want the right-hand text to fit on the same line if possible:-

| This text doesn't fit on   |
| one line             label |

Because Views in Android are always rectangular, it seems like it would be impossible to make this last example work with two TextViews even if I wrote a custom Layout. I looked into using a Spannable to let me put all the text in one TextView, but again, AlignmentSpan is a ParagraphStyle, so it doesn't look like it'll be able to put the label on the same line as the left-hand text that way. Is there some other kind of text span I can use to achieve this, or even a completely different method?

At the moment, it looks like I'll have to either put the text in HTML and use a WebView (not great, as the view is to go in a list item), or write a custom TextView-like widget.

like image 828
Dan Hulme Avatar asked Dec 19 '12 17:12

Dan Hulme


People also ask

How do you right align text on android?

To right align text in TextView in Kotlin Android, set android:textAlignment attribute with the value “viewEnd” in layout file, or programmatically set the textAlignment property of the TextView object with View.

How do you move a linear layout to the right?

Try to change the layout_width to android:layout_width="match_parent" because gravity:"right" aligns the text inside the layout_width, and if you choose wrap content it does not have where to go, but if you choose match parent it can go to the right.

How do I align text in text view?

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.

How do you center text in TextView?

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.


1 Answers

Add android:gravity="right" (not the same as layout_gravity) to the right one but also add android:layout_alignBottomand refer to id of the other text view. multiline to true etc I assume you already have. Keep the alignParentRight and alignParentLeft respectively.

To be clear, while they are rectangular they can still overlap if you want, using RelativeLayout (or FrameLayout etc)

Alternative way, try this: swap android:layout_alignBottom="@id..." with android:layout_alignParentBottom="true" for the right side text view.

Getting the third example and the second example to work at the same time though with a simple RelativeLayout, hmm...

like image 165
Mattias Isegran Bergander Avatar answered Oct 26 '22 15:10

Mattias Isegran Bergander