Tom
Tom

Reputation: 807

How do enable/disable or show/hide SharedPreferences in programs

It seems like it should be very simple to enable/disable and show/hide Preferences from within the Android program. But I can't get it to work other than directly within the XML file itself. I've only been able to find tutorials and example for deprecated preferences. Here is an example that I'm trying to get to work for hiding and disabling to two preferences below.

root_preferences.xml

<androidx.preference.PreferenceScreen xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:android="http://schemas.android.com/apk/res/android">

    <Preference
        android:key="hidePref"
        app:title="Hide/Show"
        app:summary="Change this preference's visibility"/>

    <Preference
        android:key="disablePref"
        app:title="Enable/Disable"
        app:summary="Enable or Disable this preference"/>

</androidx.preference.PreferenceScreen>
MainActivity.kt

override fun onCreate(savedInstanceState: Bundle?) {
    ...
    val sharedPreferences = Preferencemanager.getDefaultSharedPreferences(this)

    //No idea how to do the following but here are guess that obviously do not work
    sharedPreferences.edit().isVisible("hidePref", View.GONE).apply()
    sharedPreferences.edit().isEnabled("disabledPref", false).apply()
    ...
}

Upvotes: 0

Views: 594

Answers (1)

Tenfour04
Tenfour04

Reputation: 93571

SharedPreferences and Preference are two very distinct classes. A SharedPreferences is a singleton object that lets you interact with the properties file that holds your map of preferences data. A Preference is a view widget that lets the user directly modify SharedPreferences.

What you're describing you want to do is to modify visibility or enabled state of the view widget Preferences. This will not involve SharedPreferences at all.

Presumably you have your preference views on screen in a PreferenceFragmentCompat as the documentation explains. To modify a specific Preference view, you use findPreference() in your fragment. You can specify a more specific type than Preference if necessary (so long as it matches the type of the actual preference in your XML that you're looking up).

The return type is nullable because it returns null if it doesn't find it. You look up the preference using its key. So, to do what your posted code seems to be intended to do, you could put:

class MySettingsFragment: PreferenceFragmentCompat() {

    override fun onCreatePreferences(savedInstanceState: Bundle?, rootKey: String?) {
        setPreferencesFromResource(R.xml.root_preferences, null)

        findPreference<Preference>("hidepref")?.isVisible = false
        findPreference<Preference>("disabledPref")?.isEnabled = false
    }

}

I personally choose to put all my preference keys in a string resources file (and use tools:ignore="MissingTranslation" in that file to keep the linter from complaining). That way the literal strings of the keys are only typed in one place, reducing error. I write helper functions for getting these preference values from SharedPreferences or finding the Preference views using the resource ID.

Upvotes: 1

Related Questions