Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I shrink the drawable on a button?

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.

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); 
like image 545
Reto Avatar asked Sep 24 '11 08:09

Reto


People also ask

How do I change the icon size on a Drawableleft button?

You can control the size of the icon by changing scaleX and scaleY. This is useful not only in case of drawables but any place where an icon is used and where it is not easy to find styling solutions to reduce icon size like say an AlertDialogue. Show activity on this post. Best Way you can use ImageView.

How do I change drawable size on android?

Once you import svg file into Android Studio project, you are going to see <vector> resource then just change the size as you want by width , height attributes. viewportWidth and viewportHeight is to set size for drawing on virtual canvas.


2 Answers

I have found a very simple and effective XML solution that doesn't require ImageButton

Make a drawable file for your image as below and use it for android:drawableLeft

<?xml version="1.0" encoding="utf-8"?> <layer-list xmlns:android="http://schemas.android.com/apk/res/android">      <item     android:id="@+id/half_overlay"     android:drawable="@drawable/myDrawable"     android:width="40dp"     android:height="40dp"     />  </layer-list> 

You can set the image size with android:width and android:height properties.

This way you could at least get the same size for different screens.

The drawback is that it is not exactly like fitXY which would scale image width to fit X and scale image height accordingly.

like image 111
Buddy Avatar answered Oct 18 '22 14:10

Buddy


You should use a ImageButton and specify the image in android:src, and set android:scaletype to fitXY


Setting scaled drawable in code

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); //set drawableLeft for example 
like image 33
Ronnie Avatar answered Oct 18 '22 14:10

Ronnie