i have kept background music for my android app by default.I have used preference for closing the music when the user wants.Now i want to check in an activity whether the checkbox is checked or not..How can i access the preference checkbox from any other activity..How can i implement it?
You can use this:
SharedPreferences preferences = getSharedPreferences("pref",
Context.MODE_PRIVATE);
boolean x = preferences.getBoolean("mycheckbox",false);
"mycheckbox" is the key that you have used when creating preference check box.
<CheckBoxPreference
android:key="mycheckbox"/>
Related
How do i update a Prefence UI instance that i created in a Setting Activity from another activity (Main Activity)?
I tried using these lines in Main Activity to update the Preference within Settings Activity, but I get ClassCastException.
Preference IsFeature =(Preference)((PreferenceActivity)context).findPreference((getString(R.string.key_enable_feature)));
IsFeature.setEnabled(True);
Just wondering whether theres another way to do this?
Any help, feedback or answers would be great!
You can try this:
In xml of settings get the "key" attribute from element you want to change (in bottom example it's "example_switch"). Than put this code in onClick method of button or wherever else you want to. This below takes the preference of switch in general settings and sets its value to false.
SharedPreferences sharedPref = PreferenceManager.getDefaultSharedPreferences(this);
SharedPreferences.Editor editor = sharedPref.edit();
editor.putBoolean("example_switch", false); // "example_switch" - "key" attribute of your element | false - value
editor.commit();
SharedPreferences sharedPreferences = PreferenceManager.getDefaultSharedPreferences(this);
boolean cbTest = sharedPreferences.getBoolean("checkbox_test", false);
this are my preference
<CheckBoxPreference
android:defaultValue="true"
android:key="checkbox_test"
android:summary="#string/checkbox_tes"
android:title="#string/title_heckbox_tes" />
and cbTest variable is by default always false, when I open page with preferences and then check cbTest again then is true. Why is by default always false? Why preference page needs to be initialized that start working?
How can I check default value before open preference page?
I already answer similar question but I cannot quickly located. You have to initialize your default share preferences. In your main activity put the code below in onCreate()
PreferenceManager.setDefaultValues(this, R.xml.yourfilename, false);
I have a CheckBoxPreference defined as follows:
<CheckBoxPreference
android:defaultValue="true"
android:key="prefVisible"
android:summary="#string/pref_visible_summary"
android:title="#string/pref_visible" >
</CheckBoxPreference>
My application uses this preference to control the visibility of a view. When I first start my application (on a new wiped emulator) the view is not shown. However, when the I go to the preferences screen (activity) the checkbox is shown as checked.
Does this mean that the defaultValue attribute is not actually setting the preference but rather it's just setting the value of the checkbox if there is no underlying data (as would be the case on a brand new install). And does this also mean that the preference is set only after the user enters/exits the preferences screen (activity) for the first time, otherwise it's undefined?
Note that in order to get my app to work the way I intended it to work I relied on the default value argument to the preferences getter method as follows:
SharedPreferences sharedPrefs = PreferenceManager.getDefaultSharedPreferences(this);
boolean isVisible = sharedPrefs.getBoolean("prefVisible", true); // default = true
This leaves me a bit confused as to why there are 2 ways to control the default value of a preference: defining it in the Xml or providing the default value in the getBoolean method.
No the preferences can be set if you call PreferenceManager.setDefaultValues. So if you call this when the first time the app is launched then your view will be shown.
You can read more at http://developer.android.com/guide/topics/ui/settings.html
My 1st activity is members personal information i have given chkbox for checking is local address is same as permanent address? if they r not same it should go to activity2 i.e permanent address screen. After filling all the details when i clicked on save button it should go to activity1 now i want that whatever i have filled in 1st activity should remain same also chkbox state and storing 2nd activity data in some variables in 1st activity class now i am storing activity1 and acitvity2 data in database.How to do it?
Use SharedPreferences for your requirement
To obtain shared preferences, use the following method In your activity:
SharedPreferences prefs = this.getSharedPreferences("store",
Context.MODE_PRIVATE);
To edit and save preferences
boolean checkbox_state = true;
prefs.edit().putBoolean("KEY", checkbox_state ).commit();
To read preferences:
boolean state= prefs.getBoolean("KEY", false);;
I'm trying to get and set a listPreference value from different activities and it's not working.
When I read and write it from my main activity, it only keeps whatever I write, so I'm assuming that I'm not targeting the listPreference correctly when I'm out of the activity because it's working inside my preference activity no problem.
I've seen some references on the developer website to CharSequence with getValue and getEntryValues but I haven't had luck getting them to work correctly either.
Here is my code for clicking a button and setting the listpreference value then it launches an intent to switch activities:
Main Activity, attempting to set the value of the listpreference to the first index value;
SharedPreferences settings = getSharedPreferences("PreferenceXML",
MODE_PRIVATE);
SharedPreferences.Editor editor = settings.edit();
editor.putString("ListPreferenceInXML", "1");
editor.commit();
String levelCheck = settings.getString("ListPreferenceInXML","1");
In my next activity I call read the value on launch to see which listPreference is active and it is always the number I write from the mains activity listed above. The problem is when I goto the actual Preference activity and it doesn't match or update when I change it on the ListPreference and launch the same activity from there (it still reads the value I set from the Main activity button)
code as follows for activity trying to read ListPreference:
SharedPreferences settings = getSharedPreferences("PreferenceXML",
MODE_PRIVATE);
Toast.makeText(this, settings.getString("ListPreferenceInXML","1"), 1000).show();
So I finally figured it out, the problem was with the way I was calling the preferences. Instead of calling the preferences like this, in both cases;
SharedPreferences settings = getSharedPreferences("PreferenceXML",
MODE_PRIVATE);
Call them like this:
SharedPreferences settings =
PreferenceManager.getDefaultSharedPreferences(getBaseContext());
I'm not sure if there is a step missing out of the first way of calling the preferences but this 2nd way worked like a champ.