Devrath
Devrath

Reputation: 42824

How to set the default background for android checkbox

I have

Checkbox.setButtonDrawable(R.drawable.ic_checkbox_disabled);

Here i am setting to custom image


How to set to default check box background

I tried:

Checkbox.setButtonDrawable(null);

But entire box diseappered So I need the resource name of the checkbox android uses


Something like:

Checkbox.setButtonDrawable(R.android.resourcename);

Any solution for this

Upvotes: 0

Views: 1506

Answers (2)

Sushil Dubey
Sushil Dubey

Reputation: 686

Create a new selector file in drawable folder with name checkbox_selection :

<?xml version="1.0" encoding="utf-8"?>
    <selector xmlns:android="http://schemas.android.com/apk/res/android" >
        <item android:drawable="@drawable/checked" 
              android:state_checked="false"/>
        <item android:drawable="@drawable/uncheck" 
              android:state_checked="true"/>
        <item android:drawable="@drawable/checkbox"/>    
    </selector>

Modify your checkbox view in xml file as show below :

<CheckBox
     android:layout_width="match_parent"
     android:layout_height="wrap_content"
     android:button="@drawable/checkbox_selection"
     android:text="CheckBox"
     android:textColor="@color/Black" >
</CheckBox>

Upvotes: 4

Aman Shekhar
Aman Shekhar

Reputation: 2780

In drawable folder, make one file and name anything you want. Then paste this.

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

    <item android:drawable="@drawable/radio_checked_icon" android:state_checked="true"/>
    <item android:drawable="@drawable/radio_unchecked_icon" android:state_checked="false"/>


</selector>

radio_checked_icon and radio_unchecked_icon, these are the custom images you want to put, when your checkbox is clicked, then radio_checked_icon, else radio_unchecked_icon

and then in layout use like this:

 <CheckBox
            android:id="@+id/tiny_radio"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentEnd="true"
            android:layout_alignParentRight="true"
            android:layout_centerVertical="true"
            android:button="@drawable/name_of_your_file_present_in_Drawable"
            android:padding="@dimen/padding_short" />

Upvotes: 1

Related Questions