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
-
6What are you asking, exactly? – Mike M. Mar 23 '16 at 04:36
-
1seperated by comma means? – Ranjithkumar Mar 23 '16 at 04:39
-
2What you have tried ? – Sree Mar 23 '16 at 04:39
-
I want to have an address view with 3 TextViews(one for address, one for landmark and one for area code) which will be seperated by comma. I just want the TextViews to be appended – Jas Mar 23 '16 at 04:40
-
2can you post what you have – Natarajan Raman Mar 23 '16 at 04:41
-
@RanjithKumar forget comma. I want to append 3 TextViews – Jas Mar 23 '16 at 04:41
-
@Jas ok.. ask clearly all are confused.. – Ranjithkumar Mar 23 '16 at 04:42
-
If I am not mistaken, you want the children of Horizontal Linear Layout to be distributed over 2 rows ? – Shady Atef Mar 23 '16 at 04:44
-
1Question itself confusing manner, please post question clearly. – sandeepmaaram Mar 23 '16 at 04:45
-
@Jas have you try flowLayout? – Vishal Thakkar Mar 23 '16 at 04:52
-
Possible duplicate of [Android - LinearLayout Horizontal with wrapping children](http://stackoverflow.com/questions/2961777/android-linearlayout-horizontal-with-wrapping-children) – Shady Atef Mar 23 '16 at 04:55
-
@VishalHalani I want to do this simply rather than using libraries and all. Is there any way to do this? – Jas Mar 23 '16 at 04:59
-
@Jas see this link hope it help you... its not completely match with your requirement but i think you can get idea from this to make logic of this solution..http://stackoverflow.com/questions/20571553/android-make-left-textview-to-be-shorten-and-right-remain-fully-visible/20591133#20591133 – Vishal Thakkar Mar 23 '16 at 05:30
3 Answers
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>

- 8,052
- 9
- 46
- 86
-
1So 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
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);

- 530
- 5
- 8
-
I know. That will be straightforward . But I want to fetch the data from these 3 TextViews seperately later. That's why – Jas Mar 23 '16 at 05:04
-
-
That would be complicated. If I use 3 TextViews, I could've simply read the text using getText() method – Jas Mar 23 '16 at 05:09
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

- 16,071
- 12
- 120
- 159