Jas
Jas

Reputation: 3212

Adding TextViews separated by space

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

Upvotes: 2

Views: 222

Answers (3)

Bruno Carrier
Bruno Carrier

Reputation: 532

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);

Upvotes: 1

Ranjithkumar
Ranjithkumar

Reputation: 18356

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

Upvotes: 0

Kingfisher Phuoc
Kingfisher Phuoc

Reputation: 8190

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>

Upvotes: 3

Related Questions