How to save setText? - android

final Button haa = (Button) findViewById(R.id.haactiv);
haa.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
dhas = (EditText) findViewById(R.id.dha);
haout = (TextView) findViewById(R.id.haoutput);
haout.setText(dhas.getText());
}
});
I don't know how to save the output haout from the getText dhas for the next time I start the app.
Thanks for help!

You can save it in the shared preferences. Example:
SharedPreferences sharedPref = getActivity().getPreferences(Context.MODE_PRIVATE);
SharedPreferences.Editor editor = sharedPref.edit();
editor.putInt(getString(R.string.saved_high_score), newHighScore);
editor.commit();
To read from the shared preferences:
SharedPreferences sharedPref = getActivity().getPreferences(Context.MODE_PRIVATE);
int defaultValue = getResources().getInteger(R.string.saved_high_score_default);
long highScore = sharedPref.getInt(getString(R.string.saved_high_score), defaultValue);
Some more information about shared preferences here

If you need save data permanente.
Try save in a .txt file.

Related

OnButton Click Share Preferences

Below is my Code :
public void OnAttendLogin(View view) {
sp = getSharedPreferences("attendlogin", MODE_PRIVATE);
String emp_id = sp.getString("emp_id", null);
InTImeWorker inTImeWorker = new InTImeWorker(this);
inTImeWorker.delegate = (AsyncResponse) this;
inTImeWorker.execute(emp_id);
//shared pref for saving In_time in textview
sp = getSharedPreferences("InTime", MODE_PRIVATE);
SharedPreferences.Editor editor = sp.edit();
String in_time_sharedpref = In_time.getText().toString();
editor.putString("in_time_sp", in_time_sharedpref);
editor.apply();
editor.commit();
out_time_button.setEnabled(true);
in_time_button.setEnabled(false);
}
I want to know what wrong I am Doing in Code ?
How i can use two shared Preferences in android studio ??
You are doing wrong that you are using same reference for both shared preference.
Yes, you can use two shared Preference at one button click.
Create two shared Preference object.
SharedPreferences sp;
SharedPreferences sp2;
SharedPreferences.Editor editorSp ;
SharedPreferences.Editor editorSp2 ;
sp = getSharedPreferences("attendlogin", MODE_PRIVATE);
sp2 = getSharedPreferences("InTime", MODE_PRIVATE);
Final code will be like this:
public void OnAttendLogin(View view) {
sp = getSharedPreferences("attendlogin", MODE_PRIVATE);
String emp_id = sp.getString("emp_id", null);
InTImeWorker inTImeWorker = new InTImeWorker(this);
inTImeWorker.delegate = (AsyncResponse) this;
inTImeWorker.execute(emp_id);
//shared pref for saving In_time in textview
sp2 = getSharedPreferences("InTime", MODE_PRIVATE);
SharedPreferences.Editor editor = sp2.edit();
String in_time_sharedpref = In_time.getText().toString();
editorsp2.putString("in_time_sp", in_time_sharedpref);
editorsp2.apply();
editorsp2.commit();
out_time_button.setEnabled(true);
in_time_button.setEnabled(false);
}
Use one sharedpreference name
"sp = getSharedPreference("attendlogin",MODE_PRIVATE);"
and use different different variable to save data
example:
SharedPreferences.Editor editor = sp.edit();
String in_time_sharedpref = In_time.getText().toString();
editor.putString("in_time_sp", in_time_sharedpref);
editor.apply();
String emp_id = sp.getString("emp_id", null);
i.e. sp should be same with name ""

Android 4.2 SharedPreferences returns wrong value

I'm trying to get some values from SharedPreferences, and I write it in this code;
SharedPreferences prefs = getPreferences(MODE_PRIVATE);
SharedPreferences.Editor editor = prefs.edit();
editor.putInt(MainActivity.OBSDONE, observationer);
editor.putInt(MainActivity.COROBS, korrekte);
editor.commit();
I got the data from the SharedPreferences-file by pulling it off the Virtual Device, and the data looks correct.
When I try to pull it out from SharedPreferences with;
SharedPreferences prefs = getPreferences(MODE_PRIVATE);
int obs = prefs.getInt(OBSDONE,0);
int cor = prefs.getInt(COROBS,0);
it returns 0 to both values?
Use this
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(getBaseContext());
prefs.edit().putInt(MainActivity.OBSDONE,observationer).apply();
prefs.edit().putInt(MainActivity.COROBS, korrekte).apply();
This worked for me:
pref = PreferenceManager.getDefaultSharedPreferences(this.getApplicationContext());
Or if u only have a context:
pref = PreferenceManager.getDefaultSharedPreferences(context.getApplicationContext());

SharedPreferences in Android

