Android : Value of check box in SharedPreferences is not reflected at UI - android

In onResume of my PrefereneActivity, I have the following code :
SharedPreferences sp = PreferenceManager.getDefaultSharedPreferences(getApplicationContext());
boolean isEnabled = sp.getBoolean("check_enabled", false);
The value of isEnabled is false, however, in the UI the checkbox is still shown as selected. Why is it so?

Do you call mCheckBox.setChecked(isEnabled); in onResume() of your activity?
mCheckBox is your checkbox.

I think you are not using Editor.commit(); after putting the values..
Editor editor = mypreferences.edit();
editor.putBoolean("check_enabled", checkboxb.isChecked());
editor.commit();

Related

using sharedpreferences to remember textview hidden after it's clicked

I have a textview that when user clicks it, I want it to be invisible. But I want it to stay invisible, even after user has reloaded the app, using "sharedpreferences". How to get shared preferences working and then recalled correctly?
This is my code for storing the preference when user clicks the textview:
textView.setVisibility(View.INVISIBLE);
SharedPreferences pref = getApplicationContext().getSharedPreferences("MyPref", MODE_PRIVATE);
SharedPreferences.Editor editor = pref.edit();
editor.putBoolean("was_clicked", true);
editor.commit(); // commit changes
So I understand this will store a true/false boolean in shared preferences called" was_clicked".
But now how do I have it checked for "true" in the onCreate method of the activity and then have it set TextView to view.INVISIBLE if was_clicked = true?
Check out this article. But to answer your question specifically
if (getApplicationContext().getSharedPreferences("MyPref", MODE_PRIVATE).getBoolean("was_clicked", false))
textView.setVisibility(View.INVISIBLE);
You can check the value using this method:
SharedPreferences pref = getApplicationContext().getSharedPreferences("MyPref", MODE_PRIVATE);
boolean wasClicked = pref.getBoolean("was_clicked",false);
if (wasClicked){
textView.setVisibility(View.INVISIBLE);
}else {
textView.setVisibility(View.VISIBLE);
}
In your onCreate method you should create preferences and read from them like this:
static final String PREFERENCES_NAME = "MyPref";
static final String WAS_CLICKED_NAME = "was_clicked";
#Override
void onCreate()
{
SharedPreferences preferences = getApplicationContext().getSharedPreferences(PREFERENCES_NAME, MODE_PRIVATE);
boolean invisible = preferences.getBoolean(WAS_CLICKED_NAME, false); // 2nd argument is the default value
if (invisible)
{
textView.setVisibility(View.INVISIBLE);
}
}
getBoolean method will read value of "was_clicked" preference. 2nd argument will be returned if you haven't yet put value "was_clicked" to shared preferences, so you can return false if you haven't put this preference to show the textView.

Have Multiple Preferences within SharedPreferences for Checkboxes

How can I have a separate preference for each checkbox saving whether it was clicked or not? I have code that works for one and I can repeat it to make it work for each one but I feel I should be able to change it so I don't have so much repeat code.
In onCreate
CheckBox checkbox = (CheckBox) findViewById(R.id.description);
CheckBox checkbox1 = (CheckBox) findViewById(R.id.description1);
SharedPreferences sharedPref = getSharedPreferences("mypref", MODE_PRIVATE);
SharedPreferences sharedPref1 = getSharedPreferences("mypref1", MODE_PRIVATE);
boolean checked = sharedPref.getBoolean("checked", false);
boolean checked1 = sharedPref1.getBoolean("checked1", false);
checkbox.setChecked(checked);
checkbox1.setChecked(checked1);
My click handler
public void handleCheckBoxClick(View view) {
CheckBox tmpChkBox = (CheckBox) findViewById(view.getId());
if(tmpChkBox.isChecked())
{
tmpChkBox.setBackgroundColor(Color.BLUE);
SharedPreferences sharedPref = getSharedPreferences("mypref", MODE_PRIVATE);
sharedPref.edit().putBoolean("checked", true).commit();
}
else
{
tmpChkBox.setBackgroundColor(Color.RED);
SharedPreferences sharedPref = getSharedPreferences("mypref", MODE_PRIVATE);
sharedPref.edit().putBoolean("checked", false).commit();
}
}
I got it to work by making another click handler and referring to that which you see in the onCreate where I make a whole separate preference. Is there a way to make one preference that can be used on each check box?

how to save state of dynamically created checkbox in android

