androidtextviewandroid-layout-weight

Android TextView does not expand to match parent


I have a viewgroup like this image:

enter image description here

I want to not show those texts that are empty. For example, assume that I have no tips and promo then just Notes should be visible. When I test my program Notes (displayed with red background) does not expand to fill parent although its width set to match parent.

Any idea would be appreciated. Thanks.

<?xml version="1.0" encoding="utf-8"?>

<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/llNotesContainer"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:background="@color/tracking_bg_note"
    android:orientation="horizontal">

    <LinearLayout
        android:id="@+id/llNotes"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:orientation="vertical"
        android:padding="8dp">

        <TextView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="@string/tracking_notes"
            android:textColor="@color/tracking_font_address"
            android:textSize="13sp"/>

        <TextView
            android:id="@+id/tvNotes"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:maxLines="2"
            android:ellipsize="end"
            android:textColor="@color/tracking_font_note"
            android:textSize="15sp"
            android:background="@color/red"/>
    </LinearLayout>

    <View
        android:id="@+id/vVerticalSeparator"
        android:layout_width="1dp"
        android:layout_height="match_parent"
        android:layout_marginBottom="8dp"
        android:layout_marginTop="8dp"
        android:background="@color/tracking_separator"/>

    <!-- Tips and Promos -->
    <LinearLayout
        android:id="@+id/llTipsPromos"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:orientation="vertical"
        android:gravity="center_vertical">

        <LinearLayout
            android:id="@+id/llTips"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="horizontal"
            android:gravity="center_vertical"
            android:padding="8dp">

            <TextView
                android:layout_width="50dp"
                android:layout_height="wrap_content"
                android:text="@string/tracking_tips"
                android:textColor="@color/tracking_font_address"
                android:textSize="13sp"/>

            <TextView
                android:id="@+id/tvTips"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:textColor="@color/tracking_font_note"
                android:textSize="15sp"
                android:singleLine="true"
                android:ellipsize="end"/>
        </LinearLayout>

        <View
            android:id="@+id/vHorizontalSeparator"
            android:layout_width="match_parent"
            android:layout_height="1dp"
            android:background="@color/tracking_separator"/>

        <LinearLayout
            android:id="@+id/llPromos"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="horizontal"
            android:gravity="center_vertical"
            android:padding="8dp">

            <TextView
                android:layout_width="50dp"
                android:layout_height="wrap_content"
                android:text="@string/tracking_promo"
                android:textColor="@color/tracking_font_address"
                android:textSize="13sp"/>

            <TextView
                android:id="@+id/tvPromos"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:textColor="@color/tracking_font_note"
                android:textSize="15sp"
                android:singleLine="true"
                android:ellipsize="end"/>
        </LinearLayout>
    </LinearLayout>
</LinearLayout>

In the code I have a method that call once I want to update UI. The method is:

private void showHideSeparators()
    {
        boolean notes = false;
        boolean tips = false;
        boolean promos = false;

        if (!TextUtils.isEmpty(this.tvNotes.getText()))
        {
            notes = true;
        }

        if (!TextUtils.isEmpty(this.tvTips.getText()))
        {
            tips = true;
        }

        if (!TextUtils.isEmpty(this.tvPromos.getText()))
        {
            promos = true;
        }

        // We need to consider 8 configurations due to 3 variables
        if (!notes && !tips && !promos)
        {
            this.llNotesContainer.setVisibility(View.GONE);
            return;
        }

        if (!notes && !tips && promos)
        {
            this.llNotesToDriver.setVisibility(View.GONE);
            this.llTips.setVisibility(View.GONE);
            this.vVerticalSeparator.setVisibility(View.GONE);
            this.vHorizontalSeparator.setVisibility(View.GONE);
            return;
        }

        if (!notes && tips && !promos)
        {
            this.llNotesToDriver.setVisibility(View.GONE);
            this.llPromos.setVisibility(View.GONE);
            this.vVerticalSeparator.setVisibility(View.GONE);
            this.vHorizontalSeparator.setVisibility(View.GONE);
            return;
        }

        if (!notes && tips && promos)
        {
            this.llNotesToDriver.setVisibility(View.GONE);
            this.vVerticalSeparator.setVisibility(View.GONE);
            return;
        }

        if (notes && !tips && !promos)
        {
            this.llTips.setVisibility(View.GONE);
            this.llPromos.setVisibility(View.GONE);
            this.vVerticalSeparator.setVisibility(View.GONE);
            this.vHorizontalSeparator.setVisibility(View.GONE);
            return;
        }

        if (notes && !tips && promos)
        {
            this.llTips.setVisibility(View.GONE);
            this.vVerticalSeparator.setVisibility(View.GONE);
            return;
        }

        if (notes && tips && !promos)
        {
            this.llPromos.setVisibility(View.GONE);
            this.vHorizontalSeparator.setVisibility(View.GONE);
            return;
        }

        // this is default situation when all is true
        if (notes && tips && promos)
        {
            return;
        }
    }

I even tried to invalidate my textView but nothing has changed. Result is like this when I just have notes.

enter image description here


Solution

  • Finally I found a way to cope with the issue. This is the change:

    <?xml version="1.0" encoding="utf-8"?>
    
    <LinearLayout
        xmlns:android="http://schemas.android.com/apk/res/android"
        android:id="@+id/llNotesContainer"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="@color/tracking_bg_note"
        android:orientation="horizontal">
    
        <LinearLayout
            android:id="@+id/llNotes"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:orientation="vertical"
            android:padding="8dp">
    
            <TextView
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:text="@string/tracking_notes"
                android:textColor="@color/tracking_font_address"
                android:textSize="13sp"/>
    
            <TextView
                android:id="@+id/tvNotes"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:textColor="@color/tracking_font_note"
                android:textSize="15sp"
                android:background="@color/red"/>
        </LinearLayout>
    
        <View
            android:id="@+id/vVerticalSeparator"
            android:layout_width="1dp"
            android:layout_height="match_parent"
            android:layout_marginBottom="8dp"
            android:layout_marginTop="8dp"
            android:background="@color/tracking_separator"/>
    
        <!-- Tips and Promos -->
        <LinearLayout
            android:id="@+id/llTipsPromos"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:orientation="vertical"
            android:gravity="center_vertical">
    
            <LinearLayout
                android:id="@+id/llTips"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:orientation="horizontal"
                android:gravity="center_vertical"
                android:padding="8dp">
    
                <TextView
                    android:layout_width="50dp"
                    android:layout_height="wrap_content"
                    android:text="@string/tracking_tips"
                    android:textColor="@color/tracking_font_address"
                    android:textSize="13sp"/>
    
                <TextView
                    android:id="@+id/tvTips"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:textColor="@color/tracking_font_note"
                    android:textSize="15sp"/>
            </LinearLayout>
    
            <View
                android:id="@+id/vHorizontalSeparator"
                android:layout_width="match_parent"
                android:layout_height="1dp"
                android:background="@color/tracking_separator"/>
    
            <LinearLayout
                android:id="@+id/llPromos"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:orientation="horizontal"
                android:gravity="center_vertical"
                android:padding="8dp">
    
                <TextView
                    android:layout_width="50dp"
                    android:layout_height="wrap_content"
                    android:text="@string/tracking_promo"
                    android:textColor="@color/tracking_font_address"
                    android:textSize="13sp"/>
    
                <TextView
                    android:id="@+id/tvPromos"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:textColor="@color/tracking_font_note"
                    android:textSize="15sp"/>
            </LinearLayout>
        </LinearLayout>
    </LinearLayout>