Search code examples
androidandroid-layoutandroid-linearlayoutandroid-layout-weight

weight=1 and android:layout_width="0dp" makes my view disappear


I want to create a layout when I have an image, text aligned to the left of the layout. and a chip aligned to the left.

enter image description here

I have tried this layout:

<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="wrap_content"
    android:paddingTop="@dimen/my_padding"
    android:paddingBottom="@dimen/my_padding"
    android:paddingLeft="@dimen/my_padding2"
    android:paddingRight="@dimen/my_padding2"
    android:background="?attr/selectableItemBackground"
    android:clickable="true"
    android:focusable="true"
    android:orientation="horizontal">

  <ImageView
      android:id="@+id/Icon"
      android:layout_width="@dimen/icon_size"
      android:layout_height="@dimen/icon_size"
      android:layout_margin="@dimen/icon_margin"
      android:layout_gravity="center_horizontal|center_vertical"
      android:contentDescription="@null"
      android:importantForAccessibility="no"
      tools:ignore="UnusedAttribute" />
  <TextView
      android:id="@+id/Text"
      style="@style/ActionItemStyle"
      android:layout_width="match_parent"
      android:layout_height="wrap_content"
      android:layout_marginStart="@dimen/margin_between_icon_and_text"
      android:layout_marginLeft="@dimen/margin_between_icon_and_text"
      android:layout_gravity="center_vertical"
      android:ellipsize="end"
      android:gravity="start|center_vertical"
      android:maxLines="3"
      android:textAppearance="@style/TextAppearance.AppCompat.Body1" />
  <com.my.Chip
      android:id="@+id/highlight_chip"
      style="@style/Widget.Chip.Suggestive"
      android:layout_width="0dp"
      android:layout_height="wrap_content"
      android:layout_weight="1"
      android:layout_marginStart="@dimen/margin_between_highlight_chip_and_text"
      android:layout_marginLeft="@dimen/margin_between_highlight_chip_and_text"
      android:layout_gravity="end|center_vertical"
      android:visibility="visible" />
</LinearLayout>

meaning I have Chip with weight and layout_gravity, but the chip is not seen.

enter image description here

how can i fix this?

The strange thing is that I have tried

<com.my.Chip
      android:id="@+id/highlight_chip"
      style="@style/Widget.GoogleMaterial.Chip.Suggestive"
      android:layout_width="10dp"
      android:layout_height="wrap_content"
      android:layout_marginStart="@dimen/margin_between_highlight_chip_and_text"
      android:layout_marginLeft="@dimen/margin_between_highlight_chip_and_text"
      android:layout_gravity="end|center_vertical"
      android:visibility="visible" />

without weight, and the chip still doesn't show


Solution

  • You don't need to set layout_weight in your com.my.Chip

    Try to set android:layout_weight="1" on your TextView instead of in your com.my.Chip

    and also change android:layout_width="0dp" in your TextView

    You will get output something like this

    enter image description here

    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="wrap_content"
        android:paddingTop="@dimen/my_padding"
        android:paddingBottom="@dimen/my_padding"
        android:paddingLeft="@dimen/my_padding2"
        android:paddingRight="@dimen/my_padding2"
        android:background="?attr/selectableItemBackground"
        android:clickable="true"
        android:focusable="true"
        android:orientation="horizontal">
    
      <ImageView
          android:id="@+id/Icon"
          android:layout_width="@dimen/icon_size"
          android:layout_height="@dimen/icon_size"
          android:layout_margin="@dimen/icon_margin"
          android:layout_gravity="center_horizontal|center_vertical"
          android:contentDescription="@null"
          android:importantForAccessibility="no"
          tools:ignore="UnusedAttribute" />
      <TextView
          android:id="@+id/Text"
          style="@style/ActionItemStyle"
          android:layout_width="0dp"
          android:layout_height="wrap_content"
          android:layout_weight="1"
          android:layout_marginStart="@dimen/margin_between_icon_and_text"
          android:layout_marginLeft="@dimen/margin_between_icon_and_text"
          android:layout_gravity="center_vertical"
          android:ellipsize="end"
          android:gravity="start|center_vertical"
          android:maxLines="3"
          android:textAppearance="@style/TextAppearance.AppCompat.Body1" />
      <com.my.Chip
          android:id="@+id/highlight_chip"
          style="@style/Widget.Chip.Suggestive"
          android:layout_width="wrap_content"
          android:layout_height="wrap_content"
          android:layout_marginStart="@dimen/margin_between_highlight_chip_and_text"
          android:layout_marginLeft="@dimen/margin_between_highlight_chip_and_text"
          android:layout_gravity="end|center_vertical"
          android:visibility="visible" />
    </LinearLayout>