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:
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:
Is there any way I can shrink my drawable?
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);
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().)
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)
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 =)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With