SharedPreferences editing - android

Im having problem incrementing a sharedpreference. Isn't it possible?
..
SharedPreferences getPrefs = PreferenceManager.getDefaultSharedPreferences(getBaseContext());
SharedPreferences.Editor favEdit = getPrefs.edit();
int somepref = getPrefs.getInt("somePref", 0);
somepref++;
favEdit.putInt("somePref", somepref);
favEdit.commit();
This should work imo, but when executed it's just ignored.
If i use a number instead it works fine, but then the point in using the sharedpreference is lost..
Anyone?
How I load my preferences:
SharedPreferences getPrefs = PreferenceManager
.getDefaultSharedPreferences(getBaseContext());
getPrefs = getSharedPreferences(filename, 0);
int somepref = getPrefs.getInt("somePref", 0);
The problem is that I want my somepref to increase in function1 # activity1, and use the somepref to define what function to run in activity2.
The main plan:
I want to add a imagebutton from one activity to another by doing a longclick. And I want to be able add more than one imagebutton.
And from this new activity I want a longclick to remove the imagebutton.
I'm having problems getting my head around how to do this..

somepref.putInt("somePref", somepref);
should be
favEdit.putInt("somePref", somepref);
and fav.commit(); shouls be favEdit.commit();
and do you have getPrefs = getSharedPreferences(filename, 0); somewhere?

i don't know how you compile the current code
you need to change the last two lines to
favEdit.putInt("somePref", somepref);
favEdit.commit();

Related

Change style using button and save this state

I have two color styles for my app.
I can change them using button, but i haven't got idea how I could save actually style as domestic. When I restart my app, I have other style than I choose.
Have You got idea how I can save this state? Maybe savedInstanceState?
Thank's for any reply
Have you thought about persisting the changes? You can use SharedPreferences just like that:
// While choosing new style
SharedPreferences pref = getApplicationContext().getSharedPreferences("MyPref", 0);
SharedPreferences.Editor editor = pref.edit();
editor.putInt("style", styleId);
editor.commit(); // commit changes
// While retriving choosen style
SharedPreferences pref = getApplicationContext().getSharedPreferences("MyPref", 0);
int styleId = pref.getInt("style", null); // null is default value
setStyle(styleId); // custom method

EditText hint as shared pref default?

