How to intercept Preference modifications in android

35 Views Asked by At

I have an EditTextPreference and I want the user to check if the current conditions for modification are met when clicking on it. If it doesn't, a dialog box will pop up, otherwise the modification will be allowed.

As usual, I overloaded its onPreferenceClickListener variable to check the condition in it. Return false when the condition is not met.

But whether I return false or true, an edit dialog pops up to edit the EditTextPreference.

class FirstFragment : PreferenceFragmentCompat() {
    override fun onCreatePreferences(savedInstanceState: Bundle?, rootKey: String?) {
        setPreferencesFromResource(R.xml.main_preference, rootKey)
        val pref = findPreference<EditTextPreference>("key_name")!!
        pref.setOnPreferenceClickListener {
            false
        }
    }
}
<?xml version="1.0" encoding="utf-8"?>
<PreferenceScreen xmlns:app="http://schemas.android.com/apk/res-auto">
    <PreferenceCategory app:title="@string/pref_group_server">
    <EditTextPreference
        app:key="key_name"
        app:title="@string/pref_title_username"
        app:defaultValue="anonymous"
        app:summary="@string/pref_summary_username"
        app:icon="@drawable/ic_user_name_24dp"
        />
    </PreferenceCategory>
</PreferenceScreen>
0

There are 0 best solutions below