11

I am new to Android and I am kind of stuck for 6 hours straight.

The problem is I don't know the name of the preferences file, and I need to get values from preferences file. I am using Android Studio and created a "Settings Activity". All the way I had not given name to any file except SettingsActivity.java.

So my question is what is the name of the Shared Preferences file (cause the application is keeping the values). Or otherwise if there is a way to find out.

Or perhaps I am missing something obvious in code. Following is my relevant code.

String key = "example_text";
final String PREF_FILE_NAME = "SettingsActivity";
SharedPreferences preferences = getSharedPreferences(PREF_FILE_NAME, MODE_PRIVATE);
String value = preferences.getString(key, " null");                

EDIT 1: I have an activity named RemoteDevice.java, within this activity I have a Async Task subclass for internet usage. Now I have stored IP address through the above mentioned PreferencesActivity and now want to retrieve it. But am unable to find it.

EDIT 2: In the following code I am trying to get value from edit text.

<PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android">

<!-- NOTE: EditTextPreference accepts EditText attributes. -->
<!-- NOTE: EditTextPreference's summary should be set to its value by the activity code. -->
<EditTextPreference
    android:key="example_text"
    android:title="@string/pref_host_ip_address"
    android:defaultValue="@string/pref_default_host_address"
    android:selectAllOnFocus="true"
    android:inputType="numberDecimal"
    android:digits="123456789."
    android:capitalize="words"
    android:singleLine="true"
    android:maxLines="1" />

<!-- NOTE: Hide buttons to simplify the UI. Users can touch outside the dialog to
     dismiss it. -->
<!-- NOTE: ListPreference's summary should be set to its value by the activity code. -->
<ListPreference
    android:key="example_list"
    android:title="@string/pref_title_add_friends_to_messages"
    android:defaultValue="-1"
    android:entries="@array/pref_example_list_titles"
    android:entryValues="@array/pref_example_list_values"
    android:negativeButtonText="@null"
    android:positiveButtonText="@null" />

And I am guessing here android:key is the key to be passed as arguments in

String value = preferences.getString(key, " null");

Abbas
  • 3,529
  • 5
  • 36
  • 64

3 Answers3

21

I am using Android Studio and created a "Settings Activity".

Then you get your SharedPreferences via PreferenceManager.getDefaultSharedPreferences(). Replace:

SharedPreferences preferences = getSharedPreferences(PREF_FILE_NAME, MODE_PRIVATE);

with:

SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(this);
CommonsWare
  • 986,068
  • 189
  • 2,389
  • 2,491
  • I already tried that but the problem is I am using the above mentioned code in an AsyncTask-SubClass of another activity. – Abbas Aug 03 '15 at 19:21
  • 1
    @AbbasA.Ali: So? That does not change how you access the `SharedPreferences` instance used by `PreferenceFragment` and `PreferenceActivity`. You call `PreferenceManager.getDefaultSharedPreferences()`, supplying as a parameter *any* `Context` from your app (e.g., an instance of an `Activity`). – CommonsWare Aug 03 '15 at 19:24
  • So u can give any context of any activity in any activity? – Abbas Aug 03 '15 at 19:34
  • I tried that but it doesn't work. I am still getting `null`. – Abbas Aug 03 '15 at 19:52
  • thanks I got it by using your method. I apologize for late response but a few of my friends advised me against rooting my device. But in the end it worked for me and I found the file. Moreover is there a way to change the name of Shared Pref. file? Or change the name at build time? Thanks. – Abbas Aug 20 '15 at 15:25
  • This way is Deprecated – EslamWael74 Sep 14 '21 at 14:26
0

You can use this:

String key = "example_text";
final String PREF_FILE_NAME = "SettingsActivity";
shared = getSharedPreferences(PREF_FILE_NAME, Context.MODE_PRIVATE);
String value = preferences.getString(key, " null");

But first you have to save some value with your key like:

shared.edit().putString(key,"MY_VALUE").commit();
Aakash
  • 5,181
  • 5
  • 20
  • 37
  • I am using my android phone for this, and it retains the changed values. So I think the values are there. Any other suggestions? – Abbas Aug 03 '15 at 19:24
  • make your question clear, what is the issue you are getting. – Aakash Aug 03 '15 at 19:27
0

Run your project on a real device and if a SharedPreferences file (it has .xml extension) is created you can find it in the root catalog of the device, here to be more exact:

/data/data/YOUR_PACKAGE_NAME/shared_prefs/YOUR_PREFS_NAME.xml or /data/data/YOUR_PACKAGE_NAME/shared_prefs/YOUR_PACKAGE_NAME_preferences.xml

By the way, you may just use getPreferences() method. Change your

SharedPreferences preferences = getSharedPreferences(PREF_FILE_NAME, MODE_PRIVATE);

into

SharedPreferences preferences = getPreferences(MODE_PRIVATE);

Your SharedPreferences file will get a default name. But keep in mind that it is worth using getPreferences() instead of getSharedPreferences() only if you won't need more than one SharedPreferences file in your project.

DH28
  • 553
  • 3
  • 16
  • I am running it on my android phone. But where is data folder? – Abbas Aug 03 '15 at 20:34
  • I don't see my project's package name. Does that mean there is no preference's file? Cause whenever I run the app and change preferences on my phone and re-run it, it stays updated. Also tried 'Show Hidden Files and Folders' – Abbas Aug 03 '15 at 23:30
  • May be your app is installed at the external SD. Did you check there? – DH28 Aug 03 '15 at 23:37
  • That's the problem I don't have an external SD. – Abbas Aug 03 '15 at 23:40
  • You need to get to your file system root. If it is not possible from your file browser try Total Commander for Android. – DH28 Aug 03 '15 at 23:58