How to enable / disable a Preference?
Asked Answered
U

4

9

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>

Undoubted answered 16/3, 2014 at 23:56 Comment(0)
G
23

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

Goodwill answered 17/3, 2014 at 0:27 Comment(3)
That works, but now the item is "grayed out." Here is a screenshot of what it looks likeUndoubted
Yes of course it is. That is the Android default visual representation for items being not clickable / disabled (e.g. a disabled button looks the same). I would strongly recommend (also for your users sake) to keep it that way.Goodwill
Agreed! Don't hide a click event. This presents a bad UI experience.Barrows
P
7

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.

Philips answered 23/11, 2017 at 16:24 Comment(1)
Seems like the confusing part is the title.Surra
V
1

To disable clicks AND have the preference not be dimmed, use the following XML:

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

Or, use the following Java code:

EditTextPreference pref = (EditTextPreference)findPreference("yourkey");
pref.setEnabled(false);
pref.setShouldDisableView(false);
Vivle answered 12/12, 2020 at 3:40 Comment(0)
S
0

@Rawa answer helped me. I wanted to add a solution for adding a not editable preference dynamically. For my example, I'm adding a preference to display the app version. Be sure to set your key for the preference category.

In onCreatePreferences:

    // Add app version preference
    val appVersionPref = Preference(context).apply {
        title = "App Version"
        summary = "v${BuildConfig.VERSION_NAME}"
    }
    val aboutPrefCategory = findPreference<PreferenceCategory>("YOUR_PREF_CATEGORY_KEY")
    aboutPrefCategory?.addPreference(appVersionPref)

See screenshot of result below App version preference screenshot

Standardize answered 16/11, 2020 at 13:59 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.