Make text float right on Android

I am trying to achieve some text layout in Android a bit like float="right" in HTML. I have two paragraphs of text that can be found in a specific area of ​​the screen. I would like one of them to be left-aligned in a TextView, and one of them to be right-aligned, like this (where | at both ends is a container container): -

 | First text item label | 

It still sounds simple so far: I could do it with two TextView in RelativeLayout , right - with setting layout_alignParentRight="true" or layout_gravity="right" . But there is a case where the left text is too long to fit into the remaining space. With RelativeLayout it will overlap the right 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 text spills over multiple lines, I want the right text to fit on the same line, if possible: -

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

Since the View in Android is always rectangular, it seems that it would be impossible to make this last example work with two TextView , even if I wrote a custom Layout . I looked with Spannable so that I could fit all the text into one TextView , but again, AlignmentSpan is a ParagraphStyle , so it doesn't look like it can put the label on the same line as the left text. Is there any other range of text that I can use to achieve this, or even a completely different method?

At the moment, it seems to me that I will either have to put the text in HTML, or use a WebView (not very good, since the view should go in the list item) or write a custom TextView widget.

+6
source share
1 answer

Add android:gravity="right" (not the same as layout_gravity ) to the right, and also add android:layout_alignBottom and refer to the id of another text view. multiline to true etc. I guess you already have one. Save alignParentRight and alignParentLeft respectively.

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

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

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

+5
source

All Articles