How can I load a String from SharedPrefs in the Manifest file? Is it possible? Thank you for your help.
sharedPreference.getString("username","defaultName")
Related
I want to edit a String into the "Value" folder, maybe using SharedPreferences. But I can't find any way to do this, changing the value of a string for example by calling #string/stringname seems to be impossible. How can I achieve that?
Thanks in advance.
You cannot change the contents of the resource files during runtime.
I want to edit a String into the "Value" folder.
The above is not possible. You can possibly use SharedPreferences to store that string of yours, as described in the docs.
What I want to do is tell my main activity if there is a SharedPreferences file, change a bunch of strings using the values in the file. And if there is no SharedPreferences file created yet, use the default strings that are assigned in the layout (essentially do nothing).
You can save preferences with this snippet:
SharedPreferences sp = PreferenceManager.getDefaultSharedPreferences(context);
Editor edit = sp.edit();
edit.putString(yourkey, yourvalue);
edit.commit();
And you can fetch them using this:
SharedPreferences sp = PreferenceManager.getDefaultSharedPreferences(context);
sp.getString(yourkey,defaultvalue);
Why not just use the SharedPreferences#getString(String key, String defaultValue) API that let's you specify a default value?
You can try this:
PreferenceManager.getDefaultSharedPreferences(this).getString(key, defValue).
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.
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);
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.