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?
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>