Search code examples
androidandroid-linearlayoutandroid-xml

Prevent linearlayout pushing elements from its side in horizontal orientation


I have a LinearLayout with orientation horizontal and I don't want my first TextView to push the other ones, this is my code

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    xmlns:tools="http://schemas.android.com/tools"
    android:weightSum="3"
    android:orientation="horizontal">

    <CheckBox
        android:id="@+id/checkBox"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />

    <TextView
        android:id="@+id/item_name"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:text="TEST asdas asd" />

    <TextView
        android:id="@+id/item_qty"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:gravity="center"
        android:layout_weight="1"
        android:text="Qty: 2" />

    <TextView
        android:id="@+id/item_price"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:gravity="center"
        android:layout_weight="1"
        tools:text="$100.00"/>

</LinearLayout>

Output

With not so much text it looks like this

enter image description here

But when the text increases there is a problem... the first TextView pushes all other text views and I want to keep the aspect of the first image if the first text just increases I want to give it a line and not push adjacent text.

enter image description here


Solution

  • I think you have a problem with the layout. You are setting the weights of your items, but you have the layout_width set to wrap_content which is contradictory with the layout_weight. Hence I would recommend modifying the layout as follows (basically setting the layout_width=0dp).

    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:tools="http://schemas.android.com/tools"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal"
        android:weightSum="3">
    
        <CheckBox
            android:id="@+id/checkBox"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content" />
    
        <TextView
            android:id="@+id/item_name"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:text="Some long text that is pushing the other texts to the right and taking up all the spaces!!!" />
    
        <TextView
            android:id="@+id/item_qty"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:gravity="center"
            android:text="Qty: 2" />
    
        <TextView
            android:id="@+id/item_price"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:gravity="center"
            tools:text="$100.00" />
    
    </LinearLayout>
    

    I hope that helps!