How can I shrink the drawable on a button?
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
how to change a drawableLeft icon size on a button?
Wrap your resource in a drawable that defines your desired size similar to:
<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android" >
<item
android:drawable="@drawable/icon"
android:width="@dimen/icon_size"
android:height="@dimen/icon_size"
/>
</layer-list >
After that, use this drawable in your android:drawableLeft
tag
Changing the drawable size inside a button
Solution 1:
Adding a drawable to a button has very limited functions. You can't change the drawable size, so the best way to fix it is to add a button with an image view beside it in a linear layout like this:
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:background="@drawable/ronaldo"
android:orientation="horizontal">
<Button
android:id="@+id/button"
android:layout_width="wrap_content"
android:layout_height="80dp"
android:layout_marginEnd="10dp"
android:layout_marginRight="10dp"
android:background="@null"
android:text="Play Game"
android:textColor="#ff0000"
android:textStyle="italic" />
<ImageView
android:id="@+id/imageView"
android:layout_width="wrap_content"
android:layout_height="80dp"
android:src="@drawable/messi" />
</LinearLayout>
Solution 2:
You can also do the following if you prefer this one, but you will have to add onClickListener() for each one of them:
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:background="@drawable/ronaldo"
android:orientation="horizontal">
<Button
android:id="@+id/button"
android:layout_width="wrap_content"
android:layout_height="80dp"
android:background="@null"
android:text="Play Game"
android:textColor="#ff0000"
android:textStyle="italic" />
<ImageButton
android:id="@+id/imageButton"
android:layout_width="wrap_content"
android:layout_height="80dp"
android:src="@drawable/messi" />
</LinearLayout>
Solution 3:
As you have suggested in the comments, you would do something like this:
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="80dp"
android:layout_gravity="center"
android:background="@drawable/ronaldo">
<Button
android:id="@+id/button"
android:layout_width="wrap_content"
android:layout_height="80dp"
android:layout_alignLeft="@+id/buttonLayout"
android:layout_alignRight="@+id/buttonLayout"
android:background="@null" />
<LinearLayout
android:id="@+id/buttonLayout"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal">
<TextView
android:id="@+id/textView"
android:layout_width="wrap_content"
android:layout_height="80dp"
android:gravity="center"
android:text="Play Game"
android:textAppearance="?android:attr/textAppearanceMedium"
android:textColor="#ff0000"
android:textStyle="italic" />
<ImageView
android:id="@+id/imageView"
android:layout_width="wrap_content"
android:layout_height="80dp"
android:src="@drawable/messi" />
</LinearLayout>
</RelativeLayout>
Scaling a drawable inside a button?
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 =)
How to reduce the size of the button drawable on an Android RadioButton
After banging my head against a wall forever on this, I decided that I'd try making my own custom radio button view. And as it turns out, what I needed ended up being super simple! First, I made a new class, called something like ImageRadioButton, then extended AppCompatRadioButton. I only needed to override the onMeasure
method and read the attempted width of the radio button, and set the height to the aspect ratio of the width. Something like this:
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
int desiredWidth = MeasureSpec.getSize(widthMeasureSpec);
Drawable drawable = getBackground();
float aspectRatio = (float)drawable.getIntrinsicHeight() / (float)drawable.getIntrinsicWidth();
setMeasuredDimension(desiredWidth, (int)(desiredWidth * aspectRatio));
}
With the images I'm using set as the background of the radiobutton, the text empty, and button null, this works exactly as I needed, without having to make new drawables of different sizes.
This new radio button won't work for vertically aligned radio buttons, unless their widths are constrained, and since the background is taken up by the graphic, it can't have a separate background. It'll also look terrible with text in it, but this works for me for now.
How to decrease radio button drawable size?
I can't find a solution.
But I find a another solution.
xml source
<RadioButton
android:id="@+id/fragBtn1"
android:layout_width="0dp"
android:layout_height="fill_parent"
android:layout_weight="1"
android:background="@drawable/btn_bg_selector"
android:button="@null"
android:drawableLeft="@drawable/btn_selector" />
MyActivity.java
@Override
public void onWindowFocusChanged(boolean hasFocus) {
super.onWindowFocusChanged(hasFocus);
BitmapDrawable bd = (BitmapDrawable) this.getResources().getDrawable(R.drawable.mybtn);
int iconWidth = bd.getBitmap().getWidth();
int leftPadding = (btn.getWidth() / 2) - (iconWidth / 2);
btn.setPadding(leftPadding, 0, 0, 0);
}
Scale drawableLeft in button with text
You can resize an image on basis of the dimension of a Button, use getHeight()
and getWidth()
methods to get the size of the button and use following function to resize image for Button:
Resizing a Bitmap:
public Bitmap getResizedBitmap(Bitmap bm, int newHeight, int newWidth) {
int width = bm.getWidth();
int height = bm.getHeight();
float scaleWidth = ((float) newWidth) / width;
float scaleHeight = ((float) newHeight) / height;
// create a matrix for the manipulation
Matrix matrix = new Matrix();
// resize the bit map
matrix.postScale(scaleWidth, scaleHeight);
// recreate the new Bitmap
Bitmap resizedBitmap = Bitmap.createBitmap(bm, 0, 0, width, height, matrix, false);
return resizedBitmap;
}
Now use this resized image for your button.
Reference
Reduce custom button size
Just specify the desired height and width of the button in your layout XML. i.e
<Button
android:layout_width="50dp"
android:layout_height="50dp"
android:id="@+id/start_button"
android:background="@drawable/myrect"
android:focusable="false"
android:text="Start"
android:layout_below="@+id/expandview"
android:layout_centerHorizontal="true" />
If you want to do it in drawable.... then
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle">
<solid android:color="#f50057" />
<padding
android:left="30dp"
android:top="0dp"
android:right="30dp"
android:bottom="0dp" />
<corners android:radius="5dp" />
<size
android:height="50dp"
android:width="50dp" />
</shape>
Related Topics
What's Oncreate(Bundle Savedinstancestate)
Add "View More" at the End of Textview After 3 Lines
Android App Crashes When Launched in Debug Mode
Determine Addaction Click for Android Notifications
Sniffing/Logging Your Own Android Bluetooth Traffic
Actionbarsherlock + Tabs + Multi Fragments
How to Make My Android App Appear in the Share List of Another Specific App
How to Put Media Controller Button on Notification Bar
Location Needs to Be Enabled for Bluetooth Low Energy Scanning on Android 6.0
Device Not Detected in Eclipse When Connected with Usb Cable
How to Create a Resizable Rectangle with User Touch Events on Android
Android: Android.Content.Res.Resources$Notfoundexception: String Resource Id #0X5
Manually Put Files to Android Emulator Sd Card
How to Pass a View's Onclick Event to Its Parent on Android