I found a way to do it without creating your own images. In other words, the system image is being scaled. I don't pretend that the solution is perfect; if anyone knows a way to shorten some of the steps, I'll be happy to find out how.
First, I put the following in the main activity class of the project (WonActivity) . This was taken directly from Stack Overflow -- thank you guys!
/** get the default drawable for the check box */
Drawable getDefaultCheckBoxDrawable()
{
int resID = 0;
if (Build.VERSION.SDK_INT <= 10)
{
// pre-Honeycomb has a different way of setting the CheckBox button drawable
resID = Resources.getSystem().getIdentifier("btn_check", "drawable", "android");
}
else
{
// starting with Honeycomb, retrieve the theme-based indicator as CheckBox button drawable
TypedValue value = new TypedValue();
getApplicationContext().getTheme().resolveAttribute(android.R.attr.listChoiceIndicatorMultiple, value, true);
resID = value.resourceId;
}
return getResources().getDrawable(resID);
}
Second, I created a class to "scale a drawable". Please notice that it is completely different from the standard ScaleDrawable.
import android.graphics.drawable.*;
/** The drawable that scales the contained drawable */
public class ScalingDrawable extends LayerDrawable
{
/** X scale */
float scaleX;
/** Y scale */
float scaleY;
ScalingDrawable(Drawable d, float scaleX, float scaleY)
{
super(new Drawable[] { d });
setScale(scaleX, scaleY);
}
ScalingDrawable(Drawable d, float scale)
{
this(d, scale, scale);
}
/** set the scales */
void setScale(float scaleX, float scaleY)
{
this.scaleX = scaleX;
this.scaleY = scaleY;
}
/** set the scale -- proportional scaling */
void setScale(float scale)
{
setScale(scale, scale);
}
// The following is what I wrote this for!
@Override
public int getIntrinsicWidth()
{
return (int)(super.getIntrinsicWidth() * scaleX);
}
@Override
public int getIntrinsicHeight()
{
return (int)(super.getIntrinsicHeight() * scaleY);
}
}
Finally, I defined a checkbox class.
import android.graphics.*;
import android.graphics.drawable.Drawable;
import android.widget.*;
/** A check box that resizes itself */
public class WonCheckBox extends CheckBox
{
/** the check image */
private ScalingDrawable checkImg;
/** original height of the check-box image */
private int origHeight;
/** original padding-left */
private int origPadLeft;
/** height set by the user directly */
private float height;
WonCheckBox()
{
super(WonActivity.W.getApplicationContext());
setBackgroundColor(Color.TRANSPARENT);
// get the original drawable and get its height
Drawable origImg = WonActivity.W.getDefaultCheckBoxDrawable();
origHeight = height = origImg.getIntrinsicHeight();
origPadLeft = getPaddingLeft();
// I tried origImg.mutate(), but that fails on Android 2.1 (NullPointerException)
checkImg = new ScalingDrawable(origImg, 1);
setButtonDrawable(checkImg);
}
/** set checkbox height in pixels directly */
public void setHeight(int height)
{
this.height = height;
float scale = (float)height / origHeight;
checkImg.setScale(scale);
// Make sure the text is not overlapping with the image.
// This is unnecessary on Android 4.2.2, but very important on previous versions.
setPadding((int)(scale * origPadLeft), 0, 0, 0);
// call the checkbox's internal setHeight()
// (may be unnecessary in your case)
super.setHeight(height);
}
}
That's it. If you put a WonCheckBox in your view and apply setHeight(), the check-box image will be of the right size.
Here is a better solution which does not clip and/or blur the drawable, but only works if the checkbox doesn't have text itself (but you can still have text, it's just more complicated, see at the end).
<CheckBox
android:id="@+id/item_switch"
android:layout_width="160dp" <!-- This is the size you want -->
android:layout_height="160dp"
android:button="@null"
android:background="?android:attr/listChoiceIndicatorMultiple"/>
The result:
What the previous solution with scaleX and scaleY looked like:
You can have a text checkbox by adding a TextView beside it and adding a click listener on the parent layout, then triggering the checkbox programmatically.
Well i have found many answer, But they work fine without text when we require text also with checkbox like in my UI
Here as per my UI requirement i can't not increase TextSize so other option i tried is scaleX and scaleY (Strach the check box) and custom xml selector with .png Images(its also creating problem with different screen size)
But we have another solution for that, that is Vector Drawable
Do it in 3 steps.
Step 1: Copy these three Vector Drawable to your drawable folder
(Note if you are workiong with Android Studio, you can also add these Vector Drawable from there, Right click on your drawable folder then New/Vector Asset, then select these drawable from there)
<CheckBox
android:id="@+id/suggectionNeverAskAgainCheckBox"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center_vertical"
android:button="@drawable/check_box_selector"
android:textColor="#FF000000"
android:textSize="13dp"
android:text=" Never show this alert again" />
Now its like:
You can change its width and height or viewportHeight and viewportWidth
and fillColor also
You can try the following solution to change the size of custom checkbox by setting the following properties to Checkbox in your layout file. Worked for me
then simply remove android:button="@drawable/xml_above" in your checkbox xml, and do drawable scaling programmatically in java (decrease the 150 big size to your desired dp):
I could not find the relevant answer for my requirement which I figured it out. So, this answer is for checkbox with text like below where you want to resize the checkbox drawable and text separately.
You need two PNGs cb_checked.png and cb_unchechecked.png add them to drawable folder
Now create cb_bg_checked.xml
<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:tools="http://schemas.android.com/tools"
xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="@drawable/cb_checked"
android:height="22dp" <!-- This is the size of your checkbox -->
android:width="22dp" <!-- This is the size of your checkbox -->
android:right="6dp" <!-- This is the padding between cb and text -->
tools:targetApi="m"
tools:ignore="UnusedAttribute" />
</layer-list>
And, cb_bg_unchecked.xml
<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools">
<item android:drawable="@drawable/cb_unchechecked"
android:height="22dp" <!-- This is the size of your checkbox -->
android:width="22dp" <!-- This is the size of your checkbox -->
android:right="6dp" <!-- This is the padding between cb and text -->
tools:targetApi="m"
tools:ignore="UnusedAttribute" />
</layer-list>
<CheckBox
android:id="@+id/checkbox_with_text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:checked="true"
android:button="@drawable/checkbox"
android:text="This is text"
android:textColor="@color/white"
android:textSize="14dp" /> <!-- Here you can resize text -->