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:

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

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:

TextViewDrawableSize.java

public class TextViewDrawableSize extends TextView {

    private int mDrawableWidth;
    private int mDrawableHeight;

    public TextViewDrawableSize(Context context) {
        super(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);
    }

    @TargetApi(Build.VERSION_CODES.LOLLIPOP)
    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 {
            array.recycle();
        }

        if (mDrawableWidth > 0 || mDrawableHeight > 0) {
            initCompoundDrawableSize();
        }
    }

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

            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 = realBounds.top + Math.round(drawableHeight);

            drawable.setBounds(realBounds);
        }
        setCompoundDrawables(drawables[0], drawables[1], drawables[2], drawables[3]);
    }
}

attrs.xml

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

And usage

    <com.alt.view.TextViewDrawableSize
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:drawableLeft="@drawable/logo"
        android:drawableStart="@drawable/logo"
        android:drawablePadding="@dimen/space_micro"
        app:compoundDrawableHeight="@dimen/small_logo_height"
        app:compoundDrawableWidth="@dimen/small_logo_width"
        />

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:

background.xml

<?xml version="1.0" encoding="utf-8"?>
<inset xmlns:android="http://schemas.android.com/apk/res/android"
       android:insetBottom="4dp"
       android:insetTop="4dp"
       android:insetLeft="4dp"
       android:insetRight="4dp">
    <bitmap android:src="@drawable/your_icon"/>
</inset>

layout.xml

<TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Your text"
        android:drawableLeft="@drawable/background"
        android:drawablePadding="8dp"
        />

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:

<LinearLayout
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:orientation="vertical"
    tools:ignore="UseCompoundDrawables" >  <!-- This will remove the warning -->

    <ImageView
        android:layout_width="@dimen/icon_width"
        android:layout_height="@dimen/icon_height"
        android:contentDescription="@string/item1"
        android:src="@drawable/icon1" />

    <TextView
        android:text="@string/item1"
        android:textSize="@dimen/label_font_size" />
</LinearLayout>

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 :

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

}

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.