Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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>

like image 232
Ankit Goyal Avatar asked Mar 16 '14 23:03

Ankit Goyal


2 Answers

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

like image 153
Philipp Jahoda Avatar answered Oct 17 '22 23:10

Philipp Jahoda


Late answer but according to the comments on the accepted answer it seems like it was some confusion.

I do not agree that disabling is always the correct choice for non editable settings. For example in your Android settings: System -> About Phone -> Status information there is information of about your phone. These are in the Settings list but are not editable, it is important information that might be necessary for the user. Since they are never editable from the settings page it is displayed as a normal Prefrence instead of a disabled EditTextPreference.

XML:

<Preference        
    android:key="username_pref"
    android:title="Username" >
</Preference>

This will make you preference clickable but not editable.

like image 29
Rawa Avatar answered Oct 17 '22 22:10

Rawa