devnull69
devnull69

Reputation: 16584

LinearLayout: How to correctly wrap longer text with no line breaks

this is currently my row layout for a listview:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="horizontal">

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="10dp"
        android:layout_marginLeft="5dp"
        android:text="99.99.9999"
        android:layout_weight="0.2"/>

    <LinearLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:orientation="vertical"
        android:layout_weight="0.6">
        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:padding="10dp"
            android:text="This is a correct looking text"
            android:id="@+id/txtComment"/>
    </LinearLayout>

    <LinearLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_weight="0.2">
        <TextView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:gravity="right"
            android:layout_marginTop="10dp"
            android:textStyle="bold"
            android:layout_marginRight="5dp"
            android:text="-1234,56" />
    </LinearLayout>
</LinearLayout>

The problem is within the TextView with id txtComment. Everything is looking nicely for short text content and text content that contains regular line breaks.

But as soon as there is a long text without line breaks, Android still wraps the text but also changes the weight of the surrounding LinearLayout so that the left and right textviews are being compressed.

How can I make sure that for long texts without line breaks Android still respects the weights of the elements?

Screenshots showing the issue:

Short text Longer text with line breaks Longer text without line breaks

Upvotes: 1

Views: 2787

Answers (5)

Divers
Divers

Reputation: 9569

There is much better and cleaner way to achieve that:

<android.support.v7.widget.GridLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_centerHorizontal="true"
    android:padding="4dp"
    app:alignmentMode="alignBounds"
    app:columnCount="3"
    app:rowOrderPreserved="false"
    app:useDefaultMargins="true">

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginLeft="5dp"
        android:layout_marginTop="10dp"
        android:text="99.99.9999"
        app:layout_gravity="fill_horizontal" />

    <TextView
        android:id="@+id/txtComment"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:padding="10dp"
        android:text="This is a correct looking textThis is a correct looking textThis is a correct looking textThis is a correct looking textThis is a correct looking text"
        app:layout_columnWeight="3"
        app:layout_gravity="fill_horizontal" />

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginRight="5dp"
        android:layout_marginTop="10dp"
        android:gravity="right"
        android:text="-1234,56"
        android:textStyle="bold"
        app:layout_gravity="fill_horizontal" />
</android.support.v7.widget.GridLayout>

Also don't forgot to add in your gradle file:

compile 'com.android.support:gridlayout-v7:24.1.0'

enter image description here

Upvotes: 0

Vygintas B
Vygintas B

Reputation: 1694

In your root element: android:weightSum="1"

In your child elements android:layout_width="0dp"

Upvotes: 0

Sohail Zahid
Sohail Zahid

Reputation: 8149

you just have to set your android:layout_width="wrap_content" to android:layout_width="0dip" where you set weights.

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="horizontal">

    <TextView
        android:layout_width="0dip"
        android:layout_height="wrap_content"
        android:layout_marginLeft="5dp"
        android:layout_marginTop="10dp"
        android:layout_weight="0.2"
        android:text="99.99.9999" />

    <LinearLayout
        android:layout_width="0dip"
        android:layout_height="wrap_content"
        android:layout_weight="0.6"
        android:orientation="vertical">

        <TextView
            android:id="@+id/txtComment"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:padding="10dp"
            android:text="This is a correct looking textThis is a correct looking textThis is a correct looking textThis is a correct looking textThis is a correct looking text" />
    </LinearLayout>

    <LinearLayout
        android:layout_width="0dip"
        android:layout_height="wrap_content"
        android:layout_weight="0.2">

        <TextView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginRight="5dp"
            android:layout_marginTop="10dp"
            android:gravity="right"
            android:text="-1234,56"
            android:textStyle="bold" />
    </LinearLayout>
</LinearLayout>

enter image description here

Upvotes: 1

Kelevandos
Kelevandos

Reputation: 7082

The thing is, the layout works alright :-) You set the weights as 20%, 60% and 20%, which is exactly what happens in the last case.

Also, you should set android:layout_width="0dp" when using LineraLayout weights.

Upvotes: 0

Neerajlal K
Neerajlal K

Reputation: 6828

Try this,

The weights are not working properly because you have set the width as wrap_content. Change it to 0dp.

<TextView
    android:layout_width="0dp"
    android:layout_height="wrap_content"
    android:layout_marginTop="10dp"
    android:layout_marginLeft="5dp"
    android:text="99.99.9999"
    android:layout_weight="0.2"/>

<LinearLayout
    android:layout_width="0dp"
    android:layout_height="wrap_content"
    android:orientation="vertical"
    android:layout_weight="0.6">
    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:padding="10dp"
        android:text="This is a correct looking text"
        android:id="@+id/txtComment"/>
</LinearLayout>

<LinearLayout
    android:layout_width="0dp"
    android:layout_height="wrap_content"
    android:layout_weight="0.2">
    <TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:gravity="right"
        android:layout_marginTop="10dp"
        android:textStyle="bold"
        android:layout_marginRight="5dp"
        android:text="-1234,56" />
</LinearLayout>

Upvotes: 3

Related Questions