androidandroid-preferencesedittextpreference

How to enable / disable a Preference?


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>


Solution

  • 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."