rosu alin
rosu alin

Reputation: 5830

LinearLayout 2 views, if one is gone, it doesn't center the other, although I update the weightSum

this is my layout:

 <LinearLayout
        android:id="@+id/car_linear_layout"
        android:layout_toRightOf="@id/car_image"
        android:layout_width="wrap_content"
        android:orientation="vertical"
        android:layout_centerVertical="true"
        android:gravity="center_vertical"
        android:layout_height="50sp"
        android:weightSum="2">
        <TextView
            android:id="@+id/car_layout_name"
            android:textSize="17sp"
            android:maxLines="1"
            android:textColor="@color/black"
            android:ellipsize="end"
            android:layout_width="wrap_content"
            android:layout_height="0dp"
            android:layout_weight="1"/>
        <TextView
            android:id="@+id/car_layout_license"
            android:textSize="17sp"
            android:maxLines="1"
            android:ellipsize="end"
            android:textColor="@color/black"
            android:layout_width="wrap_content"
            android:layout_height="0dp"
            android:layout_weight="1"/>
    </LinearLayout>

And this is my code:

    if (userVehicleID != null) {
        carLayout.setWeightSum(2);
        carLicense.setText("test");
    }else{
        carLayout.setWeightSum(1);
        carLicense.setVisibility(View.GONE);
    } 

But even so, if I debug, and it enters the ELSE , it will show just the first textview, but it's not centered. why?

Upvotes: 2

Views: 1729

Answers (3)

SpiritCrusher
SpiritCrusher

Reputation: 21053

just remove android:weightSum from parent layout .

So if you set visibility of any child to GONE then the other child will cover the weight.If In your case parent layout is wrap_content after making a child as GONE, this will make the Your parent LinearLayout height equal to Height of VISIBLE TextView. This is the way layout_weight works.

<LinearLayout
    android:id="@+id/car_linear_layout"
    android:layout_toRightOf="@id/car_image"
    android:layout_width="wrap_content"
    android:orientation="vertical"
    android:layout_centerVertical="true"
    android:gravity="center_vertical"
    android:layout_height="50sp"
   >
    <TextView
        android:id="@+id/car_layout_name"
        android:textSize="17sp"
        android:maxLines="1"
        android:textColor="@color/black"
        android:ellipsize="end"
        android:layout_width="wrap_content"
        android:layout_height="0dp"
        android:layout_weight="1"/>
    <TextView
        android:id="@+id/car_layout_license"
        android:textSize="17sp"
        android:maxLines="1"
        android:ellipsize="end"
        android:textColor="@color/black"
        android:layout_width="wrap_content"
        android:layout_height="0dp"
        android:layout_weight="1"/>
</LinearLayout>

Upvotes: 9

Maksym V.
Maksym V.

Reputation: 2937

Do not change the weightSum or specify textAlignment. Since you are setting weightSum to 2 it takes all available height and sets text only on top of view.

Upvotes: 2

Alex
Alex

Reputation: 3382

You need to trigger a layout update (requestLayout()) to make the layout to recalculate it's children after changing the weightSum and hiding the view

Upvotes: 2

Related Questions