0

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

barotia
  • 428
  • 4
  • 12
Elad Benda
  • 35,076
  • 87
  • 265
  • 471

2 Answers2

2

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>
AskNilesh
  • 67,701
  • 16
  • 123
  • 163
  • will try, but why does that matter where i put the weight? – Elad Benda Sep 24 '19 at 08:19
  • @EladBenda if you want to set weight in your `com.my.Chip` then just change `width` of your `textView` to **`android:layout_width="wrap_content"`** it will work – AskNilesh Sep 24 '19 at 08:21
  • @EladBenda [why does that matter where i put the weight?](https://stackoverflow.com/q/3995825/7666442) – AskNilesh Sep 24 '19 at 08:22
  • @NileshRathod, It matters where you put the weight. The parent has a defined `width` which we want to divide among the `child views`. So, the child view with highest priority gets `weight=1`, and the remaining views will either get `wrap_content` or `some dp value`. This is important because when android lays out the views, it assigns the values to the ones which don't have `weight` associated first, and whatever is remaining is assigned to the view with the `weight`. I hope this helps. – theThapa Sep 24 '19 at 14:42
0

The issue is, while you provide "weight" attribute just to the textview, it will not render the expected output. You need to use "weight_sum" attribute to create an opening for all the three views which you have used inside the view group i.e. imageview, textview and the chip. so the solution will look like this ->

<?xml version="1.0" encoding="utf-8"?>
<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:orientation="horizontal"
    android:padding="20dp"
    android:weightSum="2">

    <ImageView
        android:layout_width="0dp"
        android:layout_height="30dp"
        android:layout_weight="0.2"
        android:background="@drawable/ic_launcher_background"
        tools:ignore="ContentDescription" />

    <TextView
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        android:layout_marginStart="10dp"
        android:layout_marginEnd="10dp"
        android:layout_weight="1.6"
        android:background="@android:color/holo_green_light"
        android:text="Demo Text" />

    <com.google.android.material.chip.Chip
        android:layout_width="0dp"
        android:layout_height="20dp"
        android:layout_gravity="center"
        android:layout_weight="0.2"
        android:background="@drawable/ic_launcher_background" />

</LinearLayout>