2

I want to have 3 TextViews in a View. The TextViews has to be one after another horizontally separated by say 10dp margin. Suppose if one TextView's content exceeds one line, the remaining content along with the remaining TextViews should be shifted to next line just like when using wrap_content.I don't want the TextViews to occupy equal space. It should occupy space according to its content only

Cœur
  • 37,241
  • 25
  • 195
  • 267
Jas
  • 3,207
  • 2
  • 15
  • 45

3 Answers3

3

I think you want FlowLayout? FlowLayout:

Extended linear layout that wrap its content when there is no place in the current line.

Add it as dependency in Gradle as: compile 'org.apmem.tools:layouts:1.10@aar' and declare in xml:

<org.apmem.tools.layouts.FlowLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
>
</org.apmem.tools.layouts.FlowLayout>
Kingfisher Phuoc
  • 8,052
  • 9
  • 46
  • 86
  • 1
    So there is no other simple way to do this other than complicating things using libraries?? – Jas Mar 23 '16 at 04:57
  • @Jas AFAIK, there's no way. You can try to create your own `Custom ViewGroup`, but I think it's more complicated. Using the `FlowLayout` above, you can just add your view as normal `LinearLayout`, that's all. – Kingfisher Phuoc Mar 23 '16 at 06:49
1

I suggest using a string builder with 1 text view instead of 3 text views - it'll be more straigthforward, involve less views (less expensive) and be easier to maintain

Edit: if you need to access parts of that textview later, you can store parts of your textview's text in String fields. That way, your view hierarchy will be simple and you'll still be able to access the text particles separately

Pseudo-code exmaple: string1 = "potatoes"; string2 = "are better than"; string3 = "cucumbers"; textview.setText(string1 + string2 + string3);

Bruno Carrier
  • 530
  • 5
  • 8
0

Use weightSum for linearLayout

<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:weightSum="3"
android:orientation="horizontal"
>

You can set the layout_weight of each TextView to 1 and the weightSum in LinearLayout to 3 to achieve this.

<TextView
android:layout_height="wrap_content"       
android:layout_weight="1"
android:layout_width="0dp"/>

Official guide - http://developer.android.com/reference/android/widget/LinearLayout.html#attr_android:weightSum

other tutorial - http://androidtuts.weebly.com/xml-layout-weightsum-weight.html

Update:

If you need space between 3 textviews add "Space" view. & set weight how you needed.

or use margin left or right

Ranjithkumar
  • 16,071
  • 12
  • 120
  • 159