Shared Preference problem in android app - android

in my app the first activity is a sign in page. In the edit boxes i am typing the user name and password. Those values are been move to an api and in return i am getting the userid from the server as an xml file.
I am parsing the xml file and storing the value in shared preferrence as follows
SharedPreferences.Editor IdEditor = Id.edit();
IdEditor.putString("useridValue", chap.getid());
IdEditor.commit();
And in the next time when the user opens the app i want to check whether it is already signed i or not. How to check this using the value stored in Shared preference

is your Id class extending SharedPreferences ?
maybe
String userId = Id.getString("useridValue");
If your preference is stored in the default preference then you can
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(this);
String userId = prefs.getString("useridValue");
on a side note you shouldn't really use a capital I on the IdEditor variable it should probably be idEditor

Check whether this entry already exist in shared preference, using:
id.containskey("useridvalue")

Related

How Do I Set The Value Of A String In Android?

I have a string and a button as follows:
String message = "Hello World"
button.OnClickListener(......){
message = "123456789";
}
So here is what I want,
When the app starts the string is "Hello World" but when the user clicks the button it changes to "123456789" and I want the string to change permanently for lifetime.
So when the user restarts the application or reinstall it the string is still
"123456789".I think this comes under Shared Preferences.
Please Help,I really need this
You can save that string in Shared Preferences, and get it all the from there. If is no value, you can get default value from resources or you can provide your own default String. Note that you can do that for lifetime. If user delete the app or clear cache, your view will display default value, Hello World.
Edit
You can use android:allowBackup="true" from manifest in order to keep old Shared Preference values.
There is a chance data from SharedPreferences is gone when you reinstall especially if you uninstall the app first, same case as local database using SQLite. You can try using database from your local server.
To save a String in SharedPreferences :
SharedPreferences.Editor editor = getSharedPreferences("com.package.name", Context.MODE_PRIVATE).edit();
editor.putString("keyName", message);
editor.apply();
To get it back :
SharedPreferences sharedPrefs = getSharedPreferences("com.package.name", Context.MODE_PRIVATE);
message = sharedPrefs.getString("keyName",defaultValue);
When the user modifies it, save it. When the app start, get it with as default value "Hello World" so that while the user hadn't click the button, it will stay as Hello World.
For Restarting Shared Preference is the best mechanism but for reinstall allowbackup works on API level 23 and above,
you can consider to update the value on server once you update in your Shared Preferences for the first time and during reinstall can do a API call check for the same for the particular user.

Retrieving SharedPreferences

I'm working with an android application, and when my application is open I want to log in. I'm using shared preferences for saving data.
How can I control three cases: if his credentials are null, if that user exists and does not have to register and save his credentials again and also if username and password he entered is correct and is the same with them he used when entered for the first time.
Use this code to retrieve values from sharedPreferences.
SharedPreferences sharedPref ;
sharedPref = PreferenceManager.getDefaultSharedPreferences(this);
int status=sharedPref.getInt("status",0);
if(status==1){
//do something here you want to do
}else if(status==2){
//do something here you want to do
}
Read the documentation here http://developer.android.com/reference/android/content/SharedPreferences.html
Also check this http://www.tutorialspoint.com/android/android_shared_preferences.htm

how to use SharedPreference among activities

I Am writing a code using SharedPreference to store username and password of a user but each time I entered information the older one in xml file are override by newer one what I have to to to get all my data?
SharedPreferences sp1=getSharedPreferences("myshared", 0);
sp1.edit().putString("name", name.getText().toString()).commit();
sp1.edit().putString("pass", pass.getText().toString()).commit();
sp1.edit().putString("age",age.getText().toString()).commit();
sp1.edit().putString("id",id.getText().toString()).commit();
It sounds like you're overwriting your shared preferences between activities. SharedPreferences are persistent like a file on disk, so you shouldn't ever have an issue with the values not being set, hence why you must be overwriting it.
You can get your SharedPreferences by doing
SharedPreferences sp1=getSharedPreferences("myshared", 0);
String name = sp1.getString("name", "noname");
String pass = sp1.getString("pass", "nopass");
...
You can determine if the name/pass was set by checking if they equal the default value (noname and nopass in this case, though it could easily be null).

android shared preference storing multiple values;

I created SharedPreference obj using following:
Note : creating number of preferences under the same pref file name
public static Sting name = "abx"; //pref file name
spo = context.getsharedpreference(name , mode_private);
editor = spo.edit(); //get the editor associated with pref obj
Map<String, String> test= new HashMap<String, String>();//create a map
test.put("1", "abc");//fill the values
test.put("2", "efg");//used map because values are stored in key -value pairs
test.put("3", "ghj");
for (String s : test.keySet()) {
// use the name as the key, and the icon as the value
editor.putString(s, nameIcons.get(s));//use the key as preference name
}
editor.commit();//commit the preferences
I have a doubt , please correct me if i am wrong , SharedPreference file with name "abz" is created and in this file preference values are stored as key - value pairs or if editor obj creates new preference file for each key , whats the use of supplying pref file name at the time of sharedprefences object creation?
Essentially, you can create multiple preference files for a given application. There are a few reasons one could imagine doing this, the best being if you had multiple accounts. You could label one set of preferences "account1" and another "account2". Just by keeping track of which account you are in, you can share code without a huge amount of complexity.
Unless you are specifically using this functionality, I suggest you use context.getApplicationContext().getPackageName() for the name, as it should be available almost everywhere.
Basically what happen inside your for each loop :-
there will be only one value stored in the preferances instead of creating SharedPreferance file with the name provided in the code for each value in the map.
Use of Giving File name during creation of sharedPreferance , so that it can be retrieved at other part of application and can acess its values.

Android SharedPreferences

I have 3 webviews in my search engine app.When the user enters his query, by default he gets Google results, followed by yahoo and Ask buttons at the bottom, on clicking either of them, he gets results for that query from those sites. Now I want to give the user the privilege to change the default result site. I have created 3 radiobuttons. Upon confirmation, say he chooses Yahoo, how can i set it as Yahoo till the next time he changes it to some other site,
Accessing data from SharedPreferences:
SharedPreferences sharedPref = getSharedPreferences("FileName",MODE_PRIVATE);
String webViewChoice = sharedPref.getString("userChoice","null");
if no choice was saved (in the case when the application is running for the first time), you'll get "null" in webViewChoice.
use this condition as you wish
Saving data in SharedPreferences:
SharedPreferences sharedPref = getSharedPreferences("FileName",MODE_PRIVATE);
SharedPreferences.Editor prefEditor = sharedPref.edit();
prefEditor.putString("userChoice",usersChoice);
prefEditor.commit();
I hope it helps.
Save the user's preference as default-engine=google by default in a shared preferences file.
On app loading, read the file and set the default engine during the app runtime. When user chooses a different engine as default, then update the preferences file.
Hope this helps.

Categories

Resources