how can I make the drawable on a button smaller? The icon is too big, actually higher than the button. This is the code I am using:
<Button
android:background="@drawable/red_button"
android:drawableLeft="@drawable/s_vit"
android:id="@+id/ButtonTest"
android:gravity="left|center_vertical"
android:text="S-SERIES CALCULATOR"
android:textColor="@android:color/white"
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:layout_marginLeft="25dp"
android:layout_marginRight="25dp"
android:drawablePadding="10dp">
</Button>
The upper is how it should look, the lower how it looks right now.
I tried this but there is no image displayed. :-(
Resources res = getResources();
ScaleDrawable sd = new ScaleDrawable(res.getDrawable(R.drawable.s_vit), 0, 10f, 10f);
Button btn = (Button) findViewById(R.id.ButtonTest);
btn.setCompoundDrawables(sd.getDrawable(), null, null, null);
If you want to use 1 image and display it in different size, you can use scale drawable ( http://developer.android.com/guide/topics/resources/drawable-resource.html#Scale ).
You can use different sized drawables that are used with different screen densities/sizes, etc. so that your image looks right on all devices.
See here: http://developer.android.com/guide/practices/screens_support.html#support
You should use a ImageButton and specify the image in
android:src
, and setandroid:scaletype
tofitXY
Setting scaled drawable in code
You can call
setBounds
on the "compound" drawables to modify the size of the image.Try this code to autosize the drawable of your button:
defined by this function:
Be aware, that this may not be called in
onCreate()
of an activity, because button height and width are not (yet) available there. Call this in ononWindowFocusChanged()
or use this solution to call the function.Edited:
The first incarnation of this function did not work correctly. It used userSeven7s code to scale the image, but returning
ScaleDrawable.getDrawable()
does not seem to work (neither does returningScaleDrawable
) for me.The modified code uses
setBounds
to provide the bounds for the image. Android fits the image into these bounds.Buttons do not resize their inner images.
My solution does not require code manipulation.
It uses a layout with TextView and ImageView.
The background of the layout should have the red 3d drawable.
You may need to define the android:scaleType xml attribute.
Example:
BTW:
Good luck
Did you try wrapping your image in a ScaleDrawable and then using it in your button?