Android how to set default value of EditTextPreference from SharedPreference?
Asked Answered
M

3

13

This time in the same project I'm facing a slightly challenging issue where in settings.xml file in the res/xml folder:

<EditTextPreference
    android:key="weight" 
    android:title="@string/update_user_weight"
    android:persistent="true"
    android:dialogTitle="@string/update_user_weight"
    android:dialogMessage="@string/update_user_weight_message"
    android:defaultValue="" />

<EditTextPreference
    android:key="age"
    android:title="@string/update_user_age"
    android:persistent="true"
    android:dialogTitle="@string/update_user_age"
    android:dialogMessage="@string/update_user_age_message"
    android:defaultValue="" />

and in a class file UserData.java:

SharedPreferences storeWeightAndAge = getSharedPreferences("WeightAndAgeStorage", Context.MODE_PRIVATE);
Editor store = storeWeightAndAge.edit();
store.putString("weight", weightData);
store.putString("age", ageData);
store.commit();

What I'm trying to do here is to set the above two EditTextPreferences' android:defaultValue to stored weight and age in the SharedPreferences respectively.

Now, how do I go about doing that?

EDIT: provided Settings.java file that uses the settings.xml file:

package com.example.drinkup;

import android.content.SharedPreferences;
import android.os.*;
import android.preference.PreferenceFragment;
import android.preference.PreferenceActivity;
import android.preference.PreferenceManager;

public class Settings extends PreferenceActivity {
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        addPreferencesFromResource(R.xml.settings);



    }

}
Mckibben answered 7/2, 2015 at 14:37 Comment(4)
You want to set android:defaultValue from code?Postulate
@Postulate If its only possible through code, sure. But if the xml can help find it by its own, that would be better. Like: @strings/.... etc etc... something like that.Oleograph
In the xml, you can do it by: android:defaultValue="YOUR_DEFAULT_VALUE". Notice that you get your default value from strings.xml android:defaultValue="@strings/....".Postulate
@Postulate Oh no I do not plan on setting the default values from strings.xml, I just wanted to give an example of setting the default values like in a similar way as you would from strings.xml. I want to set the default values as the strings I have stored in the SharedPreferences which their keys are "weight" and "age".Oleograph
M
7

Okay guys, I got it:

import android.preference.EditTextPreference;

public class Settings extends PreferenceActivity {


    EditTextPreference weightEditTextPreference;
    EditTextPreference ageEditTextPreference;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        addPreferencesFromResource(R.xml.settings);

        weightEditTextPreference = (EditTextPreference)findPreference("weight");
        ageEditTextPreference = (EditTextPreference)findPreference("age");

        SharedPreferences getWeightAndAgeStore = getSharedPreferences("weightAndAgeStorage", Context.MODE_PRIVATE);
        weightEditTextPreference.setText(getWeightAndAgeStore.getString("weight", "0"));
        ageEditTextPreference.setText(getWeightAndAgeStore.getString("age", "0"));

What this does is that it sets the EditText box to the SharedPreferences' saved key (in this case, weight and age of WeightAndAgeStorage) data in the dialog that pops up when you press it.

Hope anyone else reading this now or in the future (but what about the past?) benefits from this finding.

Cheers!

Mckibben answered 8/2, 2015 at 3:28 Comment(2)
@Postulate Can't, only acceptable on the next day since it was added.Oleograph
Thank you so much. After many years it still is an accelerator.Wateriness
P
5

Firstly, set the default value for EditTextPreference. The value will be stored as a String type. For example:

<EditTextPreference
    android:key="weight"
    android:defaultValue="enter your value (54)"
    ... />

<EditTextPreference
    android:key="age"
    android:defaultValue="enter your value"
    ... />

Then, apply the default value in activity where your app's activity is started first (once installed for first). For example, in the MainActivity's onCreate() method:

PreferenceManager.setDefaultValue(this, R.xml.settings, false);
Pogey answered 7/2, 2015 at 18:1 Comment(3)
Well, I've just tried this, and it doesn't seem to be working. The text is showing up in the editTextPreference as weightData, not the true saved weight which is a numbered string (54).Oleograph
You can change weightData as your value (i.e. 54). See edit first. But, it's better for you to create a NumberPicker in preferences which saves data as int (numbered value), because EditTextPreference always saves data as a String value. Remember: String cannot be used in math, but int can.Pogey
Thanks for helping. I found a way around too.Oleograph
L
0

All you have to do is use the sharedPreferences.getString() and then use the editext.setText() to that.

Lumberjack answered 7/2, 2015 at 19:4 Comment(1)
Could you help clarify further on how to do this and how it works?Oleograph

© 2022 - 2024 — McMap. All rights reserved.