What is xml format/schema for shared preferences in Android? - android

Below is how SharedPreferences are read from a file, but what is xml format/schema for SharedPreferences file? How is it different from format for regular preferences? where is this schema posted? And which directory should the file reside in?
public static final String PREF_FILE_NAME = "PrefFile";
SharedPreferences preferences = getSharedPreferences(PREF_FILE_NAME, MODE_PRIVATE);

I don't think there is a formal schema. You can infer the format by looking at what it outpus though. Open up DDMS in Eclipse, and browse to /data/data/<your package>/shared_prefs. For example, you'll find:
<map>
<int name="id" value="1" />
<string name="first">John</string>
</map>
You might want to consider reading in your initialization values and using the SharedPreferences API to persist them. This way, you can be sure that SharedPreferences will write it's file in the correct place.

Related

What is the name of SharedPreferences file name?

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");
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);
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();
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.

how to modify sharedpreference name .I want to have specific name for preferences file

Default the name of the preferences file saved on the device is always be _preferences. I want to modify it to "mypreference" on to the device how can I i do that.
SharedPreferences are stored in an xml file in the app data folder, i.e.
/data/data/YOUR_PACKAGE_NAME/shared_prefs/YOUR_PREFS_NAME.xml
or the default preferences at:
/data/data/YOUR_PACKAGE_NAME/shared_prefs/YOUR_PACKAGE_NAME_preferences.xml
SharedPreferences added during runtime are not stored in the Eclipse project.
Taken from here.
For the custom name you'll want, take a look at this thread here. For example:
String fileName="mypreference";
File f=new File("/data/data/eywa.musicplayer/shared_prefs/"+whatever+".xml");
f.renameTo(new File("/data/data/eywa.musicplayer/shared_prefs/"+fileName+".xml"));
SharedPreferences mySharedPreferences = getSharedPreferences("list_of_playlist",Activity.MODE_PRIVATE);
SharedPreferences.Editor editor = mySharedPreferences.edit();
editor.remove(PlayListName);
editor.putString(fileName, fileName);
editor.commit();
PlayListName=fileName;
Get a new preference file (getSharedPreferences) with the name "mypreference", and copy everything from "_preferences" to it.

Preferences don't show up in data/data nor can i access them with code

I'm trying to create a game. Now for some data i need to keep(music, effects, background on/of) i would like to use preferences.
I create new xml file named preferences.xml
<?xml version="1.0" encoding="utf-8"?>
<PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android" >
<EditTextPreference android:key="music" android:title="Some title" android:defaultValue="true"/>
</PreferenceScreen>
Then i go to the location(with root privileges) and there is only lib folder in data/data/my.app.folder/. No shared_prefs folder.
When i try to access the preferences it is null. I tried with getSharedPreferences() and with PreferenceManager.getDefaultSharedPreferences(). What could be the reason?
Also i don't have activity for the preferences, I want to manage them manually.
How can I get over this problem?
To use shared preferences you will have to create it first. Like this:
SharedPreferences prefs = getApplicationContext().getSharedPreferences(
"music", Context.MODE_PRIVATE);
To create preferences in first time in it:
final String BACKGROUND_DEFAULT_VALUE = "on";
final String DEFAULT_TITILE = "";
final String TITILE_KEY = "title";
final String BACKGROUND_KEY = "background_key";
Editor editor= prefs.edit();
editor.putString(TITILE_KEY, DEFAULT_TITILE);
editor.putString(BACKGROUND_KEY, BACKGROUND_DEFAULT_VALUE);
editor.commit();
Than to edit/and save preferences in it:
Editor editor= prefs.edit();
editor.putString(TITILE_KEY, "new title");
editor.putString(BACKGROUND_KEY, "new background value i.e on/off");
editor.commit();
Than To read preferences:
String background_value = prefs.getString(BACKGROUND_KEY);
String title_value = prefs.getString(TITILE_KEY );
And you can see your shared preference file at location, it will be there after the creation.
/data/data/<packagename>/shared_prefs/music.xml
For more details and help, The android sdk's sample directory contains an example of retrieving and stroing shared preferences. Its located in the:
<android-sdk-home>/samples/android-<platformversion>/ApiDemos directory
Hope it helps.
There are two different types of xml files related to preferences.
The one you are showing is the xml resource file representing the preference hierarchy (screen representation).
The second one is the file containing the actual key value pairs.
Creating the first one doesn't create the second one. You'll actually need to display the file using a PreferenceActivity (or the Fragment equivalent or your own display method which - as I understand - you intend to do) or you'll have to create the preferences in code.

read from android SharedPreference

I know it's been asked before, but it doesn't seem to work for me.
I have an XML file preferences.xml in the folder res\xml. It contains an optionscreen like so:
<PreferenceScreen>
<EditTextPreference
android:name="prfEmail"
android:summary="Your account's e-mail address"
android:title="E-mail"
android:key="prfEmail" />
</PreferenceScreen>
I can make an activity from it and it shows fine. Now I'd like to read values from these preferences, but I can't seem to find it. In another activity I do:
SharedPreferences appSharedPrefs;
appSharedPrefs = getSharedPreferences("preferences",Activity.MODE_PRIVATE);
String restoredText = appSharedPrefs.getString("prfEmail",null);
Whan I'd like to print this value it gives null back. What am I doing wrong?
The app preferences are stored in the default SharedPreferences. You can access them with :
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(this);

Are SharedPreferences stored in databases?

Is this correct?
SharedPreferences are stored in databases?
No it is not correct. SharedPreferences are stored as XML files in your applications directory.
I am agreed with #Octavian answer (up voted), it is stored inside the file.
SharedPreferrences are stored in
databases?
As i have written answer, NO, its stores inside the file inside your project directory.
To view this file, go to DDMS Perspective, click on the File Explorer.
From File Explorer, data -> data -> your project -> shared_prefs.
Above, shared_prefs is the folder contains all the shared preferences you have declared and used.
Actually shared preferences store value into a variable .it is saved as like key-value pair
below is the code is used for store and retrieve values through shared preference values.
SharedPreferences prefs=getSharedPreferences("Key", 0);
Editor e= prefs.edit();
e.putString("Name", "AAA");
e.commit();
For retrieve the shared prefs value use below code
SharedPreferences prefs=getSharedPreferences("Key", 0);
String s= prefs.getString("Name", "");

Categories

Resources