How to enable / disable a Preference?

Ankit Goyal picture Ankit Goyal · Mar 17, 2014 · Viewed 11.7k times · Source

I want to make my EditTextPreference not editable (as in, nothing happens when you click on the item in the Settings page). How do I do that?

Here is my code:

<PreferenceCategory android:title="My Account" >

    <EditTextPreference
        android:clickable="false"
        android:focusable="false"
        android:focusableInTouchMode="false"
        android:key="username_pref"
        android:summary="     "
        android:title="Username" >
    </EditTextPreference>

</PreferenceCategory>

Answer

Philipp Jahoda picture Philipp Jahoda · Mar 17, 2014

You can do that programatically or in .xml.

XML:

<EditTextPreference
    android:enabled="false" >
</EditTextPreference>

Programmatically:

getPreferenceScreen().findPreference("yourpref").setEnabled(false);

From the Android Documentation:

"If disabled, the preference will not handle clicks."