-1

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="40dp"
      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>

Why does the textView's match_parent makes the chip's width to be 0dp?

I read about match_parent:

Special value for the height or width requested by a View. MATCH_PARENT means that the view wants to be as big as its parent, minus the parent's padding, if any. Introduced in API Level 8.

but does it ignores the siblings size?

I know I can use weight, but still want to understand: does match_parent ignore siblings' width?

enter image description here

Elad Benda
  • 35,076
  • 87
  • 265
  • 471

1 Answers1

0

match_parent attribute reacts according to the Parent View. LinearLayout's child view with width as `match_parent will take up the whole width and will not accommodate any further child view if orientation is horizontal as we are talking abour width.

For LinearLayout - To accommodate another child view along with child view having match_parent; you need to use layout_weight.

Though you can use another ViewGroup - FrameLayout without making use of layout_weight.

Below is the code using FrameLayout as Parent ViewGroup.

<FrameLayout 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="start|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"//give enough margin from your icon
       android:layout_marginLeft="@dimen/margin_between_icon_and_text"//give enough margin from your icon
       android:layout_marginRight="64dp"//give enough margin from your chip
       android:layout_gravity="start|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="40dp"
       android:layout_height="wrap_content"
       android:layout_marginEnd="8dp"//give enough margin from right edge of screen
       android:layout_gravity="end|center_vertical"
       android:visibility="visible" />
 </FrameLayout>
Vikalp Patel
  • 10,669
  • 6
  • 61
  • 96
  • I have added to the chip `android:layout_width="0dp" android:layout_weight="1"` but the chip still get out of the frame – Elad Benda Sep 25 '19 at 12:12
  • `LinearLayout` requires all the child view's to have `android:layout_weight` to proportionate according to weight. Just making Chip layout with android:layout_width="0dp" android:layout_weight="1" won't work. All the other view's - `TextView` & `ImageView` should also have weight & width as 0dp; that will make it proportionate, it's better to use `FrameLayout` in that case. Please try the solution; I have provided in the above answer. – Vikalp Patel Sep 25 '19 at 13:07