2

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?

Phantômaxx
  • 37,901
  • 21
  • 84
  • 115
rosu alin
  • 5,674
  • 11
  • 69
  • 150

3 Answers3

9

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>
ADM
  • 20,406
  • 11
  • 52
  • 83
2

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

Alex
  • 3,382
  • 2
  • 32
  • 41
2

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.

Maksym V.
  • 2,877
  • 17
  • 27