Changing the size of a checkbox in android

17,949

Solution 1

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.

Solution 2

A simple way to do it, from API Level 11 is:

<CheckBox
...
android:scaleX="0.50"
android:scaleY="0.50"
...
/>
Share:
17,949

Related videos on Youtube

Aaron
Author by

Aaron

Updated on May 25, 2022

Comments

  • Aaron
    Aaron about 2 years

    I am trying to make a checkbox smaller. I have tried playing around with layout in XML and .width()/.height in Java. Neither does anything to change the size of it. I went to the tutorial that was recommended to other that asked this questions, but I did not understand what he did. Any suggestions?

    • Brian
      Brian over 12 years
      Could you be a little bit more specific? How small do you want it?
    • Aaron
      Aaron over 12 years
      The size of the checkbox to remember your username and password on Facebook
    • Christoph
      Christoph over 11 years
  • Aaron
    Aaron over 12 years
    Tried to follow this tutorial, added the files that he added and then got lots or errors
  • Robert
    Robert over 12 years
    @Aaron: Could you be more specific about the errors that you received and the code in which you received them?
  • Aaron
    Aaron over 12 years
    Sure I added the file my_check_background in drawables. More error poped up because in my_checkbox_background started referring to other files(my_checkbox_background_on). Every time I added another file more of the same type of errors popped up.
  • Robert
    Robert over 12 years
    @Aaron: See my edits to the answer above. I gave the code that I have in Eclipse that was also tested on my phone.
  • IcyFlame
    IcyFlame about 8 years
    If your checkbox was overflowing out of the bounds before, this will simply reduce the size, won't take care of the overflow.
  • Joel
    Joel over 4 years
    I came here looking for a simple way to enlarge the check area and this works very well