Home » Android » android – What is the name of SharedPreferences file name?

android – What is the name of SharedPreferences file name?

Posted by: admin June 15, 2020 Leave a comment


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

How to&Answers:

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


SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(this);


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:



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:


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

SharedPreferences preferences = getSharedPreferences(PREF_FILE_NAME, MODE_PRIVATE);


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.