usea savePreferences in dynamically layout -android - android

I'm making an app that contains a dynamically layout and it generates textView's in run time, I need that when I close the application all the textView has been added, don't erase; I think I can do that whit savePreferences(), but What parameters I have to use?
private void savePreferences(What parameter here, What parameter here) {
SharedPreferences sp = PreferenceManager.getDefaultSharedPreferences(this);
Editor edit = sp.edit();
edit.commit();
}
Thank you

Shared Preferences should work. Try this...
public class sample extends Activity {
public static final String PREFS_NAME = "MyPrefsFile";
#Override
protected void onCreate(Bundle state){
super.onCreate(state);
. . .
// Restore preferences
SharedPreferences settings = getSharedPreferences(PREFS_NAME, 0);
boolean silent = settings.getBoolean("silentMode", false);
setSilent(silent);
}
#Override
protected void onStop(){
super.onStop();
// We need an Editor object to make preference changes.
// All objects are from android.context.Context
SharedPreferences settings = getSharedPreferences(PREFS_NAME, 0);
SharedPreferences.Editor editor = settings.edit();
// THIS IS WHERE YOU STORE THE TEXTVIEW DATA
// THERE ARE MANY ' editor.put...(...) ' methods
// ie. editor.putInt(...) , editor.putString(...)
editor.putBoolean("silentMode", mSilentMode);
// Commit the edits!
editor.commit();
}
}

Related

SharedPreferences should remember the content of the TextView when it returns to MainActivity

I have a situation where there is a TextView in MainActivity with some textual content. However, when I go from MainActivity to SecondActivity and return to MainActivity again, the text contained in TextView is lost. I tried to solve this with the help of SharedPreferences and I wrote the code. SharedPreferences does not save when I return from SecondActivity. I really do not see where I am making a problem in this code and I ask your help.
private void SavePreferences(String key, String value){
SharedPreferences sharedPreferences = getPreferences(MODE_PRIVATE);
SharedPreferences.Editor editor = sharedPreferences.edit();
editor.putString(key, value);
editor.apply();
}
private void LoadPreferences()
{
SharedPreferences sharedPreferences = getPreferences(MODE_PRIVATE);
String textstorage = sharedPreferences.getString("TEXT", "");
mytext.setText(textstorage);
}
You should call LoadPreferences() in onResume() because onCreate() won't be called when you return back from Second Activity
#Override
protected void onResume() {
super.onResume();
LoadPreferences();
}
you need to give name to your preference try below code:SharedPreferences.Editor editor = getSharedPreferences(PREF_NAME, PRIVATE_MODE).edit()
editor.putString(key, value);
editor.apply();
editor.commit(); // you can omit this, i use this one
and to get it back SharedPreferences sharedPreferences = getSharedPreferences(PREF_NAME, PRIVATE_MODE);
String textstorage = sharedPreferences.getString(key, "default value"); // default value will be alternate value if you string is not found
mytext.setText(textstorage);

Shared preferences not working correctly

I'm trying to store some strings in a shared preference file and then retrieve them in another activity, except it doesn't seem to be working. Any guidance as to where im going wrong would be much appreciated. Many thanks.
public void save(View view) {
SavePreferences("name", nameS);
SavePreferences("current", currentS);
SavePreferences("goal", goalS);
SavePreferences("CurrentBmi", cBmiS);
SavePreferences("goalBmi", gBmiS);
Toast.makeText(this, "profile Saved", Toast.LENGTH_SHORT).show();
startActivity(new Intent(this, MainActivity.class));
}
private void SavePreferences(String key, String value) {
SharedPreferences sharedPreferences = getPreferences(MODE_PRIVATE);
SharedPreferences.Editor editor = sharedPreferences.edit();
editor.putString(key, value);
editor.commit();
}
public class Progress extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_progress);
SharedPreferences sharedPreferences = getPreferences(Context.MODE_PRIVATE);
String test = sharedPreferences.getString("name", "");
String test2 = sharedPreferences.getString("current", "");
TextView testy = (TextView) findViewById(R.id.textView1);
testy.setText(test);
TextView testz = (TextView) findViewById(R.id.test2);
testz.setText(test2);
}
With the code you have you are limiting the access of sharedpreferences to activity(context) level.
Values saved in activity Activity MainActivity will not be available in activity Progress since you are using getPreferences(MODE_PRIVATE);
Change this to
SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(this);
or
SharedPreferences preferences = getSharedPreferences(PREF_FILE_NAME, MODE_PRIVATE);
getPreferences:
public SharedPreferences getPreferences (int mode)
Retrieve a SharedPreferences object for accessing preferences that are private to this activity.

getting string from preferences

