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
Related
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"));
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
I've a strange issue with SharedPreferences and boolean.
I've set this code in my xml:
xml:
<CheckBoxPreference
android:key="onlywifiupload"
android:defaultValue="true"
android:summary="#string/summary_onlywifiupload"
android:title="#string/title_onlywifiupload"
/>
and from the Java code, I'm calling:
boolean onlywifiupload = pref.getBoolean("onlywifiupload", true);
Even the checkbox is checked or unchecked, in onlywifiupload there's always true.
Same with setting:
boolean onlywifiupload = pref.getBoolean("onlywifiupload", false);
It seems the default value is always loaded instead of checked values.
It seems the only way to make it working is:
mPrefs = PreferenceManager.getDefaultSharedPreferences(MainActivity.this);
onlywifiupload = mPrefs.getBoolean("onlywifiupload", true);
don't know why I need to call getDefaultSharedPreferences from PrefenceManager object
Before that, I was calling the preferences in this way:
pref = getSharedPreferences("AppPref", MODE_PRIVATE);
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.
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();