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);



    }

}
ㅤㅤㅤㅤㅤㅤ
  • 323
  • 1
  • 2
  • 11
  • You want to set *android:defaultValue* from code? – Rami Feb 07 '15 at 17:32
  • @Rami 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. – ㅤㅤㅤㅤㅤㅤ Feb 07 '15 at 23:05
  • 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/...."*. – Rami Feb 08 '15 at 00:03
  • @Rami 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"`. – ㅤㅤㅤㅤㅤㅤ Feb 08 '15 at 00:05

3 Answers3

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!

ㅤㅤㅤㅤㅤㅤ
  • 323
  • 1
  • 2
  • 11
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);
Anggrayudi H
  • 14,977
  • 11
  • 54
  • 87
  • 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). – ㅤㅤㅤㅤㅤㅤ Feb 07 '15 at 22:58
  • 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. – Anggrayudi H Feb 08 '15 at 03:52
  • Thanks for helping. I found a way around too. – ㅤㅤㅤㅤㅤㅤ Feb 08 '15 at 09:20
0

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

endlesschaos
  • 145
  • 1
  • 6