9

How to customize the color of the CheckMark color in android in a dialog. Currently , By default, the color of the checkmark is green by default. I would like to customize it to a different color of choice

Karthik
  • 91
  • 1
  • 1
  • 2

3 Answers3

6

If you look at the styles.xml from android system, you will see that the checkbox style is defined as follows :

<style name="Widget.CompoundButton.CheckBox">
   <item name="android:background">@android:drawable/btn_check_label_background</item>
   <item name="android:button">@android:drawable/btn_check</item>
</style>

And If you search the resources of the system, you will see that btn_check is a drawable selector with 2 states (on/off) with the check colored green or not.
So if you want to have your own color-drawable, here is what you should do :
- create a styles.xml
- define the 2 drawables to use
- create the xml file supporting the selector

You can find the full documentation quite detailled on the android google doc.

Sephy
  • 50,022
  • 30
  • 123
  • 131
  • Hi, I still could not succeed in implementing the solution. – Karthik Sep 06 '10 at 10:45
  • Can u please help me with a bit more information regarding the same. I have pasted a sample java file for the same. Please help me what exactky i need to do to to have my checkbox color to be changed to some other color instead of red. – Karthik Sep 06 '10 at 10:46
  • AlertDialog.Builder checkbox_dialog = new AlertDialog.Builder( DialogCheckBox.this); checkbox_dialog.setTitle("Main Title goes here"); checkbox_dialog.setMultiChoiceItems(myitems, choiceitems, new DialogInterface.OnMultiChoiceClickListener() { public void onClick(DialogInterface dialog, int which, boolean isChecked) { Log.d("TAG","items getting selected"); } }); – Karthik Sep 06 '10 at 10:48
  • 2
    Please edit your first post with this code, this will help next users to find answers to their questions more easily and we will also have a better display with the code styling. You should add your java code, the xml file of your selector to see if there is something wrong. – Sephy Sep 06 '10 at 21:25
1

To change the checkbox inside a multi-choice dialog, you need a custom adapter for your dialog, so as to have access to the views of the list. Then, you call method setCheckMarkDrawable of class CheckedTextView.

Here is an example:

Alert dialog with custom checkboxes

File default_checkbox.xml inside res/drawable

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

    <item android:state_checked="true"
        android:drawable="@drawable/checkbox_checked" /> <!-- checked -->

    <item android:state_pressed="true"
        android:drawable="@drawable/checkbox_checked" /> <!-- pressed -->

    <item android:drawable="@drawable/checkbox_default" /> <!-- default -->

</selector>

File DialogUtil.java

package example.dialog;

import android.app.AlertDialog;
import android.content.Context;
import android.util.Log;
import android.view.*;
import android.widget.*;
import android.widget.AdapterView.OnItemClickListener;

public class DialogUtil {

    private DialogUtil() {
    }

    public static AlertDialog show(Context context) {
        String[] items = {"text 1", "text 2", "text 3"};
        AlertDialog.Builder builder = new AlertDialog.Builder(context);
        builder.setTitle("Test")
            .setPositiveButton("OK", null)
            .setAdapter(new CustomAdapter(context, items), null);
        AlertDialog dialog = builder.show();

        ListView list = dialog.getListView();
        list.setItemsCanFocus(false);
        list.setChoiceMode(ListView.CHOICE_MODE_MULTIPLE);
        list.setOnItemClickListener(listener);
        return dialog;
    }

    private static OnItemClickListener listener = new OnItemClickListener() {

        @Override
        public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
            Log.i("DialogUtil", "Clicked on " + view);
        }
    };

    private static class CustomAdapter extends ArrayAdapter<String> {

        public CustomAdapter(Context context, String[] array) {
            super(context, android.R.layout.simple_list_item_multiple_choice, array);
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            View view = super.getView(position, convertView, parent);
            if (view instanceof CheckedTextView) {
                CheckedTextView checkedView = (CheckedTextView) view;
                checkedView.setCheckMarkDrawable(R.drawable.default_checkbox);
            }
            return view;
        }
    }
}

NOTE: If you simply use AlertDialog, then before getting the ListView, you call show, firstly, like explained above.

However, if you use DialogFragment and onCreateDialog, then you get the ListView, inside onStart.

KitKat
  • 1,495
  • 14
  • 15
0

You have to override checkbox widget styles with modified drawable resources. Here is good guide to start with styles/themes http://brainflush.wordpress.com/2009/03/15/understanding-android-themes-and-styles/

Konstantin Burov
  • 68,980
  • 16
  • 115
  • 93