Search code examples
androidxmlcheckboxpreferencepreferencefragment

Android checkboxpreference disabled even though it's enabled in XML


I currently have the problem of a disabled checkbox preference which is enabled in the preference.xml. This happened after the migration to Metarial design. I tried to enable the view programmatically but it didn't help.

(I'd like to show a screenshot but my rep is not high enough yet :/)

my preference.xml

<?xml version="1.0" encoding="utf-8"?>
<PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android" >

    <PreferenceCategory android:title="Network Service" >

        <CheckBoxPreference
            android:enabled="true"
            android:key="@string/communication_option"
            android:selectable="true"
            android:defaultValue="true"
            android:shouldDisableView="false"
            android:summary="Allows down- and upload."
            android:title="Enable server communication" />

        <CheckBoxPreference
            android:disableDependentsState="false"
            android:enabled="true"
            android:dependency="@string/communication_option"
            android:key="@string/wifi_sync_option"
            android:selectable="true"
            android:defaultValue="true"
            android:shouldDisableView="true"
            android:summary="Do not use mobile bandwidth to download"
            android:title="WiFi synchronization only" />

    </PreferenceCategory>
</PreferenceScreen>

my preferenceFragment

public class FragmentPreference extends PreferenceFragment implements
    SharedPreferences.OnSharedPreferenceChangeListener {


@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    // Load the preferences from an XML resource
    addPreferencesFromResource(R.xml.preferences);

    // added to make sure it really is enabled
    CheckBoxPreference cbp = (CheckBoxPreference) getPreferenceManager()
            .findPreference(getString(R.string.communication_option));
    cbp.setEnabled(true);
    cbp.setShouldDisableView(false);

}

I am using the Theme.AppCompat.Light.NoActionBar theme to be able to add the new toolbar.

An other thing i noticed is, that the preference is shown as enabled it the screen orientation changes to landscape and even though the preference is shown as disabled the preference can still be clicked/checked/unchecked.


Solution

  • After a few trial and error I found out that I just overloaded the Preferences. The slimmer and much cleaner version that works looks like this:

    <PreferenceCategory android:title="Network Service" >
    
        <CheckBoxPreference
            android:key="@string/communication_option"
            android:defaultValue="true"
            android:summary="Allows down- and upload."
            android:title="Enable server communication" />
    
        <CheckBoxPreference
            android:dependency="communication_option"
            android:key="@string/wifi_sync_option"
            android:defaultValue="true"
            android:summary="Do not use mobile bandwidth to download"
            android:title="WiFi synchronization only" />
    
    </PreferenceCategory>
    

    and

    public class FragmentPreference extends PreferenceFragment implements
                SharedPreferences.OnSharedPreferenceChangeListener {     
    
            @Override
            public void onCreate(Bundle savedInstanceState) {
                super.onCreate(savedInstanceState);
    
                // Load the preferences from an XML resource
                addPreferencesFromResource(R.xml.preferences);
            }
    }