Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Scaling a drawable inside a button?

Currently my drawable just scales to it's normal size, I want it to fit inside my button. Here's a picture of how it looks now:

enter image description here

Here is the xml for the button:

<LinearLayout android:layout_width="wrap_content"
    android:id="@+id/linearLayout2" android:layout_height="40dp"
    android:layout_weight="0.4" android:gravity="right|center_vertical"
    android:paddingRight="5dp">
    <Button android:id="@+id/button1" android:background="@drawable/refresh"
        android:layout_height="25dp" android:layout_width="150dp"
        android:text="@string/buttonSearchAgain" android:drawableRight="@drawable/refresh_48"></Button>
</LinearLayout>

So I want it to look like this:

enter image description here

Is there any way I can shrink my drawable?

like image 296
soren.qvist Avatar asked Jul 02 '11 19:07

soren.qvist


4 Answers

I think I found the solution for your problem, very late but it might help others

Drawable drawable = getResources().getDrawable(R.drawable.s_vit);
drawable.setBounds(0, 0, (int)(drawable.getIntrinsicWidth()*0.5), 
                     (int)(drawable.getIntrinsicHeight()*0.5));
ScaleDrawable sd = new ScaleDrawable(drawable, 0, scaleWidth, scaleHeight);
Button btn = findViewbyId(R.id.yourbtnID);
btn.setCompoundDrawables(sd.getDrawable(), null, null, null); 
like image 103
Ravi Avatar answered Nov 15 '22 11:11

Ravi


You can solve this programmatically: Have a look at the function scaleButtonDrawables in this answer. With that function you can write in onCreate() of your activity:

final Button button = (Button) findViewById(R.id.button1);
ViewTreeObserver vto = button.getViewTreeObserver();
vto.addOnPreDrawListener(new ViewTreeObserver.OnPreDrawListener() {
            public boolean onPreDraw() {
                button.getViewTreeObserver().removeOnPreDrawListener(this);
                //scale to 90% of button height
                DroidUtils.scaleButtonDrawables(button, 0.9);
                return true;
            }
        });

(You cannot call this directly because button height is not available in onCreate().)

like image 20
yonojoy Avatar answered Nov 15 '22 13:11

yonojoy


Since you cannot modify or scale the image over xml you need to set the compound drawable bounds within the code.

Override the Button class with your own "CustomButton". Then override the method "SetCompoundDrawables(left, top, right, bottom):

    public override void SetCompoundDrawables(Drawable left, Drawable top, Drawable right, Drawable bottom)
    {
        if (right != null)
        {
            var bounds = right.Bounds;
            right.SetBounds(bounds.Left, bounds.Top, bounds.Right, bounds.Bottom - 70);
        }

        if (left != null)
        {
            var bounds = left.Bounds;
            left.SetBounds(bounds.Left, bounds.Top, bounds.Right, bounds.Bottom - 70);
        }

        base.SetCompoundDrawables(left, top, right, bottom);
    }

Please be aware that this is C# Code since I am using Xamarin. But the java code should be the same (despite of some uppper case namings)

like image 2
Michael Probst Avatar answered Nov 15 '22 11:11

Michael Probst


Recreate the image. Keep image size in pixels but reduce the size of the arrow and make the rest transparent. That's my bet anyway =)

like image 1
Emir Kuljanin Avatar answered Nov 15 '22 13:11

Emir Kuljanin