0

I have a RecyclerView that implements a linear list of string items. I want the list item to have a secondary action, 'share', to the right of each item. I created a compound drawable like Google suggested, in XML, but how do I access the drawable and set a OnClickListener() on it? Am I better off just having two elements?

Compound Drawable TextView in my list item layout:

<TextView
    android:id="@+id/item"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:padding="@dimen/text_margin"
    android:textSize="17sp"
    android:layout_marginTop="1dp"
    android:background="#101010"
    android:drawableRight="@drawable/ic_share_white_24dp"
    android:textColor="#fff"
    android:textAppearance="?attr/textAppearanceListItem" />
Frank B.
  • 204
  • 5
  • 17

1 Answers1

0

Got some good tips but for my project in particular I found it better management to have two elements. Check out @Vishnuvathsan's answer for this StackOverflow post for the compound drawable.

<TextView
    android:id="@+id/content"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:padding="@dimen/text_margin"
    android:textSize="17sp"
    android:layout_marginTop="1dp"
    android:background="#101010"
    android:textColor="#fff"
    android:textAppearance="?attr/textAppearanceListItem" />
<ImageView
    android:id="@+id/shareBtn"
    android:contentDescription="@string/action_share"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:padding="6dp"
    android:src="@drawable/ic_share_white_24dp"
    android:background="#101010"
    android:layout_centerVertical="true"
    android:layout_alignParentRight="true"
    android:layout_alignParentEnd="true" />
Community
  • 1
  • 1
Frank B.
  • 204
  • 5
  • 17