0

I have two textviews, and I want to align the second textview to right or to the bottom of the first textview depending on the space. The result should be something like:

First case (single line):

Text1 one line Text2

Second case (multi line):

Text1 example with

two lines
___________Text2

As you can see, in the first case the second textview is aligned to the right, while on the second case it is aligned to the right and bottom. How could I achieve that result in xml?

Thanks in advance!

FVod
  • 2,245
  • 5
  • 25
  • 52

2 Answers2

0

You can use the FlowLayout ViewGroup as parent.

https://github.com/ApmeM/android-flowlayout

giannisf
  • 2,479
  • 1
  • 17
  • 29
0

what are you mean Text1 larger than the screen?

this is sample code that i wrote :

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="horizontal"
    android:layout_width="match_parent"
    android:background="@android:color/holo_green_dark"
    android:layout_height="wrap_content">

    <TextView
    android:layout_weight="1"
    android:gravity="bottom|left"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:text="your text more than 1 line"/>

    <TextView
        android:gravity="center|bottom"
        android:layout_weight="1"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:text="Text2"/>

</LinearLayout>

in order to get Text1 is left and Text2 is right, use LinearLayout and set the orientation:"horizontal"

enter image description here

let me know, if this doesn't answer what you want.

related with your question, you just need to play with TextView's gravity and if the value of Textview dynamic, you have to create condition in your java file.

R Besar
  • 574
  • 1
  • 5
  • 12
  • Thank you for your answer, this is not what I was looking for. Let me explain, I want the second textview to be aligned to the right of the first textview if the first text doesn't fill the width of the screen. And, at the bottom-right if there's more than one line in the first textview. – FVod Mar 04 '16 at 08:01
  • check my updated answer. I add more that 1 line and text 2 still at the bottom. let me know if there still not what you need. – R Besar Mar 04 '16 at 09:08
  • Thank you, take a look at my example. When multiline, then I need the text2 to be at the bottom right, not just at right of the text1 :( – FVod Mar 04 '16 at 09:36
  • have you try my code to your project? if you need the `TextView2` is right, you can change the `android:gravity` of the `TextView2` to be `right|bottom` instead of `center|bottom` or if you need the implementation in java, you just have to create condition of the Textview2's `gravity` – R Besar Mar 04 '16 at 09:56