i am creating number of check boxes dynamically and i have done some logic to select only one at a time . now i just want to save the state of the selected checkbox and when i come back from the next screen it should be selected. below i have given the code for checkbox
cb.setOnCheckedChangeListener(new OnCheckedChangeListener() {
#Override
public void onCheckedChanged(CompoundButton buttonView,
boolean isChecked) {
if (isChecked) {
if (hash.size() > 0) {
hash.get("1").setChecked(false);
}
hash.put("1", buttonView);
selAnyone = true;
} else {
hash.clear();
selAnyone = false;
}
Edit:
Whenever you are navigating to the current activity to the next activity , save the id of selected check box like this
SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(this);
SharedPreferences.Editor editor = preferences.edit();
editor.putString("checkboxid","1");
editor.commit();
or simply send it via Intent
yourintent.putExtra("checkboxid","1"); // selected checkbox id
And then
If you are using the intent instead of sharedpreferences put the same extra when navigating back to the current screen.
First using SharedPreferences:
In the onCreate of your Activity check whether pref contains checkbox id or not
SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(this);
String name = preferences.getString("Name",null); // if the preference doesn't exist it returns null
if(name!=null)
{
int checkedid=Integer.parseInt(name);
checkbox[checkid].setChecked(true);
}
Same logic follows for the intent extra also check for checkboxid , if it doesn't exist don't check anything, if it exists check the checkbox with assoicated id
Use SharedPreferences
Example:
SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(this);
SharedPreferences.Editor editor = preferences.edit();
editor.putString("checkboxid","1");
editor.commit();
Get like this
SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(this);
String name = preferences.getString("Name","checkboxid");

save value of spinner selected item

How can I save the current selected spinner value, such that when I reopen the application the saved value is automatically selected by default?
Please Write below code on onItemSelectedListener() of spinner and store selected value into shared preferences.
String mSpnValue=mSpinner1.getSelectedItem().toString();
SharedPreferences myPrefs = this.getSharedPreferences("myPrefs", MODE_WORLD_READABLE);
SharedPreferences.Editor prefsEditor = myPrefs.edit();
prefsEditor.putString("Value", mSpnValue);
Use below code for set item as selected in spinner.
SharedPreferences myPrefs = this.getSharedPreferences("myPrefs", MODE_WORLD_READABLE);
String myString = myPrefs.getString("Value","nothing"); // the value you want the
ArrayAdapter<String> myAdap = (ArrayAdapter<String>) mSpinner1.getAdapter();
int spinnerPosition = myAdap.getPosition(myString);
// set the default according to value
mSpinner1.setSelection(spinnerPosition);
You can save spinner position in preferences and when entering back use spinner.setSelection(position_from_preferences);
You can use several methods
For example you can use a DataBase and save on it.
Other methods, and the best IMO, is used SharedPreferences
http://developer.android.com/intl/es/reference/android/content/SharedPreferences.html
http://developer.android.com/intl/es/reference/android/app/backup/SharedPreferencesBackupHelper.html
For set value one another way:---
for(int i=0;i<adapter.getCount();i++){
if(adapter.getItem(i).equals(your save preference value){
spinner_timer.setSelection(i);
}
}
you can also refer to the spinner value by position. that way you need only to deal with ints straight-fowardly:
SharedPreferences settings = getSharedPreferences("MYPREFS", 0);
SharedPreferences.Editor editor = settings.edit();
editor.putInt("VALUE", spinner.getSelectedItemPosition());
editor.commit();
and to load:
spinner.setSelection(settings.getInt("VALUE", 0));

How can I get SharedPreferences of a PreferenceActivity from another class in android?

I'm new at android. I have a little idea over sharedPreference. Some tutorials say to add preferences in a xml file, but I need to add preferences dynamically. So I done that from a java class(my settings page).
PreferenceScreen root = getPreferenceManager().createPreferenceScreen(this);
CheckBoxPreference checkboxPref = new CheckBoxPreference(this);
checkboxPref.setKey("1");
checkboxPref.setTitle("SomeRandomStuff");
root.addPreference(checkboxPref);
Now, Now I need to get title of all selected checkbox (true) from that settings page to show which option been selected.
How can I do that?
thank you.
you can use a regular checkbox and sharedPreferences. Just add it's state like this
// global variables
SharedPreferences data;
public static String filename = "prefs";
// setup the SharedPreferences in onCreate()
data = getSharedPreferences(filename, 0);
// set the SharedPreference based on checkbox state
#Override
public void onCheckedChanged(CompoundButton arg0, boolean arg1) {
switch (arg0) {
case R.id.checkBox1:
boolean checked = checkBox1.isChecked();
SharedPreferences.Editor e = dataAddHS.edit();
e.putBoolean("preferenceName", checked);
e.commit();
break;
}
then when you need to pull the preference state, just do
boolean checked = data.getBoolean("preferenceName", false);
then you can use an if statement to see if checked is true or false, etc.
From what you're saying, it sounds like all you need is a default value for the preferences you will be working with. To be specific, you may have a bunch of checkbox-preferences that you want to use. When you read them, you can use the getBoolean method to get their values.
Note that the getBoolean method takes a second argument, which is the default value to return.
This means that you don't have to set the preferences dynamically. You use getBoolean to read the preferences and if the preferences have not been set by the user, a default value that you specify will be returned.
SharedPreferences sp = PreferenceManager.getDefaultSharedPreferences(this);
sp.getBoolean("whether_user_wants_setting1", false);

Categories

Resources