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"));
Related
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
I have a text view called tv that takes content from a web page. I want to store the content of that text view on button click but I don't want that the content of one day overwrite the content of the previous day ( for example today tv=ok and tomorrow tv=ok1 , I want to store ok and ok1 not only ok1)
Of course with shared preferences, I can store that text but I don't want to overwrite the text of the previous day
TextView tv = (TextView) findViewById(R.id.oks);
tv.setText(result[1].toString());
//in pass there is the text of tv(in main activity)
SharedPreferences ok = getSharedPreferences("pass", 0);
String str = ""+ok.getString("textvalue","");
SharedPreferences.Editor editor = ok.edit();
editor.putString("textvalue",str);
editor.apply();
TextView textView=(TextView) findViewById(R.id.preferiti);
textView.setText(ok.getString("textvalue",""));
This is your textview
//this are two fields which you want to append and getting somehow from API
String todayTv="ok" , tomorrowTv="ok1";
TextView tv = (TextView) findViewById(R.id.oks);
Let's set todayTv value on textView
tv.setText(todayTv);
now you want to store this value
SharedPreference sharedPref= getSharedPreferences("pass", 0);
Editor edit=sharedPref.edit();
edit.putString("textvalue",tv.getText().toString()); //getting value from textview
edit.apply();
Saved ! lets say you have now tomorrowTv which you want to show with your previous result i.e like this ok ok1
tv.setText(sharedPref.getString("textvalue","")+" "+tomorrowTv); //Concat strings and set text
Old Value - sharedPref.getString("textvalue","")
New Value - tomorrowTv
Now save it again with the above code for future
SharedPreference sharedPref= getSharedPreferences("pass", 0);
Editor edit=sharedPref.edit();
edit.putString("textvalue",tv.getText().toString()); //It will give you final string containg old and new value into one string
edit.apply();
Hope it will give you insight about It.
Read More : https://developer.android.com/training/basics/data-storage/shared-preferences.html
https://developer.android.com/reference/android/widget/TextView.html
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 have editText in Activity. I used SharedPreference for saving this value and get later. I passed this edittext value to another activity button text. Initially i need to hide the button. If edittext values are coming from sharedPreference i need to display the button
code:
Activity:
SharedPreferences preferences = getSharedPreferences("sample",0);
SharedPreferences.Editor editor = preferences.edit();
editor.putString("Name",et.getText().toString());
editor.putString("Name1",et1.getText().toString());
editor.commit();
Intent intent = new Intent(Activity.this, Activity1.class);
startActivity(intent);
Activity1:
btn=(Button)findViewById(R.id.btn);
btn.setVisibility(View.GONE);
SharedPreferences preferences = getSharedPreferences("sample",0);
if(preferences){
btn.setVisibility(View.VISIBLE);
btn.setText(preferences.getString("Name", ""));
}
And also btn setText with Name and this button having values of Name, Name1 editText values
btn=(Button)findViewById(R.id.btn);
btn.setVisibility(View.GONE);
SharedPreferences preferences = getSharedPreferences("sample",0);
String Namestr=(preferences.getString("Name",""));
if(Namestr.length>0){
btn.setVisibility(View.VISIBLE);
btn.setText(preferences.getString("Name", ""));
}
i hope its useful to you.
Debug this condition Your if(preference) condition is wrong.
if(preferences){
btn.setVisibility(View.VISIBLE);
btn.setText(preferences.getString("Name", ""));
}
btn=(Button)findViewById(R.id.btn);
btn.setVisibility(View.GONE);
SharedPreferences preferences = getSharedPreferences("sample",0);
if(preferences.contains("Name")){
String Namestr=(preferences.getString("Name",""));
btn.setVisibility(View.VISIBLE);
btn.setText(Namestr);
}
Oki i think first you should have a validation condition while putting into shared preference.
And then use this code. Coz in my case i have edittext hint text so chek it before putting in
shared preference.
LinearLayout layout = (LinearLayout) findViewById(R.id.linear)
SharedPreferences preferences = getSharedPreferences("sample",0);
String Namestr=(preferences.getString("Name",""));
if(Namestr.length>0){
Button button = new Button(this);
btn.setText(preferences.getString("Name", ""));
layout.addView(button);
}
OK here is my problem. I have a spinner and every option in spinner should edit textview in my layout (option in spinner you can see in position command), so I have made it with SharedPreferences, it is not problem, when I select some option in spinner, so the textview is changed, but when I kill my app and then re-open, the textview is default (the same as defined in my layout). So where is the problem?
First Activity:
if(position == 4){
SharedPreferences myPrefsUv = CPUActivity.this.getSharedPreferences("myPrefsUv", MODE_PRIVATE);
SharedPreferences.Editor prefsEditorUv = myPrefsUv.edit();
prefsEditorUv.putString(maxUv, "1");
prefsEditorUv.commit();
}
if(position == 5){
SharedPreferences myPrefsUv = CPUActivity.this.getSharedPreferences("myPrefsUv", MODE_PRIVATE);
SharedPreferences.Editor prefsEditorUv = myPrefsUv.edit();
prefsEditorUv.putString(maxUv, "2");
prefsEditorUv.commit();
}
if(position == 6){
SharedPreferences myPrefsUv = CPUActivity.this.getSharedPreferences("myPrefsUv", MODE_PRIVATE);
SharedPreferences.Editor prefsEditorUv = myPrefsUv.edit();
prefsEditorUv.putString(maxUv, "3");
prefsEditorUv.commit();
}
if(position == 7){
SharedPreferences myPrefsUv = CPUActivity.this.getSharedPreferences("myPrefsUv", MODE_PRIVATE);
SharedPreferences.Editor prefsEditorUv = myPrefsUv.edit();
prefsEditorUv.putString(maxUv, "4");
prefsEditorUv.commit();
}
Second Activity:
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.my_layout);
SharedPreferences myPrefsUv = UVActivity.this.getSharedPreferences("myPrefsUv", MODE_PRIVATE);
String prefNameUv = myPrefsUv.getString(maxUv, "");
if(prefNameUv == "1"){
final TextView textUv = (TextView)findViewById(R.id.text_max);
textUv.setText("100");
}
if(prefNameUv == "2"){
final TextView textUv = (TextView)findViewById(R.id.text_max);
textUv.setText("110");
}
if(prefNameUv == "3"){
final TextView textUv = (TextView)findViewById(R.id.text_max);
textUv.setText("120");
}
if(prefNameUv == "4"){
final TextView textUv = (TextView)findViewById(R.id.text_max);
textUv.setText("130");
}
}
Thank you.
You are adding the different values with the same key? This way I think it will always return the first match. I don't see in your code "maxUv" to be assigned different values, according to the different cases.
1) I suggest you move to if/else construct. This way, when you're in the right case, the remaining conditions are not evaluated.
2) call the following lines outside the if/else construct. You don't need to get the SharedPreferences Editor in every case.
SharedPreferences myPrefsUv = CPUActivity.this.getSharedPreferences("myPrefsUv", MODE_PRIVATE);
SharedPreferences.Editor prefsEditorUv = myPrefsUv.edit();
Same for the acquiring of TextView textUv in the second Activity. Just acquire reference once, and use the same reference in all cases.
3) In the second Activity, you're also comparing Strings with == operator, but Strings should be compared with equals().
The other guy beat me to it. the old equals stuff.
You're comparing a String using "==". You should compare using .equals:
if(prefNameUv.equals("1"))
This is what you need.
Besides, try not to declare the preferences file. In your case, you're not needing it, so there is no need to make it more complex. Use the following...
First activity:
SharedPreferences myPrefsUv = PreferenceManager.getDefaultSharedPreferences(CPUActivity.this);
Second activity:
SharedPreferences myPrefsUv = PreferenceManager.getDefaultSharedPreferences(UVActivity.this);