How to adjust color of CheckMark color in android in dialog box .: android

How to adjust CheckMark color color in Android in a dialog box. By default, the default checkmark color is green. I would like to adjust it to a different color of choice

+6
android
Aug 16 '10 at 7:07
source share
3 answers

If you look at styles.xml from the android system , you will see that the style of the flag 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 look for system resources, you will see that btn_check is a portable selector with two states (on / off) with a check in green or not.
Therefore, if you want to have your own color, then here is what you should do:
- create .xml style
- identify 2 drawings to use
- create an xml file that supports the selector

You can find the full documentation described in detail in the android google doc.

+6
Aug 16 '10 at 7:28
source share

To change the check box in a dialog box with multiple choices, you need a custom adapter for your dialog to have access to the list views. Then you call the setCheckMarkDrawable method of the setCheckMarkDrawable class.

Here is an example:

Alert dialog with custom checkboxes

res/drawable file 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> 

DialogUtil.java File

 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 just use AlertDialog , then before you get the ListView , you call show , firstly, as described above.

However, if you use DialogFragment and onCreateDialog , you will get a ListView , inside onStart .

+1
Oct 25 '13 at 19:36 on
source share

You must override the styles of the widget checkboxes with modified resources. Here is a good guide to get started with styles / themes http://brainflush.wordpress.com/2009/03/15/understanding-android-themes-and-styles/

0
Aug 16 '10 at 7:10
source share



All Articles