2

edit:

for anyone coming across this, the way I got it to work is to either:

Set the layout directly for each preference item, as in:

<CheckboxPreference
    android:layout="@layout/checkbox_preference_item"/>

OR set the style via a theme as I was doing before, but define a widgetlayout and set it instead of an actual layout, like so, where preferenceStyle is set to @style/Preference and checkBoxPreferenceStyle is set to @style/Preference.Checkbox:

<Style name="Preference">
    <item name="android:layout">@layout/checkbox_preference_item</item>
</Style>

<Style name="Preference.Checkbox">
    <item name="android:widgetLayout">@layout/your_custom_widget</item>
</Style>

I'm trying to customize the layout of a Checkbox Preference item and the checkbox itself is not showing up. I'm modeling after android code and not sure where I'm going wrong. The text views are working fine as well as the rest of the layout but the checkbox is not showing up. Would appreciate any help with this.

Relevant code:

theme.xml:

<style name="Theme.Settings" parent="android:Theme.Holo.Light">
    <item name="android:actionBarStyle">@style/SettingsActionBarStyle</item>
    <item name="android:windowActionBarOverlay">true</item>
    <item name="android:windowContentOverlay">@null</item>
    <item name="android:preferenceCategoryStyle">@style/PreferenceCategory</item>
    <item name="android:preferenceStyle">@style/Preference</item>
    <item name="android:checkBoxPreferenceStyle">@style/Checkbox</item>
</style>

style.xml

<style name="Checkbox">
    <item name="android:layout">@layout/checkbox_preference_item</item>
</style>

<style name="Preference.Text">
    <item name="android:layout_height">wrap_content</item>
    <item name="android:layout_width">wrap_content</item>
    <item name="android:fontFamily">sans-serif-light</item>
    <item name="android:textSize">@dimen/preferences_item_font</item>
    <item name="android:textColor">@color/preference_text</item>
</style>

<style name="Preference.Text.Extra" parent="Preference.Text">
    <item name="android:textSize">@dimen/preferences_header_font</item>
</style>

checkbox_preference_item.xml

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
                android:layout_width="match_parent"
                android:layout_height="@dimen/preferences_item_height"
                android:background="@color/white"
                android:paddingLeft="@dimen/preferences_item_left">
    <LinearLayout android:layout_width="wrap_content"
            android:orientation="vertical"
            android:layout_height="match_parent"
            android:layout_alignParentLeft="true"
            android:gravity="center_vertical">
         <TextView android:id="@android:id/title"
                style="@style/Preference.Text"/>
         <TextView android:id="@android:id/summary"
                style="@style/Preference.Text.Extra"/>
    </LinearLayout>
    <LinearLayout
            android:layout_width="wrap_content"
            android:layout_height="match_parent"
            android:orientation="vertical"
            android:visibility="visible"
            android:id="@android:id/widget_frame"
            android:layout_alignParentRight="true"/>
</RelativeLayout>
sherpya
  • 4,890
  • 2
  • 34
  • 50
Ian
  • 45
  • 7

1 Answers1

-1

I am not seeing a CheckBox in the layout xml. Am I missing something? You need to specify something like:

<CheckBox android:id="..." ... />

Then you may need to specify something like

<CheckBoxPreference android:key="..." ... />

in the preference xml file. Setting should be android:widgetLayout to the custom CheckBox.

See this post may help you more customizing checkbox preference

Community
  • 1
  • 1
Wildroid
  • 864
  • 7
  • 9
  • I was under the impression that the checkbox would be automatically added to the widget_frame linearlayout, modeled after this: https://github.com/android/platform_frameworks_base/blob/master/core/res/res/layout/preference.xml I'm not actually trying to customize the checkbox itself, just the container that will hold it – Ian Oct 15 '14 at 16:28
  • In that case - See [link](http://stackoverflow.com/questions/7094584/checkboxpreference-with-own-layout) - I think the layout_height may be limiting the visibility of checkbox - – Wildroid Oct 15 '14 at 19:24
  • weird...if I set the layout in the settings.xml for the preference item directly, as in that link, it works, but if I just try to set the style for all preference items (so I don't have to do this for every single one) it doesn't work. Thanks though! – Ian Oct 15 '14 at 19:50