So I have EditTextPreference in my Preferences, I want to type something in and save it, then i want to get that text in Activity. My key of EditTextPreference is B1. I tried this code :
SharedPreferences getPrefs = PreferenceManager.getDefaultSharedPreferences(getBaseContext());
String samples = getPrefs.getString("B1", "");
b1.setText(samples);
<EditTextPreference
android:dialogTitle="Button 1"
android:key="B1"
android:summary="Set text on button 1"
android:title="Set text on button 1" />
I get java.lang.NullPointerException between those
String samples = getPrefs.getString("B1", "lol");
b1.setText(samples);
Declare:
SharedPreferences pref = this.getSharedPreferences("MyPreferences", Context.MODE_PRIVATE);
Put a value (can be int, String, etc)
String tom = "tom";
pref.edit().putString("tom", tom).commit(); // To set a value to SharedPreferences
Get a value (remember the type)
String name = pref.getString("tom", null); // To get a value from SharedPreferences
See SharedPreferences
Try this
Home.java
public class Home extends Activity {
public SharedPreferences prefs;
String mValues;
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
setContentView(R.layout.main);
prefs = PreferenceManager.getDefaultSharedPreferences(this);
mValues= prefs.getString("male", "female");
}
}
Preferences.java
public class Preferences extends PreferenceActivity implements OnSharedPreferenceChangeListener{
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
addPreferencesFromResource(R.xml.setting);
}
#Override
public void onSharedPreferenceChanged(SharedPreferences sharedPref, String key) {
Editor editor = sharedPref.edit();
if(key.equalsIgnoreCase("B1")){
editor.putString("male","my value");
}
editor.commit();
}
}
Let me know if it works for you.
In order to write to SharedPreferences you can use:
Editor editor = getPrefs.edit();
editor.putString("B1", "you value here");
editor.commit();

I want to do registration of my app at the time of installation?

Problems
The activity which will do registration will be shown only once
After registration the control should move to
next main activity
I have used following code
Below code will not meet my requirement?
Any Help will be appreciated!!
Code in Registration Activity
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(this);
SharedPreferences.Editor editor=prefs.edit();
editor.putBoolean("registration", true);
editor.commit();
Code in Main Activity
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(this);
boolean regComplete =prefs.getBoolean("registration", false);
SharedPreferences.Editor editor =prefs.edit();
editor.putBoolean("registration", false);
editor.commit();
if(regComplete)
{
startActivity(new Intent(this, SecureXActivity.class));
} else
{
startActivity(new Intent(this, LoginActivity.class));
}
Just put your Registration code in SecureXActivity.class
and Check for the Registration before setContentView(), if its not done then start LoginActivity.class
And in LoginActivity.class after registration complete put these code,
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(this);
SharedPreferences.Editor editor=prefs.edit();
editor.putBoolean("registration", true);
editor.commit();
If you use this approach then I think you don't need Main Activity class..
And keep in mind this all thing done at time of your application's first run not at the time of installation.
The Registration Activity should be like this:
public class RegistrationActivity extends Activity {
public static SharedPreferences pref;
public static final String PREFS_NAME = "MyPrefsFile";
public void onCreate(Bundle savedInstanceState) {
pref = getSharedPreferences(PREFS_NAME, 0);
boolean regComplete =prefs.getBoolean("registration", false);
if(regComplete){
//go to main class
}else{
//stay in the registration class
}
}
}
and the Main class should be:
public class MainActivity extends Activity {
public void onCreate(Bundle savedInstanceState) {
RegistrationActivity.pref = getSharedPreferences(PREFS_NAME, 0);
SharedPreferences.Editor editor = settings.edit();
editor.putBoolean("registration", true);
// Commit the edits!
editor.commit();
}
}

How to retrieve the default value of a preference defined in the XML file

How do you get the default value of a single Android Shared Preference as it is explicitly defined in the corresponding XML file? E.g.:
<CheckBoxPreference
android:defaultValue="false"
android:key="fulldb"
android:summary="No selection rules apply"
android:title="Use Full Database" />
Like this..
public class Calc extends Activity {
public static final String PREFS_NAME = "MyPrefsFile";
#Override
protected void onCreate(Bundle state){
super.onCreate(state);
. . .
// Restore preferences
SharedPreferences settings = getSharedPreferences(PREFS_NAME, 0);
boolean silent = settings.getBoolean("silentMode", false); //default value if nothing is in the preference is the last parameter false.
setSilent(silent);
}
#Override
protected void onStop(){
super.onStop();
// We need an Editor object to make preference changes.
// All objects are from android.context.Context
SharedPreferences settings = getSharedPreferences(PREFS_NAME, 0);
SharedPreferences.Editor editor = settings.edit();
editor.putBoolean("silentMode", mSilentMode);
// Commit the edits!
editor.commit();
}
}
See below:
To Store the Value:
public static SharedPreferences myPrefs;
public static SharedPreferences.Editor prefsEditor;
myPrefs = this.getSharedPreferences("myPrefs",MODE_WORLD_WRITEABLE);
prefsEditor = myPrefs.edit();
prefsEditor.putBoolean("FullResultIsOn", true); // value to store
prefsEditor.commit();
Now to retrive the value:
myPrefs = this.getSharedPreferences("myPrefs", MODE_WORLD_READABLE);
fullResultSound = myPrefs.getBoolean("FullResultIsOn", false);
Enjoy. :)
Thanks.

Categories

Resources