CompoundDrawable size

drawablestart size
compound drawable
android scale drawable
textview drawable size
android set drawable size in xml
android drawablestart
android studio change image size
set compound drawable textview

Android-Lint gives me this hint in some cases:

This tag and its children can be replaced by one and a compound drawable

I could do it on some places, but on other places where scaling the image is important I am not able to do it. Is there any option that i can set the size for a compound drawable?

I finally released a library for it:

Use this library to set size of compound drawable.

Original answer

You can add custom attribute to your view to work with size of drawable. Of course, this solution requires additional code, but only once! After you can solve the problem using xml only (only once change the control and use it in all project).

See my example:

public class TextViewDrawableSize extends TextView {

    private int mDrawableWidth;
    private int mDrawableHeight;

    public TextViewDrawableSize(Context context) {
        init(context, null, 0, 0);

    public TextViewDrawableSize(Context context, AttributeSet attrs) {
        super(context, attrs);
        init(context, attrs, 0, 0);

    public TextViewDrawableSize(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init(context, attrs, defStyleAttr, 0);

    public TextViewDrawableSize(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) {
        super(context, attrs, defStyleAttr, defStyleRes);
        init(context, attrs, defStyleAttr, defStyleRes);

    private void init(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) {
        TypedArray array = context.obtainStyledAttributes(attrs, R.styleable.TextViewDrawableSize, defStyleAttr, defStyleRes);

        try {
            mDrawableWidth = array.getDimensionPixelSize(R.styleable.TextViewDrawableSize_compoundDrawableWidth, -1);
            mDrawableHeight = array.getDimensionPixelSize(R.styleable.TextViewDrawableSize_compoundDrawableHeight, -1);
        } finally {

        if (mDrawableWidth > 0 || mDrawableHeight > 0) {

    private void initCompoundDrawableSize() {
        Drawable[] drawables = getCompoundDrawables();
        for (Drawable drawable : drawables) {
            if (drawable == null) {

            Rect realBounds = drawable.getBounds();
            float scaleFactor = realBounds.height() / (float) realBounds.width();

            float drawableWidth = realBounds.width();
            float drawableHeight = realBounds.height();

            if (mDrawableWidth > 0) {
                // save scale factor of image
                if (drawableWidth > mDrawableWidth) {
                    drawableWidth = mDrawableWidth;
                    drawableHeight = drawableWidth * scaleFactor;
            if (mDrawableHeight > 0) {
                // save scale factor of image

                if (drawableHeight > mDrawableHeight) {
                    drawableHeight = mDrawableHeight;
                    drawableWidth = drawableHeight / scaleFactor;

            realBounds.right = realBounds.left + Math.round(drawableWidth);
            realBounds.bottom = + Math.round(drawableHeight);

        setCompoundDrawables(drawables[0], drawables[1], drawables[2], drawables[3]);


    <declare-styleable name="TextViewDrawableSize">
        <attr name="compoundDrawableWidth" format="dimension"/>
        <attr name="compoundDrawableHeight" format="dimension"/>

And usage


Is it possible to scale drawableleft & drawableright in textview , This might help you out. There are two properties scaleX and scaleY. The code below will scale down the image and the text with 30%. CompoundDrawable size. Ask Question Asked 7 years, 10 months ago. Active 1 year, 3 months ago. Use this library to set size of compound drawable. Original answer.

If you scale the images in code as Drawable objects, you can then set them in code using the setCompoundDrawables() method of TextView. This will require having called setBounds() on the Drawables.

As far as I know there is no way to set the size in XML.

TextViewDrawableSize, set Compound Drawable Size Base On Height - Android Graphics. Android examples for Graphics:Drawable Size. HOME · Android · Graphics · Drawable Size  TextViewDrawableSize - CompoundDrawable size. GitHub Gist: instantly share code, notes, and snippets.

There is a solution but its not 100% satisfying since you cannot control the size directly only the relative padding with inset:


<?xml version="1.0" encoding="utf-8"?>
<inset xmlns:android=""
    <bitmap android:src="@drawable/your_icon"/>


        android:text="Your text"

Another solution would ScaleDrawables, but I guess this wont work great over different pixel densities.

set Compound Drawable Size Base On Height, Luckily, there is a partial workaround. Implementation. Scaling the entire TextView smaller will decrease the size of both the text and the drawable  The compound drawables are pretty useful, but not for all cases. The drawable has different gravity than the text itself, meaning that it won’t be close to the text when the component is wide or tall relatively to the size of the text itself. For instance, try to create a text item with left drawable, set its gravity to center and make it wide.

I know this answer is a little late but I would recommend using the quickfix: "Add ignore 'UseCompoundDrawables' to element"

This will result in something like:

    tools:ignore="UseCompoundDrawables" >  <!-- This will remove the warning -->

        android:src="@drawable/icon1" />

        android:textSize="@dimen/label_font_size" />

Use with caution and lets hope that the API or tools gets "fixed" soon.

Reducing The Size Of TextView DrawableStart / DrawableEnds , Push object to the top of its container, not changing its size. android:scaleHeight. Scale height, expressed as a percentage of the drawable's bound. The value's  Supports auto-sizing via androidx.core.widget.TextViewCompat by allowing to instruct a TextView to let the size of the text expand or contract automatically to fill its layout based on the TextView's characteristics and boundaries.

@Oleksandr answer is the best one especially if your are going to change the Drawable programmatically, but you should add :

public void setCompoundDrawablesRelativeWithIntrinsicBounds(int start, int top, int end, int bottom) {
    super.setCompoundDrawablesRelativeWithIntrinsicBounds(start, top, end, bottom);


so it will Re-Calculate whenever Coumpounds is changed.

ScaleDrawable, [xml] But how set the drawable size on image??? Reply. Insert: A Compound drawable that inserts another drawable by a specified distance Android APIs. java,  Android Platform. API level 29 28 27 26 25 24 23 22 21 20 19 18 17 16 15 14 13 12 11 10 9 8 7 6 5 4 3 2 1. Manifest.permission. Manifest.permission_group. android.accessibilityservice. AccessibilityService.MagnificationController.OnMagnificationChangedListener. AccessibilityService.SoftKeyboardController.OnShowModeChangedListener.

Compound drawable size, Lint already warns that there is a better way to do this by using a TextView and a compound drawable. What's this? Our TextView has four properties that let us  New challenge: Draw a drawable based on its state…The problem here is that I have a custom drawable that is set as a TextView compoundDrawable. How can I draw my drawable based on the pressed state (I can andle the clicks with the above manner, I just need visual indication that the drawable has been pressed) Reply

The power of TextView (Part 1): Drawables, I always thought the compound drawable (e.g. drawableLeft) was a nifty But… if you can't predict the size of the icon, things will probably get a bit uglier. Therefore you have to increase the font size with that many "sp", so that when it get re-sized (scaled) it would fit the "sp" you prefer. Example. If I set the font to 18, then 30% out of 18 is 5.4sp, so roughly, this is the value I am targeting at, because when it gets scaled, it would become 13sp

Simplifying layouts with layer-list drawables, CompoundDrawable size. Android-Lint daje mi tę wskazówkę w niektórych przypadkach: Ten tag i jego dzieci mogą zostać zastąpione przez jeden i Mieszany. TextViewRichDrawable . This is a tiny library which empowers TextView's (and its inheritors) compound drawables with size specifying, vector support and tinting.