Reputation: 1351
Thanks in advance for the help.
I have a checkbox that I am creating in a method call
public int myMethod(Context context, doThing) {
if(doThing){
this.checkBox = new CheckBox(context);
do some stuff...
return 1;
}else{
do something else...
return 0;
}
}
I would like to change the size of the checkbox (the actual box not the box and text combo) with a call like checkBox.setBoxSize(mySize). setBoxSize is not a native android method. Is it possible to do this, and if it is, how might I go about it?
Upvotes: 0
Views: 1922
Reputation: 12002
If you want to create your checkBox with predefined width and height you can use this method:
this.checkBox = new CheckBox(context);
this.checkBox.setLayoutParams(new LinearLayout.LayoutParams(width, height)); // or if your parent is RelativeLayout use RelativeLayout.LayoutParams(width, height)
Note, that your width and height are in pixels.
If you want to change already existing layout params, you should do something like this:
private void setBoxSize(CheckBox checkBox) {
LinearLayout.LayoutParams params = (LinearLayout.LayoutParams) checkBox.getLayoutParams(); // or RelativeLayout.LayoutParams
params.height = yourHeightInPixels;
params.width = yourWidthInPixels;
checkBox.setLayoutParams(params);
}
EDIT: In that case, if you want to change size of the checkbox's button, you could only use one of this methods
checkBox.setButtonDrawable(int resId);
checkBox.setButtonDrawable(Drawable d);
and set the right size of your drawables. Unfortunately, Android doesn't have methods to manipulate button (as you mentioned - "box") of the checkbox.
Example of the drawable, that is using as checkbox's button:
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_checked="true"
android:drawable="@drawable/ic_check_on" /> <!-- checked -->
<item android:state_checked="false"
android:drawable="@drawable/ic_check_off" />
</selector>
Upvotes: 3
Reputation: 9418
You should set the layout Params for the checkbox
like this
//width and height are in pixels
checkbox.setLayoutParams(new ViewGroup.LayoutParams(width,height));
Upvotes: 0