Custom a CheckedTextView in android

Sergio76 picture Sergio76 · Sep 18, 2013 · Viewed 9.6k times · Source

I'm using a code that is not mine in my work. This code uses CheckedTextView, do this:

...
    AlertDialog.Builder alert;
        v.performHapticFeedback(HapticFeedbackConstants.VIRTUAL_KEY);
        switch (soundPreference.getType()) {
        case BOOLEAN:
            CheckedTextView checkedTextView = (CheckedTextView) v;
            boolean checked = !checkedTextView.isChecked();
            ((CheckedTextView) v).setChecked(checked);
            switch (soundPreference.getKey()) {
            case SOUND_ACTIVE:

                sound.setAlarmActive(checked);
                break;
...

works perfectly, the problem is that I want I want to change the checkbox graphic. Right now it looks like (defualt, I think): enter image description here

and I want to get something like this:

enter image description here

if the CheckedTextView were defined in xml, know how to do it. My problem is not being defined in xml, do not know how to do it.

I found this: Customize the CheckedTextView for ListView but I do not know how to implement it

appreciate any help.

Thank you very much in advance

Answer

Jems picture Jems · Sep 18, 2013

http://developer.android.com/reference/android/widget/CheckedTextView.html#setCheckMarkDrawable%28int%29

Example:

myCheckedTextView.setCheckMarkDrawable(R.drawable.icon);

This method allows you to set the check mark image dynamically. You should call it when setting up the CheckedTextView. The method in the developer reference just needs the id of the image resource you want to use.

There is also a version that takes a Drawable as a parameter instead of an id.