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
Related
There is this thing I want to achieve, I have searched all questions on SO and no answer related to this, so I guess no one has asked which makes me curious.
On my XML, Android studio app, I added a Linear Layout, under the Linear layout, I added a 《TextView with text "Dismiss"》.
I want to set an onclick listener to this textview, once user clicks this TextView with text "Dismiss" I don't want that user to see that Linear Layout again on the page until they reinstall the app.
Logically, the Linear Layout will be like a notice to highlight something on my app when users arrive at that page, but instead of that notice to stay there forever I want user to be able to dismiss it which shows they have gotten the notice.
My workings:
Since I want each app user to decide not to see that text again after clicking dismiss, I need to use shared preference on that TextView with text "Dismiss", so whenever app gets the value of this shared preference then it will hide that layout.
I can set the LinearLayout to be invisible if that value from shared preference is found when stored on the app
The question now, how will I set this shared preference and which code will I use to make the Layout "GONE" or "INVISIBLE" if the TextView with text "Dismiss" is clicked?
Your intelligent solution to this will be much appreciated.
check this out , its in Java, and this will hide the layout after clicking the dismisstextview and when u re open it alert will still be hidden.
public class MainActivity extends AppCompatActivity {
SharedPreferences sharedPref;
SharedPreferences.Editor editor;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
sharedPref = getSharedPreferences(getApplicationContext().getPackageName(),0);
editor = sharedPref.edit();
LinearLayout alertLayout = (LinearLayout)findViewById(R.id.alertLayout);
TextView textViewDismiss = (TextView)findViewById(R.id.textViewDismiss);
if(sharedPref.getBoolean("isLayoutAlertShown",false)){
alertLayout.setVisibility(View.GONE);
}else{
alertLayout.setVisibility(View.VISIBLE);
}
textViewDismiss.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
alertLayout.setVisibility(View.GONE);
editor.putBoolean("isLayoutAlertShown",true);
editor.apply();
}
});
}
}
I would recommend you to use SharedPreferences since this is related to setting matter. You can refer to this link: https://developer.android.com/training/data-storage/shared-preferences#java
You have to call this method at the onCreate. So it will check if the there is showLayout stated true or false, then it will update the design.
//Inside onCreate
private void initSharedPreference(final Activity activity, final LinearLayout linearLayout) {
final SharedPreferences sharedPref = activity.getPreferences(Context.MODE_PRIVATE);
if (sharedPref.getBoolean("showLayout", false)) {
linearLayout.setVisibility(View.GONE);
} else {
linearLayout.setVisibility(View.VISIBLE);
}
}
At the setOnClickListener you need to implement this method.
//At the button
private void setSharedPrefShowLayout(final Activity activity, final Boolean isVisible) {
final SharedPreferences sharedPref = activity.getPreferences(Context.MODE_PRIVATE);
final SharedPreferences.Editor editor = sharedPref.edit();
editor.putBoolean("showLayout", isVisible);
editor.apply();
}
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
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"));
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.