2

I've created an EditTextPreference and now I want to get the value from any other activity. I've been trying a lot of things but I cannot get it working. Where is this value stored? How can I retrieve it?

EDIT: I want to get the value from a different activity, not the Preferences activity.

settings.xml

<EditTextPreference
    android:title="EditText1"
    android:key="edit1"
    android:defaultValue="0"
    android:dialogIcon="@drawable/fleetespiar"
    android:inputType="number" />

Settings.java (How to do this in a different activity)

Preference edit1= findPreference("edit1");
EditTextPreference editt1 = (EditTextPreference) edit1;

System.out.println(String.valueOf(editt.getText().toString()));
Willi Mentzel
  • 27,862
  • 20
  • 113
  • 121
anonymous
  • 1,320
  • 5
  • 21
  • 37

2 Answers2

2

First you need to create an instance of SharedPreferences, then you can call getString() on it, specifying the key for the value.

Willi Mentzel
  • 27,862
  • 20
  • 113
  • 121
Egor
  • 39,695
  • 10
  • 113
  • 130
  • First of all thank you for your answer. I know how to do it, I know how to get the value when I create the `SharedPreference` and I store it where I want. The problem is that this value is saved automatically. `I've tried with getDefaultSharedPreferences()` but no luck... Maybe the question is not as self-explainable as I tought. Sorry. – anonymous Jul 09 '12 at 08:31
1

Your value will be stored in a shared preference file.

Check my little example below:

  1. Create a xml directory inside the res directory (resources) of your android project.

  2. Inside your new xml directory you must create a preference.xml file wich will contains your EditTextPreference.

        <?xml version="1.0" encoding="utf-8"?>
        <PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android" >
            <EditTextPreference
                android:dialogTitle="My preference"
                android:key="pref"
                android:summary="Enter Your Preference"
                android:title="Edit Text Preference" />
        </PreferenceScreen>
    
  3. Now create your Preferences class which extends PreferenceActivity.

        import android.content.SharedPreferences;
        import android.content.SharedPreferences.OnSharedPreferenceChangeListener;
        import android.os.Bundle;
        import android.preference.EditTextPreference;
        import android.preference.Preference;
        import android.preference.PreferenceActivity;
    
            public class Preferences extends PreferenceActivity {
    
            @Override
            protected void onCreate(Bundle savedInstanceState) {
                 super.onCreate(savedInstanceState);
                 addPreferencesFromResource(R.xml.preferences);
            }
        }
    
  4. Use a SharedPreference object to insert and get your String value.

yugidroid
  • 6,640
  • 2
  • 30
  • 45
  • First of all thank you for your answer. I know how to do it, I know how to get the value when I create the `SharedPreference` and I store it where I want. The problem is that this value is saved automatically. `I've tried with getDefaultSharedPreferences()` but no luck... Maybe the question is not as self-explainable as I tought. Sorry. – anonymous Jul 09 '12 at 08:32
  • @KrLx_roller, you might take a look at [this](http://android-er.blogspot.pt/2010/08/edittextpreference.html) and [this](http://androidpartaker.wordpress.com/tag/edittext-preference-summary/) references. I think they can help you a lot. – yugidroid Jul 09 '12 at 08:52
  • Thank you! I Googled and I found the second webpage, not the first one. It's what I was looking for. But it's strange... I did exactly what the example show with no luck... Maybe I forgot something... Thank you! I'll try it now! – anonymous Jul 09 '12 at 09:49