Activate and deactivate preference

I need an example tutorial on how to activate and deactivate an element in activity preferences.

For example, in the figure below, when the Wi-Fi check box is not selected, I can’t touch the “Network notification” check box and it turns gray when the Wi-Fi check box is checked, then I can touch another.

Also, how can I fill in the Add Wi-Fi network tab when the whi-fi checkbox is on?

enter image description here

+5
source share
3 answers

We need to add the preferences.xml file in the preference, which depends on another preference: android code: dependency = "".

For instance:

        <CheckBoxPreference
            android:key="checkBox"
            android:summary="On/Off"
            android:title="Enable" />

        <ListPreference
            android:entries="@array/listOptions"
            android:entryValues="@array/listValues"
            android:key="listpref"
            android:summary="List preference example"
            android:title="List preference"
            android:dependency="checkBox" />
+16
source

OnSharedPreferenceChangeListener. .

onResume() onSharedPreferenceChanged , , .

@Override
public void onSharedPreferenceChanged(SharedPreferences sharedPreferences,
        final String key) {
    if (key.equals(PREFERENCE_KEY)) {
        // handle setting enabled or disabled depending on value of preference
        if (sharedPreferences.getBoolean(key, false)) {
            // prefField.setenabled(true);
        } else {
            // prefField.setenabled(false);
        }

    }
}

PreferenceCategory, .

+6

When the preference activity starts with the onResume method, check the status of the wifi connection or whatever you want, and enable / disable the corresponding settings.

It may seem like this simple example to give you a general idea.

@Override
protected void onResume() {
   super.onResume();
   boolean isConnected = getConnectionStatus();

   if(isConnected) {
     connPreference.setEnabled(false);
   } else {
     connPreference.setEnabled(true);
   }
}
0
source

All Articles