Question

I'm constructing a dialog with multi-choice items (checkboxes):

AlertDialog.Builder builder = new AlertDialog.Builder(this);

builder.setMultiChoiceItems(arrayResource, selectedItems, new DialogInterface.OnMultiChoiceClickListener() {
    // ...
});

AlertDialog dialog = builder.create();
dialog.show();

And I have a custom style for checkboxes:

<style name="CustomCheckBox" parent="@android:style/Widget.CompoundButton.CheckBox">
    <item name="android:button">@drawable/btn_check</item>
    <item name="android:textColor">@android:color/holo_purple</item>
</style>

It works perfectly when applied to individual checkboxes in the layout, by setting style="@style/CustomCheckBox".

But how can I apply this style to the created dialog? Or alternatively for the entire theme...

If it's somehow relevant - I'm using minSdkVersion=14 and targetSdkVersion=19.


Update:

Now according to MattMatt's answer, I'm applying a custom checkbox style to the entire theme, and also settings a custom style for dialogs:

<style name="AppTheme" parent="AppBaseTheme">
    <item name="android:checkboxStyle">@style/CustomCheckBox</item>
    <item name="android:dialogTheme">@style/CustomDialog</item>
    <item name="android:alertDialogTheme">@style/CustomDialog</item>
</style>

<style name="CustomCheckBox" parent="@android:style/Widget.CompoundButton.CheckBox">
    <item name="android:button">@drawable/btn_check</item>
    <item name="android:checkMark">@drawable/btn_check</item>
</style>

<style name="CustomDialog" parent="@android:style/Theme.Holo.Light.Dialog">
    <item name="android:checkboxStyle">@style/CustomCheckBox</item>
    <item name="android:background">#aaf</item>
</style>

Now any checkbox added to the layout gets the custom style, and I'm able to change the dialog's background, but the checkboxes in the dialog aren't affected in any way...

Was it helpful?

Solution

Actually, calling setMultiChoiceItems does not result in CheckBox widgets but CheckedTextView widgets. [Updated] It also seems that changing the value for the checkMark attribute does not have any effect. However, by changing the value of the listChoiceIndicatorMultiple attribute for the CustomDialog style, I was able to change the drawable for the choices. Like this:

<style name="CustomDialog" parent="@android:style/Theme.Holo.Light.Dialog">
    <item name="android:listChoiceIndicatorMultiple">@drawable/btn_check</item>
    <item name="android:background">#aaf</item>
</style>

I discovered this from looking at the Android source code and finding that the template used for the multiple choice items in the dialog box is this one:

https://github.com/android/platform_frameworks_base/blob/master/core/res/res/layout/select_dialog_multichoice_holo.xml

OTHER TIPS

Everything you're looking for can be found in Themes and Styles from the docs, and apply the attributes of your theme with the attributes found in Android attrs docs

To make it easy for you, follow this example:

<style name="myTheme" parent="@android:Theme.Holo">
    <!-- override default check box style with custom checkBox -->
    <item name="android:checkBoxStyle">@style/CustomCheckBox</item>

</style>
<style name="CustomCheckBox" parent="@android:style/Widget.CompoundButton.CheckBox">
<item name="android:button">@drawable/btn_check</item>
<item name="android:textColor">@android:color/holo_purple</item>

Now, as long as "myTheme" is set to Activity, you have a custom check box ;)

Hope this helps, happy coding!

you can set drawables for all checkbox states. Drawables might be pictures or shapes, so you are free to set any background that you want to have.

1> make two png files for cb checked and unchecked states as you want them and store them in drawable folder of your app.

2> now make one customdrawble.xml file as below:

<?xml version="1.0" encoding="utf-8"?>
     <selector  xmlns:android="http://schemas.android.com/apk/res/android">
     <item android:state_checked="false" android:drawable="@drawable/yourdrawable1" />
     <item android:state_checked="true" android:drawable="@drawable/yourdrawable2" />
     <item android:drawable="@drawable/yourdrawable1" /> <!-- default -->
</selector>

3> now make your checkbox as below:

<CheckBox
    android:id="@+id/chk"
    android:button=“@drawable/customdrawable”
    android:layout_alignParentLeft="true"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content" />

the solution worked for me to change the display for single checkbox

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top