How do I use a compound drawable instead of a LinearLayout that contains an ImageView and a TextView

AndroidAndroid LayoutAndroid LinearlayoutTextviewCompound Drawables

Android Problem Overview


Ran the new Lint tool against my code. It came up with a lot of good suggestions, but this one I cannot understand.

>This tag and its children can be replaced by one and a compound drawable > >Issue: Checks whether the current node can be replaced by a TextView using compound drawables. > >A LinearLayout which contains an ImageView and a TextView can be more efficiently handled as a compound drawable

And here is my layout

<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:layout_centerInParent="true">

<ImageView 
    android:id="@+id/upImage"
    android:layout_width="20dp"
    android:layout_height="20dp"
    android:layout_gravity="center_vertical"
    android:scaleType="centerInside"
    android:src="@drawable/up_count_big">
</ImageView>

<TextView
    android:id="@+id/LikeCount"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_marginLeft="2dp"
    android:layout_marginBottom="1dp"
    android:textColor="@color/gray"
    android:textSize="16sp"
    android:layout_gravity="center_vertical">
</TextView>
</LinearLayout>

Can someone provide a concrete example of how to make a compound drawable in this case?

Android Solutions


Solution 1 - Android

TextView comes with 4 compound drawables, one for each of left, top, right and bottom.

In your case, you do not need the LinearLayout and ImageView at all. Just add android:drawableLeft="@drawable/up_count_big" to your TextView.

See TextView#setCompoundDrawablesWithIntrinsicBounds for more info.

Solution 2 - Android

if for some reason you need to add via code, you can use this:

mTextView.setCompoundDrawablesWithIntrinsicBounds(left, top, right, bottom);

where left, top, right bottom are Drawables

Solution 3 - Android

To add to this - it seems important to define the width & height of the drawable as per this post:

(his code works)

Solution 4 - Android

You can use general compound drawable implementation, but if you need to define a size of drawable use this library:

https://github.com/a-tolstykh/textview-rich-drawable

Here is a small example of usage:

<com.tolstykh.textviewrichdrawable.TextViewRichDrawable
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Some text"
        app:compoundDrawableHeight="24dp"
        app:compoundDrawableWidth="24dp" />

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionLeoView Question on Stackoverflow
Solution 1 - AndroidchiukiView Answer on Stackoverflow
Solution 2 - AndroidJavier PView Answer on Stackoverflow
Solution 3 - AndroidRichard Le MesurierView Answer on Stackoverflow
Solution 4 - AndroidOleksandrView Answer on Stackoverflow