I am trying to save the backgroundImage of the ViewPager, using SharedPreferences and load that image as background when we run the app for the next time. It works fine in the emulator but not in the device. Please help me in solving this.Thanks,
Here is my code..
SlidePagerActivity.mPager.setBackgroundResource(R.drawable.snow);
SharedPreferences backgroundImage = getSharedPreferences("BGPREFS", MODE_WORLD_READABLE);
SharedPreferences.Editor bgEditor = backgroundImage.edit();
bgEditor.putInt("imgValue", R.drawable.app_bg_snowfall);
bgEditor.commit();
Try this:
To put in SharedPreferences
SharedPreferences sp = getSharedPreferences("myApp", Activity.MODE_PRIVATE);
SharedPreferences.Editor editor = sp.edit();
editor.putInt("NewBackground", idImage);
editor.commit();
To get the Preference:
SharedPreferences sp = getSharedPreferences("myApp", Activity.MODE_PRIVATE);
int myNewIntValue = sp.getInt("NewBackground", 0);
and finally use it. MyNewIntValue
Related
I want to save some data into SharedPreferences. everything is intact. I can read from it but I can not save anything.
SharedPreferences prefs = getSharedPreferences("preferences", 0);
SharedPreferences.Editor editor = prefs.edit();
editor.putInt("age", 25);
editor.putString("name", "name1");
editor.commit();
I have checked everything many times. any suggestion ?
EDIT: I solved it by using this line:
SharedPreferences Prefs = PreferenceManager
.getDefaultSharedPreferences(this);
You're going to want to add .apply(); after any change. So for example the
editor.putInt("age", 25);
would change to
editor.putInt("age", 25).apply();
I'm unable to delete SharedPreferences from the app on click event.
Here is how I'm storing the value into UserInfoActivity SharedPreferences:
SharedPreferences notificationCountSP = PreferenceManager
.getDefaultSharedPreferences(getApplicationContext());
SharedPreferences.Editor notificationEditor = perkBalance.edit();
notificationEditor.putString("notificationCount",notificationCountValue);
notificationEditor.commit();
And here is how I'm trying to clear all data in SharedPreferences from MainActivity:
SharedPreferences clearNotificationSP = getSharedPreferences(
"notificationCountSP", 0);
SharedPreferences.Editor editor = clearNotificationSP.edit();
editor.remove("notificationCount");
editor.clear();
editor.commit();
Please tell what am I doing wrong with this.
Any kind of help will be appreciated.
SharedPreferences notificationCountSP = PreferenceManager
.getDefaultSharedPreferences(getApplicationContext());
SharedPreferences.Editor notificationEditor = notificationCountSP.edit();
notificationEditor.putString("notificationCount", notificationCountValue);
notificationEditor.commit();
notificationEditor.remove("notificationCount");
notificationEditor.commit();
or
SharedPreferences clearNotificationSP = getSharedPreferences("notification_prefs", 0);
SharedPreferences.Editor editor = clearNotificationSP.edit();
editor.putString("notificationCount", notificationCountValue);
editor.commit();
editor.remove("notificationCount");
editor.commit();
First solution uses the default application preferences file, and the second a custom notification_prefs file.
You are using PreferenceManager.getDefaultSharedPreferences to store but retrieving from getSharedPreferences("notificationCountSP"). They are different files unless you set the default one to "notificationCountSP".
You can do it like below
SharedPreferences userPref = getSharedPreferences(
MyActivity.SHARED_PREFERENCES_FILENAME,MODE_PRIVATE);
My Code is:
SharedPreferences myPrefs = this.getSharedPreferences("MYPREF'USERID",
Context.MODE_PRIVATE);
int uid = myPrefs.getInt("USERID", 0);
I want to use USERID in another two activities for fetching records
Please help me how to do
Thank You
You can set values in preference as follows
SharedPreferences pref = PreferenceManager
.getDefaultSharedPreferences(this);
SharedPreferences.Editor editor = pref.edit();
editor.putInt ("USERID", 0);
editor.commit();
and you can retrieve the data form preference as
final SharedPreferences pref = PreferenceManager.getDefaultSharedPreferences(this);
SharedPreferences.Editor editor = pref.edit();
final int userId= pref.getInt("USERID", -1);
You can get SharedPreferences all around your application by using:
SharedPreferences sharedPreferences = Context.getApplicationContext().getSharedPreferences("pref", Context.MODE_PRIVATE);
You also want to put your values in this global sharedPreference.
I've on Activity A the following code:
SharedPreferences sharedPreferences = getSharedPreferences("prefs", 0);
SharedPreferences.Editor editor = sharedPreferences.edit();
editor.putString("nome", nome.getText().toString());
editor.commit();
I've on Activity B the following code:
SharedPreferences sharedPreferences = getSharedPreferences("prefs", 0);
String a = sharedPreferences.getString("nome", "");
nomeMediador.setText(a); //TextBox
Can anyone tell why it's not showing (the value saved on Activity A) on Activity B?
Refer the below code
Activity A
SharedPreferences sharedPreferences = PreferenceManager
.getDefaultSharedPreferences(getApplicationContext());
SharedPreferences.Editor editor = sharedPreferences.edit();
editor.putString("nome", nome.getText().toString());
editor.commit();
Activity B
SharedPreferences sharedPreferences = PreferenceManager
.getDefaultSharedPreferences(getApplicationContext());
String a = sharedPreferences.getString("nome", "");
nomeMediador.setText(a);
its better use getter and setter with the help of string.xml file, you will never get this kind of problem. for that matter you may check this blog:
http://sspower3.blogspot.in/2011/11/sharedpreferences-in-eazy-way.html
I have a string I want to store in my SharedPreferences. Is there some kind of setString I could do to accomplish this?
You mean something like this?
SharedPreferences sharedPreferences = PreferenceManager.getDefaultSharedPreferences(this);
SharedPreferences.Editor editor = sharedPreferences.edit();
editor.putString("username", "myUsername");
editor.commit();
You can make changes in the SharedPreferences, and commit the changes after you are done.
Try this.
SharedPreferences prefs = getSharedPreferences("PreferenceFileName", MODE_PRIVATE);
SharedPreferences.Editor editor = prefs.edit();
editor.putString("StringNameToBeStored", "value");
editor.commit();
And you can do everything in the same line, there's no need to declare editor
SharedPreferences sharedPreferences = PreferenceManager.getDefaultSharedPreferences(this);
sharedPreferences.edit().putString("username", "myUsername").commit();
And don't forget the commit!