CepMan
CepMan

Reputation: 101

Why my TextView invisible with long text in previous TextView in LinearLayout?

I have this XML:

<LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:gravity="center_vertical"
        android:orientation="horizontal"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent">

        <ImageView            
            style="@style/Icon"
            tools:background="@color/Black54Opacity" />

        <LinearLayout
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:baselineAligned="false">

            <LinearLayout
                android:layout_width="0dp"
                android:layout_height="24dp"
                android:layout_weight="1"
                android:gravity="center_vertical"
                android:orientation="horizontal">

                <TextView                    
                    android:layout_width="wrap_content"
                    android:layout_height="24dp"                                        
                    android:ellipsize="end"                    
                    android:includeFontPadding="false"                    
                    android:maxLines="1"
                    android:padding="0dp"
                    android:textColor="@android:color/black"
                    android:textSize="20sp"
                    tools:text="LONG LONG LONG" />

                <TextView
                    android:layout_width="wrap_content"
                    android:layout_height="18dp"                    
                    android:minWidth="18dp"
                    tools:text="2342"
                    tools:visibility="visible" />
            </LinearLayout>

            <TextView                
                android:layout_width="wrap_content"
                android:layout_height="24dp"
                android:gravity="center_vertical|end"
                android:textColor="4CAF50"
                tools:text="EDIT" />
        </LinearLayout>
    </LinearLayout>

When I insert a short text in my first TextView - I haven't a problem. Short text Long text

When I insert a long text in my first textView - my second TextView moved under the third TextView. Very Very long text

I want the text in the first TextView can resize to that point so that the other two TextViews don't change their size.

The long text in the first TextView was ellipsized in the end. The second TextView must always be pressed against the left side of the first TextView.

Upvotes: 1

Views: 251

Answers (2)

CepMan
CepMan

Reputation: 101

We found a solution through the ConstraintLayout.

  1. We remove all LinearLayout.
  2. Then we added app:layout_constrainedWidth="true" in my first TextView. It's a very important attribute.

Upvotes: 1

Dharmender Manral
Dharmender Manral

Reputation: 1520

Use weight to your TextView

Try this

<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="match_parent"
android:orientation="horizontal"
tools:context=".MainActivity">

<ImageView
 android:layout_width="wrap_content"
 android:layout_height="wrap_content"
 android:src="@mipmap/ic_launcher"
 android:fitsSystemWindows="true"
 android:scaleType="fitXY"
 android:id="@+id/img"/>

<LinearLayout
 android:layout_width="match_parent"
 android:layout_height="wrap_content"
 android:weightSum="5"
 android:orientation="horizontal">


<TextView
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="2"/>

<TextView
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="2" />

<TextView
android:layout_width="0dp"
android:layout_height="wrap_content"
android:textAlignment="center"
android:layout_weight="1"
android:text="Edit"/>

</LinearLayout>

</LinearLayout>

Upvotes: 0

Related Questions