Android: How to Change Checkbox Size

Android: How to change CheckBox size?

You just need to set the related drawables and set them in the checkbox:

<CheckBox 
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="new checkbox"
android:background="@drawable/my_checkbox_background"
android:button="@drawable/my_checkbox" />

The trick is on how to set the drawables. Here's a good tutorial about this.

Changing the size of a checkbox in android

From another question here on SO:

You just need to set the related drawables and set them in the checkbox:

<CheckBox 
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="new checkbox"
android:background="@drawable/my_checkbox_background"
android:button="@drawable/my_checkbox" />

The trick is on how to set the drawables. Here's a good tutorial about
this.

EDIT: Just to make it a bit more clear, you will need these files to make the tutorial work:

CheckBoxTestActivity.java:

import android.app.Activity;
import android.os.Bundle;

public class CheckBoxTestActivity extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
}
}

main.xml:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >

<CheckBox
android:id="@+id/checkBox1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Checked CheckBox"
android:checked="true"/>

<CheckBox
android:id="@+id/checkBox2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Unchecked CheckBox" />

<CheckBox
android:id="@+id/checkBox3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@drawable/checkbox_background"
android:button="@drawable/checkbox"
android:text="New Checked CheckBox"
android:checked="true"/>

<CheckBox
android:id="@+id/checkBox4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@drawable/checkbox_background"
android:button="@drawable/checkbox"
android:text="New Unchecked CheckBox" />

</LinearLayout>

checkbox.xml:

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">

<item
android:state_checked="false"
android:drawable="@drawable/checkbox_off_background"/>

<item
android:state_checked="true"
android:drawable="@drawable/checkbox_on_background"/>

</selector>

checkbox_background.xml:

<?xml version="1.0" encoding="utf-8"?>

<selector xmlns:android="http://schemas.android.com/apk/res/android">

<item
android:drawable="@drawable/btn_check_label_background" />

</selector>

and btn_check_label_background.9.png, checkbox_off_background.png and checkbox_on_background.png from the tutorial page.

How to change the size of the button of an android checkbox?

I solved it by making the bitmaps the right size. I cannot scale it now but I tested it on several devices with varying dpi settings and it seems fine. For now.

Change the size of android Checkbox

The path for themes.xml and styles.xml is changed from the given one to android-sdk-windows\platforms\android-8\data\res\values (android-8 is for API-level 8).
The drawable btn_check.xml is available at android-sdk-windows\platforms\android-8\data\res\drawable, while the other drawable components are available at respective hdpi / mdpi folders.

2) You simply need to include the respective drawables of the checkbox in different states (i.e. focused, pressed, checked, or normal), and a drawable for the background as well (often the nine-patch file given in the link).
Include these in the respective xml to specify the looks of the checkbox as given in the link, and include that xml in the android:button and android:background attributes of your checkbox. That should do it all...! :)

how to change the border size(thickness) of checkbox in android kotlin?

<CheckBox

android:scaleX="0.70"
android:scaleY="0.70"
...
/>

Try this, above API level 11

Checkbox Size Xamarin Android

Maybe I found a possible solution, check the steps below:

  • Add a BindableProperty called SizeRequest in the custom CheckBox control.
  • Create a method GetDefaultCheckBoxDrawable to get the default CheckBox drawable.
  • Change OnElementChanged method to clear and resize the text, set the width/height based on SizeRequest, reset the button drawable and set a new Background drawable with the default checkbox drawable.

AndroidCheckboxRenderer.cs:

private Drawable GetDefaultCheckBoxDrawable(Android.Views.View view)
{
TypedValue value = new TypedValue();
view.Context.Theme.ResolveAttribute(Android.Resource.Attribute.ListChoiceIndicatorMultiple, value, true);
var origImg = view.Context.Resources.GetDrawable(value.ResourceId);
var porterDuffColor = new Android.Graphics.PorterDuffColorFilter(Element.CheckColor.ToAndroid(), Android.Graphics.PorterDuff.Mode.SrcIn);
origImg.SetColorFilter(porterDuffColor);
return origImg;
}

protected override void OnElementChanged(ElementChangedEventArgs<CustomCheckbox> e)
{
...

// CheckBox displays its height from the TEXT, as well as images.
checkBox.Text = "";
checkBox.SetTextSize(Android.Util.ComplexUnitType.Sp, 0);

// Set the width and height based on SizeRequest
if (Element.SizeRequest >= 0)
{
checkBox.SetWidth((int)Element.SizeRequest);
checkBox.SetHeight((int)Element.SizeRequest);
}

// Reset the Button Drawable
checkBox.SetButtonDrawable(null);
// Set Background Drawable with the default CheckBox
checkBox.SetBackgroundDrawable(GetDefaultCheckBoxDrawable(this));

...
}

Check out my GitHub for the full solution.

I hope this can help you.



Related Topics



Leave a reply



Submit