Is it possible to set the EditText "hint" or default text to the value of a sharedpref string? I have 3 EditText, one button, when the button is clicked, the 3 values are saved in SharedPreferences for a later time in the app. As of right now, once the button is clicked the 3 edit texts stay to the values entered, but if you leave the activity and return, they all go back to blank. I'm wondering if there is a way to set them so they are the sharedpref value of the string, and if nothing is saved set them to the default given. Also, how do I set the default value of a sharedpref String? Thanks!
SharedPreferences pref = getApplicationContext().getSharedPreferences("MyPref", 0);
SharedPreferences.Editor editor = pref.edit();
if (mEditTextBench.getText().length() > 0 && mEditTextDead.getText().length() > 0 && mEditTextSquat.getText().length() > 0) {
editor.putString("maxDead", mEditTextDead.getText().toString());
editor.putString("maxSquat", mEditTextSquat.getText().toString());
editor.putString("maxBench", mEditTextBench.getText().toString());
editor.apply();
This is my code in another layout to test to make sure it is properly saving.(which it is) I'm not entirely sure what the "null" is doing though. So if anyone can help with that too thanks!!
String maxDead = pref.getString("maxDead", null);
TextView textViewTest = (TextView) findViewById(R.id.textViewTest);
textViewTest.setText(maxDead);
Doh!! Figured it out! Added this to onCreate:
SharedPreferences pref = getApplicationContext().getSharedPreferences("MyPref", 0);
String maxDeadHint = pref.getString("maxDead", "100");
mEditTextDead.setHint(maxDeadHint);
My bad! Thanks!
Once you make the appropriate replacements, this should work:
TextView deadText = (TextView) findViewById(R.id.//insert id//);
TextView squatText = (TextView) findViewById(R.id.//insert id//);
TextView benchText = (TextView) findViewById(R.id.//insert id//);
deadText.setHint(pref.getString("maxDead", "default_value"));
squatText.setHint(pref.getString("maxSquat", "default_value"));
benchText.setHint(pref.getString("maxBench", "default_value"));

Textview value not incrementing on button click

Iam developing a game in which i have to increment the textview value every time a "Rematch" button is tapped
Iam saving the textview value using shared preference, it works fine.
The Problem is that "if i tapped my 5 times on button then the value is shown 5, then if i closed the app and comes again it shows 5 (so shared pref works fine) but when i tapped on rematch again it again starts the textview value from 1, although my expected result was 6 because 5 was already saved
Here is my code for oncreate method
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.game);
SharedPreferences sp = getSharedPreferences(SP_NAME, MODE_PRIVATE);
tValue = sp.getString("textvalue","");
coins=(TextView)findViewById(R.id.textView1);
coins.setText(tValue);
and on button click iam doing this
count++;
coins.setText(String.valueOf(count));
SharedPreferences sp = getSharedPreferences(SP_NAME, MODE_PRIVATE);
SharedPreferences.Editor sedt = sp.edit();
sedt.putString("textvalue", coins.getText().toString());
sedt.commit();
The problem I think is that count when you start the activity again is 0. You have to make count to start from the value that is saved, this could be a way to do it
count = Integer.valueOf(sp.getString("textValue",""))
The problem is that you use the value of count instead of tValue. So don't do count++ but save tValue as int and increment that one. Also do setText(tValue). Then it will work. Good luck.
That's because count starts again as 0,
and when u press the button it wil increment to 1.
so in your onCreate after you get your saved value back.
assign it to count, like:
count = Integer.parseInt(tValue);
Hope this will help u out.

How to retain changed button text in android?

In my project I have one edit text and one button.
When the project loads and I press the button,
the text of the button changes to the value of edit text.
But when I press the back button the change of button text does not persist.
I have included the snapshot which can describe my problem better.
I basically want the button text change to persist.
Please help.
The text doesn't show up on reopening the app because it was stored temporarily and when you closed your app it got destroyed. To retain the text you can store it in shared preference or file and on app startup load the text of the button from that source and if source is not present(When opening app for the first time) then put the default text on the button.
In your activity onCreate () method, you can add this code:
SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(this);
String btnText = preferences.getString("btnText", "");
if(!btnText.equalsIgnoreCase(""))
{
yourButton.setText(btnText);
}
yourButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(this);
SharedPreferences.Editor editor = preferences.edit();
editor.putString("btnText",yourTextEdit.getText().toString());
editor.apply();
....your code
}
});
In your layout xml file, set your button default text. android:text="#string/yourtext"
You need to save the text somewhere and load it when the activity starts. There are various ways to do this but I think Shared Preferences will work for you.
In the on click function for the button set the Shared Preference and in the Activity's onCreate check if the same Shared Preference is set. If the value is present load it else load the default value.
When you press the button for first time after getting text from edit text use the following code to save the edit text text that to be set on button in shared prefrences:
In on Button Click method:
EditText et = (EditText) findViewById(....);
String text = et.getTex().toString();
then you set it on button and so..
SharedPreferences.Editor editor = getSharedPreferences(MY_PREFS_NAME, MODE_PRIVATE).edit();
editor.putString("buttonText", text);
editor.commit();
then when you relauch the app in onCreate method use the following code:
SharedPreferences prefs = getSharedPreferences(MY_PREFS_NAME, MODE_PRIVATE);
String value= prefs.getString("buttonText", "");
button.setText(value); //whatever you button is

How can i save value of theme set by user to retrieve it later?

This is what i am doing to save value of drawables:
case R.id.purple:
for (Button currentButton : buttons) {
currentButton.setBackgroundResource(R.drawable.purple);
button1 = buttoncos = buttonmadd = R.drawable.purple;
}
editor.putInt("DigitButtonStyle",button1);
editor.putInt("MemoryButtonStyle", buttonmadd);
editor.putInt("FunctionButtonStyle", buttoncos);
editor.commit();
return true;
Drawables here are integeral values so it was easy.How do i store values of different themes.
Easy way to do this, is SharedPreferences
Save the relevant value:
SharedPreferences shared=getSharedPreferences("theme", Activity.MODE_PRIVATE);
shared.edit().putString("theme_name", "THEME_BLUE").commit();
Retrieve the saved data:
SharedPreferences shared=getSharedPreferences("theme", Activity.MODE_PRIVATE);
String theme=shared.getString("theme_name", null);
For more info: link
Read this setTheme in this page, it tells that you cannot use in middle of an activity or in other words, has to be done before setting any theme to your activity.

Categories

Resources