john
john

Reputation: 31

Shared Preferences

I want to show an edit box in shared preferences which should be read only for user but i should be able to change it at through code. how to achieve this?

Upvotes: 3

Views: 3231

Answers (6)

TheIT
TheIT

Reputation: 12219

One way you could display non editable text is to use a standard Preference and set it's summary (small text displayed under the Preference title).

<Preference
    android:key="text_preference"
    android:selectable="false"
    android:title="Title" />

.

    Preference textPreference = (Preference) getPreferenceManager().findPreference("text_preference");
    textPreference.setSummary("Text");

Upvotes: 0

javawebapps
javawebapps

Reputation: 378

Try this in the XML: android:selectable="false"

Upvotes: -1

Ads
Ads

Reputation: 6691

i hope u should first display the value from shared pref and use android:editable="false" in the textbox layout. this code will not allow the user to modify the text in editbox.

Upvotes: 0

Romain
Romain

Reputation: 2348

Are you using the standard PreferenceActivity? If so, you should be able to go to your preference XML file, and set android:enabled="false" on the ones you want to be read-only.

Upvotes: 2

ferostar
ferostar

Reputation: 7082

What you want is a TextView. An EditText is a TextView subclass that's editable by the user by default. So you set the TextView, the user can't modify it and you set the text with

TextView tv = new TextView(this);
tv.setText("");

Upvotes: 2

raukodraug
raukodraug

Reputation: 11619

This tutorial is good for creating preferences. Also, if you want to change the preferences outside the PreferenceActivity you should use the Editor For example:

Editor e = PreferenceManager.getDefaultSharedPreferences(getBaseContext()).edit();
e.putString("yourPreference", "default value");
e.commit();

I hope it helps

Also, as the documentation show in here, the PreferenceActivity is used for showing a visual style of the preferences. Also, as it is indicated in the same link, "the preferences will automatically save to SharedPreferences as the user interacts with them".

So, knowing this, if you don't want the user to edit that preference, you can use a TextView, or an EditText non/editable in the PreferenceActivity, and then use the code above to modify it outside the PreferenceActivity. I hope that helps.

Upvotes: 3

Related Questions