I have Activity for ListPreference and i can change any values in it , but my question i want to change a value in ListPreference first time without the user enters to activity that there is ListPreference .
First time a user enters to an app i want to save default value depend on location the user.
I found the solution
SharedPreferences sharedPreferences = PreferenceManager.getDefaultSharedPreferences(getActivity());
SharedPreferences.Editor editor = sharedPreferences.edit();
editor.putString(getString(R.string.settings_method_key),repear);
editor.commit();
Related
Hello I am developing a simple project with android studio and I wanted to know how can I change the starting activity of the application via Java Code. I know how to do it with the androidmanifest.xml but I want the user to insert some data in the starting activity and then the next time the user loads the application the main activity pops up directly and not the starting activity again.
Thank you.
Use shared preference, with this you can save limited information. depending on what your user has selected you can save it and start an activity from it. the information remains even after the application is closed.
e.g. so you create a start activity that checks what settings have been selected and depending on that it starts the required activity with an intent.
search for shared preference and intent.
You should save a flag in Shared preference. For example in a first activity that launch save this:
SharedPreferences.Editor editor = getSharedPreferences(MY_PREFS_NAME, MODE_PRIVATE).edit();
editor.putString("name", "isNew");
editor.apply();
And when you want to Retrieve data from preference:
SharedPreferences prefs = getSharedPreferences(MY_PREFS_NAME, MODE_PRIVATE);
String name = prefs.getString("name", "default value");//"default value defined" is the default value.
Then Check if (name == isNew) then start your MainActivity class
I'm working on a practise 7 days exercise app. I set a progree bar on the main activity and want to change progress after days. Means if i complete exercise of day 1 ,progress bar set 15% . and when i complete day2 , progress set 30%. I can do it without shared preference , it working correctlt but when after day1 complete i closed the app it again set progress from 0 . So i want to use shared preferences for this reason . Kindly someone guide me regarding this issue;
For Set Value to Shared Prefrence
SharedPreferences.Editor editor = getSharedPreferences("ProgressBarData",
MODE_PRIVATE).edit();
editor.putInt("progress", 15);
editor.apply();
for get value From Shared Prefrence
SharedPreferences prefs = getSharedPreferences(ProgressBarData,
MODE_PRIVATE);
int progress = prefs.getInt("progress", 0);
1st ur know ur mistake
u can't store ur data in local variable because at the end of the activity it destroyed every things and when u come back to android activity it will be started all thing once again and every thing is restarted
[https://developer.android.com/guide/components/activities/activity-lifecycle
read this u have better understanding
Now ur solution
if u want to store the data and process every day better to use local storage like
Sqlite ,room or shared preference.
Step to do the task
There is three step to store, get and remove data into share preference
For storing , getting deleting data
//storing
SharedPreferences.Editor editor = context.getSharedPreferences(name,Context.MODE_PRIVATE).edit();
editor.putString(key, data);
editor.apply();
//getting
SharedPreferences getSharedPrefrence = context.getSharedPreferences(name, Context.MODE_PRIVATE);
int data = getSharedPrefrence.getInt(key, IntegerValuesAndStringValues.REGISTER_BEFORE_LOGIN);
return data;
BasicFunctions.removeSharedPrefrences(getContext(),"Name of the preference");
I'm using the library android material app rating (https://github.com/stepstone-tech/android-material-app-rating)
and I don't know how to save in SharedPreferences that the user has already done the review or rating.
I can save information in SharedPreferences if it's a TextView, but in this case I have no idea what to do.
Can you help me?
Example of how to save the info whether the user has rated you app:
SharedPreferences sharedPreferences = getSharedPreferences("MyShared", MODE_PRIVATE);
SharedPreferences.Editor editor = sharedPreferences.edit();
editor.putBoolean("HasUserRating", true);
editor.apply();
Read back from SharedPreferences that the user has or has not rated:
SharedPreferences sharedPreferences = getSharedPreferences("MyShared", MODE_PRIVATE);
boolean hasRated = sharedPreferences.getBoolean("HasUserRating", false);
(Note: This will return false if the SharedPreferences key "HasUserRating" is not present.)
Use the callbacks within RatingDialogListener to save a value to SharedPreferences. Check for the existence of that SharedPreferences value prior to displaying the dialog.
I have many buttons in a page, now I what want do is, every account can select one button, when they select that button, that button will be disabled and when account is logged out, open application again ,the button should be still disabled. If anyone know the answer, please kindly help me.
Thanks.
I am using android studio and my login and register using SQLite.
When you click logout button, please save a variable to SharePreference:
SharedPreferences sharedpreferences = getSharedPreferences("Name", Context.MODE_PRIVATE);
SharedPreferences.Editor editor = sharedpreferences.edit();
editor.putBoolean("isLogout", true);
editor.commit();
Now application will keep variable isLogout = true.
Next time when come to that screen, you just need to get this variable to check:
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(this);
\\the true which is second parameter is default value that you want if isLogout variable is null
if( prefs.getBoolean("isLogout", true)){
button.disable
};
You should use Shared Preferences in the application for storing button visibility value. When the button is clicked then give your preference some value and when logging out of the application simply clear that shared preference value so that button will not be disabled any more.
Put this code inside click listener of button
SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences((MY_PREFS_NAME, MODE_PRIVATE).edit();
SharedPreferences.Editor editor = preferences.edit();
editor.putString("value","buttondisabled");
editor.apply();
Put this code in the onCreate method of your activity
SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(this);
String name = preferences.getString("value", "");
if (value.equals("buttondisabled")
{
yourbutton.setVisibilty(View.VISIBLE)
}
and when logging out of the application simply write these lines:
Editor editor = getSharedPreferences("MY_PREFS_NAME", Context.MODE_PRIVATE).edit();
editor.clear();
editor.commit();
Thats it :)
Say ActivityA with two buttons. Each of these button will open ActivityB but with a different fragment respectively. Both fragment contain an EditText. If I want to switch fragment while in ActivityB, I need to return to ActivityA and press the other button.
Now what I want to do is to save the value entered in each EditText when I switch fragment or close the app and repopulate the value in the right EditText when I re-open the fragment.
It seems to do it by it self when I open the SettingActivity then come back, but not if I destroy the activity. In the end I want the fragment to re-open just as I left it. Thank you.
You must save value and then restore them.A good way is Shared Preferences.
1-save:
SharedPreferences sharedpreferences = getSharedPreferences("mypref", Context.MODE_PRIVATE);
SharedPreferences.Editor editor = sharedpreferences.edit();
editor.putString("var1", edittext.getText().toString());
editor.commit();
2-restore:
String s = sharedpreferences.getString("var1","DEF");
Use SharedPreference to store the value
When you call your new activity use this
SharedPreferences settings = PreferenceManager.getDefaultSharedPreferences(getBaseContext());
SharedPreferences.Editor editor = settings.edit();
editor.putString("userEmail", youredittext.getText().toString());
editor.commit();
When you want to retrieve the stored value in any of your activities, get it like this
SharedPreferences getPrefs = PreferenceManager
.getDefaultSharedPreferences(getBaseContext());
String mUserEmail = getPrefs.getString("userEmail", null);
Yes if you have more fields or like you want to maintain relation for data.. in that case you also can use SQLite. Below is post on SQLite
http://www.kpblogs.com/mobile-development/sqlite-android-tutorial-with-crud-operations/