Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Custom checkbox style in dialog

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...

like image 439
yprez Avatar asked Jan 25 '14 19:01

yprez


2 Answers

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

like image 83
mikeplate Avatar answered Nov 16 '22 17:11

mikeplate


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!

like image 3
MattMatt Avatar answered Nov 16 '22 16:11

MattMatt