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");
Android Shared Preferences Overview Android stores Shared Preferences settings as XML file in shared_prefs folder under DATA/data/{application package} directory. The DATA folder can be obtained by calling Environment. getDataDirectory() .
OR goto file explorer and in the file tree, find your app's data folder under /data/data/com. your-package/shared_prefs. The preference file will be there, as an XML. Copy it from the device and enjoy.
The first thing we need to do is to create one shared preferences file per app. So name it with the package name of your app- unique and easy to associate with the app. When you want to get the values, call the getSharedPreferences() method.
You can create a new shared preference file or access an existing one by calling one of these methods: getSharedPreferences() — Use this if you need multiple shared preference files identified by name, which you specify with the first parameter. You can call this from any Context in your app.
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);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With