I have used SharedPreferences to save some details in the Main Activity .Now i want to get the details in some other activity but i am not able to get them .
Code to save
public void saveInformation(String username,String password) {
SharedPreferences shared = getSharedPreferences("shared", MODE_PRIVATE);
SharedPreferences.Editor editor = shared.edit();
editor.putString("username", username);
editor.putString("password", password);
editor.commit();
}
Code to get
SharedPreferences prefs = getPreferences(MODE_PRIVATE);
String username = prefs.getString("username", null);
But this is not working for me . How could I get his?
May this help you:
Buddy Change these lines in your code to get:
SharedPreferences prefs = getSharedPreferences("shared", MODE_PRIVATE);
String username = prefs.getString("username","");
Code to get should be
SharedPreferences prefs = getSharedPreferences("shared", MODE_PRIVATE);

SharedPreferences keep getting default value

I keep Getting the Default value either my UI will display null or if I use integers it displays that default value as well here it is in the string form plz help
//putting the information in shared preferences
TextView pScore1=(TextView)findViewById(R.id.pScore1f);
SharedPreferences peepsScores2= PreferenceManager.getDefaultSharedPreferences(GamePlayFirst.this);
SharedPreferences.Editor editor2 =peepsScores2.edit();
String userScore11 = pScore1.getText().toString();
editor2.putString("userScore11",userScore11);
editor2.commit();
//getting it and editing it
SharedPreferences peepsScores2 = PreferenceManager.getDefaultSharedPreferences(this);
int u;
int one =1;
int newUsrScore1=1;
String userScore11 = peepsScores2.getString("userScore11",null);
u=Integer.parseInt(userScore11);
newUsrScore1 = u+one;
String newUserScore1 = Integer.toString(newUsrScore1);
SharedPreferences.Editor editor = peepsScores2.edit();
editor.putString(newUserScore1, NewUserScore1);
editor.commit();
//getting it and displaying it on the UI
SharedPreferences peepsScores2 = PreferenceManager.getDefaultSharedPreferences(this);
String userScore11 = peepsScores2.getString("NewuserScore1",null);
pScore1.setText(" "+userScore11);
I have added some comment to you code please check:
//putting the information in shared preferences
TextView pScore1=(TextView)findViewById(R.id.pScore1f);
SharedPreferences peepsScores2=
PreferenceManager.getDefaultSharedPreferences(GamePlayFirst.this);
SharedPreferences.Editor editor2 =peepsScores2.edit();
String userScore11 = pScore1.getText().toString();
editor2.putString("userScore11",userScore11);
editor2.commit();
//getting it and editing it
SharedPreferences peepsScores2 = PreferenceManager.getDefaultSharedPreferences(this);
int u;
int one =1;
int newUsrScore1=1;
String userScore11 = peepsScores2.getString("userScore11",null);
u=Integer.parseInt(userScore11);
newUsrScore1 = u+one;
String newUserScore1 = Integer.toString(newUsrScore1);
SharedPreferences.Editor editor = peepsScores2.edit();
//#Praful: here newUserScore1 seems to be integer value and you are storing
//null here. I think it it should be
//`editor.putString("NewuserScore1", newUsrScore1);`
editor.putString(newUserScore1, null);
//#Praful: call commit here
editor.commit;
//getting it and displaying it on the UI
SharedPreferences peepsScores2 = PreferenceManager.getDefaultSharedPreferences(this);
String userScore11 = peepsScores2.getString("NewuserScore1",null);
pScore1.setText(" "+userScore11);
This line
editor.putString(newUserScore1, null);
should be
editor.putString("NewuserScore1",newUserScore1);
and also don't forget to commit your changes using editor.commit();
Whenever you working with SharedPreference never forget to call commit() to save your changes.
SharedPreferences.Editor editor = peepsScores2.edit();
editor.putString("NewuserScore1", newUserScore1);
editor.commit();

Android: string value is not getting in Shared Preference

I have created a shared preference for a boolean value and for a string value. The boolean value is gotten in another activity. But for the string I am only getting default value.
Home.class
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(this);
SharedPreferences.Editor spe = prefs.edit();
spe.putBoolean("flag", true);
spe.putString("user", "hello");
spe.commit();
welcome.class
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(this);
boolean flag= prefs.getBoolean("flag", false);
String user=prefs.getString("user","Nothing");
TextView tv = new TextView(this);
tv.setText("Flag : "+flag+(" User : "+user);
For 'user', only 'Nothing' is displaying. Where should I correct my code?
Try using:
SharedPreferences settings = getSharedPreferences(appName,0);
settings.getBoolean("flag", true);
settings.getString("user", "hello");
And to put:
SharedPreferences settings = getSharedPreferences(appName,0);
SharedPreferences.Editor editor = settings.edit();
editor.putBoolean("flag",true);
editor.putString("user","hello");
editor.commit();
This is what I use in my application, and it shares booleans/ints/strings accrossed many many Classes
Note: appName doesn't have to be the app name, like in the official tutorial.

Categories

Resources