Display button with sharedpreference value - android

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);
}

Related

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"));

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 parse a given edittext value from one activity to another? the value should overwrite a given textview in the new activity

I am sort of new to android programming. I have a series of EditText fields and I want the program to allow a user to input a value into the EditText field and whatever value that is input should be transferred to another activity where the value overwrites the content of a textview. How can I do this?
final Intent intent = new Intent(this, AnotherActivity.class);
intent.putExtra(AnotherActivity.KEY_EXTRAS_MESSAGE_AUTHOR, this.myEditText.getText().toString()));
startActivity(intent);
in AnotherActivity:
public class AnotherActivity extends Activity {
public static final String KEY_EXTRAS_MESSAGE_AUTHOR= "author";
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_another);
String author = getIntent().getStringExtra(KEY_EXTRAS_MESSAGE_AUTHOR);
You can do this as shown below:
Define Edit Text in the activity:
EditText editText = (EditText) findViewById(R.id.editText);
Read the value from edit text:
String readValue = editText.getText().toString();
Create an Intent and pass the value to another activity:
Intent intent = new Intent(this, anotherActivity.class);
//Passing the string here
intent.putExtra("value", readValue);
startActivity(intent);
In your anotherActivity catch the intent and set the value to edit text:
Intent intent = getIntent();
String result = intent.getStringExtra("value");
Define Edit Text in the activity:
EditText editText = (EditText) findViewById(R.id.editText);
set the text:
editText.setText(result);
If you do not want to fire the activity by using startActvity(intent), you can use the following procedure:
Save the edit text value in Shared preferences:
Define Edit Text in the activity:
EditText editText = (EditText) findViewById(R.id.editText);
Read the value from edit text:
String readValue = editText.getText().toString();
Save the value:
SharedPreferences sharedPreferences = getSharedPreferences("FileName", Context.MODE_PRIVATE);
SharedPreferences.Editor editor = sharedPreferences.edit();
editor.putString("value", readValue);
editor.commit();
In another activity use it as shown below
SharedPreferences sharedPreferences = getSharedPreferences("FileName", Context.MODE_PRIVATE);
String result = sharedPreferences.getString("value");
Define Edit Text in the activity:
EditText editText = (EditText) findViewById(R.id.editText);
set the text:
editText.setText(result);

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.

How save changed TextView with SharedPreferences?

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);

Categories

Resources