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
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.
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:
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
.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With