How to save a review or rating with Shared Preferences? - java

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.

Related

How can i change value ListPreference from another Activity

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

Android storing user ID after login

I was wondering what the best way is to store an user ID after logging the user in? I have just started Android Development, and have read about the Shared Preferences, but this does not seem to work?
This is happening in my LoginActivity:
User user = dbHelper.findUser(email);
SharedPreferences preferences = getSharedPreferences("preferences", MODE_PRIVATE);
SharedPreferences.Editor editor = preferences.edit();
editor.putString("user_id", user.getId());
editor.commit();
Intent mainIntent = new Intent(LoginActivity.this, MainActivity.class);
startActivity(mainIntent);
In my MainActivity I would like to obtain the logged in user's ID as follows:
SharedPreferences preferences = getSharedPreferences("preferences",
MODE_PRIVATE);
String user_id = prefs.getString("user_id", "0");
Now, I need this user id in order to build lists based on the user, so I do database queries with this ID. What's the best way to do pass on this ID? I could just pass it on with intents, but storing it somewhere central is preferable. I do need this ID in multiple activities.
Solved. Like user CommonsWare pointed out here, the value did not return the one I intended, therefore always choosing the default value from my SharedPreferences.

The button still disabled after login again into application

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

Saving a value when destroying an activity, then restore 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/

get SharedPreferences from a service

I am trying to access shared preferences from a service. I have used the the following to save the value of text to a string...
SharedPreferences sharedPreferences = getPreferences(MODE_PRIVATE);
SharedPreferences.Editor editor = sharedPreferences.edit();
editor.putString("Ignore1_value", Example.getText().toString());
editor.commit();
But, how would i get the value in a service? Everything i have tried returns as nothing. Any help would be perfect and much appreciated?
I looked through some other questions as well with no solution. I have came up with this, but like i said it returns it as no text.
Context ctx = getApplicationContext();
SharedPreferences sharedPreferences = PreferenceManager.getDefaultSharedPreferences(ctx);
String example1string = sharedPreferences.getString("Ignore1_value","");
Log.i("**GetSettings", example1string);
I'm always using PreferenceManager.getDefaultSharedPreferences(context). This is the same for all Contexts in your application.
A Service is a Context itself, so this would be sufficient:
PreferenceManager.getDefaultSharedPreferences(this);

Categories