How to change images for Checkbox

from the CommonsWare Community archives

At August 29, 2019, 7:41am, Ven asked:

I have two images which are used when check box is checked and unchecked , how do I do this ?
for example,Image1 is displayed in place of checkbox (Unchecked state) after it is checked Image2 will be displayed

At August 29, 2019, 10:46am, mmurphy replied:

First, add Image1 and Image2 as drawable resources.

Next, create a <selector> resource that ties those drawables to particular “checked” states:

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="">
    <item android:drawable="@drawable/ic_terms_checkbox_checked" android:state_checked="true" />
    <item android:drawable="@drawable/ic_terms_checkbox_unchecked" android:state_checked="false" />

(here, ic_terms_checkbox_checked and ic_terms_checkbox_unchecked are my two images)

Then, use that <selector> drawable as the background of your CheckBox:

    android:button="@drawable/ic_terms_checkbox" />

(here, ic_terms_checkbox is my <selector> drawable resource)

At August 30, 2019, 1:52pm, Pvr replied:

Thank you
I also want to know that ho w to keep text after (next to ) custom check box image

At August 30, 2019, 2:03pm, mmurphy replied:

The location and positioning of the text is part of the CheckBox implementation. AFAIK, you do not have control over that.

At August 30, 2019, 4:13pm, Pvr replied:

After taking checkbox image from selector file , the text given to check box getting part of image , I want to seperate both side by side . Is that possible or I need to create a textview for that

At August 30, 2019, 9:55pm, mmurphy replied:

If I understand your problem… you could try playing with android:drawablePadding and see if it affects the relative positioning of the CheckBox image and text. But, if that does not work, setting the CheckBox text to "" and having a separate TextView should work.

At September 3, 2019, 5:02pm, Pvr replied:

Thank you,
Is it possible to make check box as checked by clicking the Text (Text that is written next to checkbox image)

At September 3, 2019, 9:28pm, mmurphy replied:

Only if you do that yourself, by adding an OnClickListener to the TextView and having it toggle the CheckBox.

At September 4, 2019, 9:06am, Pvr replied:

Thank you,
It is working.

At September 4, 2019, 9:54am, Pvr replied:

I have .ttf file for font view , how to apply this file from java code to change font view

At September 4, 2019, 11:02am, mmurphy replied:

You could set up a font resource and use that font in your app.

Or, use a third-party library for that.

Or, use Typeface along with setTypeface() on TextView.

At September 4, 2019, 1:14pm, Pvr replied:

While doing Espresso testing i’m facing issue in Gradle file, How to resolve it

At September 4, 2019, 9:10pm, mmurphy replied:

I have no idea, sorry.

At September 5, 2019, 5:32pm, Pvr replied:

1)I want to know how to perform espresso test for colour change of a button when it is clicked
2)how to Test custom library using Espresso or Junit ( same as app or it has different approach)

At September 5, 2019, 11:39pm, mmurphy replied:

My guess is that you will need to write a custom ViewAssertion for that. See this chapter from The Busy Coder’s Guide to Android Development for an example.

In terms of JUnit, there is no significant difference that I can think of.

In terms of Espresso, I have not needed to do that, so I do not know what might be different, sorry.

At September 11, 2019, 9:30am, Pvr replied:

I would like to know the process for creating Tiles on screen
and i want to provide clickable property for each tile .

At September 11, 2019, 10:52am, mmurphy replied:

Sorry, but I do not know what “tiles” means in this context. I also do not know what “clickable property” means in this context.