Suraj Patil
Suraj Patil

Reputation: 25

Android: How to divide a LinearLayout in unequal parts?

I have 2 TextViews, and I want to assign 67% of the space to title and the remaining 33% to the details.

<LinearLayout
    android:id="@+id/layout"
    android:layout_width="0dp"
    android:layout_height="match_parent"
    android:layout_weight="1"
    android:orientation="horizontal"
    android:weightSum="2"
    android:layout_marginStart="4dp"
    android:layout_marginEnd="@4dp">

    <TextView
        android:id="@+id/title"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center_vertical"
        android:textSize="16sp" />

    <TextView
        android:id="@+id/details"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center_vertical"
        android:ellipsize="end"
        android:maxLines="1"
        android:textSize="16sp" />
</LinearLayout>

Upvotes: 1

Views: 502

Answers (2)

Shubham Raitka
Shubham Raitka

Reputation: 1052

Set the widths of the TextViews as 0dp and set layout_weight as 2 for title TextView and layout_weight as 1 for description TextView.

Upvotes: 0

Jeel Vankhede
Jeel Vankhede

Reputation: 12138

Use weight sum on LinearLayout as 4 & provide weight to both TextView like below:

<LinearLayout
android:id="@+id/layout"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:orientation="horizontal"
android:weightSum="4"
android:layout_marginStart="4dp"
android:layout_marginEnd="@4dp">

<TextView
    android:id="@+id/title"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_gravity="center_vertical"
    android:layout_weight="1"
    android:textSize="16sp" />

<TextView
    android:id="@+id/details"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_gravity="center_vertical"
    android:ellipsize="end"
    android:layout_weight="3"
    android:maxLines="1"
    android:textSize="16sp" />

Upvotes: 1

Related Questions