getPreferenceScreen() - android

I need check a value and enable or disable programatically "checkboxpreference". I using this code but getPreferenceScreen() wants a method and I don't know which method use. (I use this on android 2.1).
<CheckBoxPreference
android:enabled="true"
android:title="Now"
android:defaultValue="false"
android:key="keep" />
protected void check(){
// read values
if (values){
getPreferenceScreen().findPreference("checkbox-preference-key").setEnabled(true);
}
else {
getPreferenceScreen().findPreference("checkbox-preference-key").setEnabled(false);

Use getPreferenceManager() instead.
So your code now looks like:
getPreferenceManager().findPreference("keep").setEnabled(values);
This is the equivalent of
if (values){
getPreferenceManager().findPreference("keep").setEnabled(true);
}
else {
getPreferenceManager().findPreference("keep").setEnabled(false);
}

Related

Making PreferenceScreen not write to default SharedPreferences

I have a PreferenceScreen:
<?xml version="1.0" encoding="utf-8"?>
<PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android">
<CheckBoxPreference
android:id="#+id/checkBoxHighlightWeekends"
android:key="checkBoxHighlightWeekends"
android:summary="Weekends are different"
android:title="Highlight weekends" />
<MultiSelectListPreference
android:id="#+id/multiSelectListSelectWeekends"
android:key="multiSelectListSelectWeekends"
android:dialogTitle="Select weekends"
android:entries="#array/weekends"
android:entryValues="#array/weekends_values"
android:summary="Saturday, Sunday"
android:title="Select weekends"/>
</PreferenceScreen>
I'm trying to use only the "view" part of this and not make it write to it's default SharedPreferences. When user changes the preference, I manually check the changed data and send it to a MVVM repo, when then writes to my own custom SharedPreference. I plan to use LiveData to update the settings screen when it's opened. All this hassle because I'm trying to learn the MVVM architecture and Android in general.
Inside MainPreferenceFragment:
#Override
public void onCreate(final Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
addPreferencesFromResource(R.xml.settings_main);
CheckBoxPreference checkBoxHighlightWeekends = getPreferenceManager().findPreference("checkBoxHighlightWeekends");
MultiSelectListPreference multiSelectListPreference = getPreferenceManager().findPreference("multiSelectListSelectWeekends");
checkBoxHighlightWeekends.setOnPreferenceChangeListener(changeListener);
multiSelectListPreference.setOnPreferenceChangeListener(changeListener);
}
Preference.OnPreferenceChangeListener changeListener = new Preference.OnPreferenceChangeListener() {
#Override
public boolean onPreferenceChange(Preference preference, Object o) {
// Call method in ViewModel which will save settings state to SharedPrefs
Logger.alert("Something changed! " + preference.getKey());
return true;
}
};
Any change to settings triggers the function, however the checkbox stops working - It doesn't change to the "tick" state anymore.
How can I strip out all the default click/change functionality from the PreferenceScreen and make it into a dumb view?

setOnPreferenceClickListener function doesn't work for Preference on Android

I'm using the code below to print text to logcat, but the setOnPreferenceClickListener function doesn't catch the event.
I'm using Android API Level 8 to test the code.
<PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android" >
<CheckBoxPreference
android:defaultValue="true"
android:key="settings_use_cache"
android:summary="Use cache"
android:title="Use Cache" />
<Preference
android:defaultValue="true"
android:key="settings_delete_cache"
android:summary="Delete all cache data"
android:title="Clear Cache" />
</PreferenceScreen>
Here is the code
public static class CachePreferenceFragment extends
PreferenceActivity {
#Override
public void onCreate(Bundle savedInstanceState) {
Log.w("DBG", "Oncreate started");
super.onCreate(savedInstanceState);
addPreferencesFromResource(R.xml.pref_cache);
Preference settings_delete_cache=findPreference("settings_delete_cache");
settings_delete_cache.setOnPreferenceClickListener(new OnPreferenceClickListener() {
#Override
public boolean onPreferenceClick(Preference preference) {
Log.w("Prefence", "Deleting Cache");
return false;
}
});
}
}
What can I do to ensure the listener catches the event?
EDIT:
After talking with this dev outside of the thread, it was identified that the problem was that he was putting his listener inside of the fragment. So this code actually works, but it will only run if you are using a two-panel layout. In fact this is stated right above the method:
/**
* This fragment shows notification preferences only. It is used when the
* activity is showing a two-pane settings UI.
*/
public static class CachePreferenceFragment extends
PreferenceActivity {
The solution is to also set the onPreferenceClick listener in setupSimplePreferencesScreen()!

Preference Activity on Preference Click Listener

I am building a Preference Activity where most of the preferences in the list will be executing code and not modifying a SharedPreference directly. My preferences.xml file looks like this.
<PreferenceCategory
android:title="Connection" >
<Preference
android:id="#+id/settings_connectToNewComputer"
android:key="connectToNewComputer"
android:summary="Currently connected to:"
android:title="Connect to new computer" />
<Preference
android:id="#+id/removeDevice"
android:key="removeDevice"
android:summary="Remove this device from the computer's whitelist"
android:title="Remove this device from computer" />
</PreferenceCategory>
<PreferenceCategory
android:title="About" >
<Preference
android:id="#+id/settings_About"
android:key="about"
android:summary="About me and my thanks to those who made this app great"
android:title="About Hue Pro" />
<Preference
android:id="#+id/contact"
android:key="contact"
android:summary="Contact me with comments, bugs, and suggestions for updates"
android:title="Contact me" />
</PreferenceCategory>
My goal is to have a block of code executed when a one of these preferences are clicked. Similar to the "Clear search history" in the Google Play settings preference menu. (http://i.imgur.com/qnHbJX9.png)
Does anyone know how to make this possible?
I have to add that I have tried using findPreference("KeyNameHere") but it always returns null.
Thank you!
Edit:
I added in this code and implemented OnPreferenceClickListener:
#Override
public boolean onPreferenceClick(Preference preference) {
return false;
}
But this method never gets called. Is there another way to do this?
Edit 2:
I have found that if I take out the PreferenceCategory tags so I am left with this:
<Preference
android:id="#+id/settings_connectToNewComputer"
android:key="connectToNewComputer"
android:summary="Currently connected to:"
android:title="Connect to new computer" />
<Preference
android:id="#+id/removeDevice"
android:key="removeDevice"
android:summary="Remove this device from the computer's whitelist"
android:title="Remove this device from computer" />
<Preference
android:id="#+id/settings_About"
android:key="about"
android:summary="About me and my thanks to those who made this app great"
android:title="About Hue Pro" />
<Preference
android:id="#+id/contact"
android:key="contact"
android:summary="Contact me with comments, bugs, and suggestions for updates"
android:title="Contact me" />
and call this:
getPreferenceScreen().setOnPreferenceClickListener(new Preference.OnPreferenceClickListener() {
#Override
public boolean onPreferenceClick(Preference preference) {
return false;
}
});
then I actually get a response from the click event. The only down side is I have to remove the preference grouping. Anyone know why this is and any way to fix it?
Implement OnPreferenceClickListener and in the onPreferenceClick
#Override
public boolean onPreferenceClick (Preference preference)
{
String key = preference.getKey();
// do what ever you want with this key
}
Maybe this could not be useful for OP, but could be useful for someone else.
I'd like to write a sort of summary; in general, you can follow mainly three ways:
1) you can find your preference somewhere in your code with
Preference examplePreference = findPreference(KEY_EXAMPLE_PREFERENCE);
and then you can add a click listener and override its on click method with
examplePreference.setOnPreferenceClickListener(new Preference.OnPreferenceClickListener() {
#Override
public boolean onPreferenceClick(Preference preference) {
// handle click here
}
});
This has to be done for every preference whose clicks you want to listen to
2) You can implement Preference.OnPreferenceClickListener interface in your settings fragment/activity and override onPreferenceClick just once, by using a switch construct or a if-else if-else if-... construct and merging all the single handlings; it should be something like:
#Override
public boolean onPreferenceClick(Preference preference) {
switch (preference.getKey()) {
case KEY_EXAMPLE_PREFERENCE: {
// handle click here
}
break;
case ...
}
}
Then, you still have to find each preference but you can simply call on each of them
setOnPreferenceClickListener(this);
(I think the OP's implementation didn't work (his method wasn't called) because of this last part)
we pass "this" as parameter because we implemented the click listener interface
3) (which I think is the easiest) you can override
onPreferenceTreeClick(PreferenceScreen preferenceScreen, Preference preference)
in your preference fragment/activity without implementing any other interface and there you can copy the switch of the if-else if-... construct of option 2); the main advantage in that you shouldn't need to find each preference and to call on them setOnPreferenceClickListener.
Hope this will be useful for someone!
Just override:
#Override
public boolean onPreferenceTreeClick(PreferenceScreen preferenceScreen, Preference preference) {
String key = preference.getKey();
...
return super.onPreferenceTreeClick(preferenceScreen, preference);
}
You could also find the preference and set the click listener.
Preference connectToNewComputer= findPreference("connectToNewComputer");
connectToNewComputer.setOnPreferenceClickListener(this);
For Androidx in Feb 2020
Others answers were not worked in Androidx for me. I implemented Settings from Android Developers guides
See below guide for implementing click listener
1) Implement PreferenceManager.OnPreferenceTreeClickListener in your settings fragment, like below code
import androidx.preference.PreferenceManager;
class SettingsFragment extends PreferenceFragmentCompat implements PreferenceManager.OnPreferenceTreeClickListener {
2) Override onPreferenceTreeClick inside your SettingsFragment
#Override
public boolean onPreferenceTreeClick(Preference preference) {
String key = preference.getKey();
switch (key) {
case "key1":
return true;
case "key2":
return true;
//codes
}
}
I came up with my own (what I believe is really messed up) solution; but it works.
for(int x = 0; x < getPreferenceScreen().getPreferenceCount(); x++){
PreferenceCategory lol = (PreferenceCategory) getPreferenceScreen().getPreference(x);
for(int y = 0; y < lol.getPreferenceCount(); y++){
Preference pref = lol.getPreference(y);
pref.setOnPreferenceClickListener(new Preference.OnPreferenceClickListener(){
#Override
public boolean onPreferenceClick(Preference preference) {
return false;
}
});
}
}
So what I have learned is there is a hierarchical system that works like: PreferenceScreen has children PreferenceCategory has children Preference, as you can see in the XML file. My problem was I could not set the preferences' onClickListeners directly from the PreferenceScreen. So I made two for loops that will get down to each Preference and set an OnPreferenceClickListener for each and every one of them. Messy, but works finally.
Your Preference object wont get null if you will find followings
(copypasting from the project):
public class ImePreferences extends PreferenceActivity {
.....
#Override
protected boolean isValidFragment(String fragmentName) {
return Settings.class.getName().equals(fragmentName);
}
.....
public static class Settings extends InputMethodSettingsFragment {
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setInputMethodSettingsCategoryTitle(R.string.language_selection_title);
setSubtypeEnablerTitle(R.string.select_language);
// Load the preferences from an XML resource
addPreferencesFromResource(R.xml.ime_preferences);
Preference pLcl = getPreferenceScreen().findPreference(getResources().getString(
R.string.dictionary_button));
pLcl.setOnPreferenceClickListener(new Preference.OnPreferenceClickListener() {
#Override
public boolean onPreferenceClick(Preference preference) {
// handle click here
l.a("this is the click");
return true;
}
});
if(pLcl != null)
l.a(6576);
}
}
.....
}
For Kotlin
your xml should look like this
<Preference
app:title="Contact me"
app:key="contact"/>
do not forget the key.
Then in your Settings Activity find this class
class SettingsFragment : PreferenceFragmentCompat()
and add this code
override fun onPreferenceTreeClick(preference: Preference): Boolean {
val key = preference.key
return super.onPreferenceTreeClick(preference)
}
When you are done your code should look like this
class SettingsFragment : PreferenceFragmentCompat() {
override fun onPreferenceTreeClick(preference: Preference): Boolean {
val key = preference.key
return super.onPreferenceTreeClick(preference)
}
override fun onCreatePreferences(savedInstanceState: Bundle?, rootKey: String?) {
setPreferencesFromResource(R.xml.root_preferences, rootKey)
}
}

Why does Android ignore OnPreferenceClickListener() that returns true?

I defined my preferences in xml file like the following:
<PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android">
<PreferenceScreen
android:title="#string/pref_screen"
android:key="pref_screen_key">
<CheckBoxPreference
android:defaultValue="true"
android:key="prefs1_key"
android:title="#string/prefs1_str"
android:summary="#string/prefs1_summary" />
....
Inside PreferenceActivity I override onPreferenceClick() for "pref_screen_key":
...
findPreference("pref_screen_key").setOnPreferenceClickListener(
new OnPreferenceClickListener() {
#Override
public boolean onPreferenceClick(Preference preference) {
if (flag) {
// do some stuff
return true; // According to Android doc it means, that click was handled
} else
return false;
}
So I wish that Android will not open view with CheckBoxPreference if my flag is true, but just executes my stuff (I return true inside onPreferenceClick() to indicate that the click was handled and it is not needed to execute the default onPreferenceClick() behavior).
Unfortunately, Android always handles onPreferenceClick() with default behavior and makes my stuff at the same time.
Is it a bug in Android?
Is there a way to prevent Android from showing CheckBoxPreference after user clicks on PreferenceScreen?
You're probably running into problems with the fact that the flag must be declared final to be accessed inside the OnPreferenceClickListener. Try encapsulating it in a class and declaring the class final.
To more directly answer your question, remember that when you return true from onPreferenceClick(), you're telling the system "don't worry, I handled it." So, when you simply return true without actually doing anything, the system isn't ignoring you, per se, you just told it to do nothing when the Preference is clicked.
Your code looks similar to lines 108-115 of this example. Look through that and it may help.
The bug is in https://github.com/android/platform_frameworks_base/blob/android-5.1.1_r13/core/java/android/preference/Preference.java#L985
In Preference.performClick, it checks if isEnabled() is true, calls onClick(), then your listener and returns if it returns false - if not, continue processing, and propagate the click to the right tree / screen / start the activity specified.
The bug is that it calls onClick before your onPreferenceClick, so checkboxes for example are always toggled, no matter what your onPreferenceClick returns.
Workaround: unfortunately, you'll need to subclass all preferences and do your own listener dispatching and only call super.onClick() if allowed.
I believe you want to use setOnPreferenceChangeListener instead of setOnPreferenceClickListener. As per the android docs:
This is called before the state of the Preference is about to be updated and before the state is persisted.
Return:
True to update the state of the Preference with the new value.
Example:
Preference pref = findPreference("pref_key");
pref.setOnPreferenceChangeListener(new OnPreferenceChangeListener() {
#Override
public boolean onPreferenceChange(Preference preference, Object newValue) {
if (flag) {
// do some stuff
return false; // Pref change will be not be persisted.
} else {
return true;
}
}
});

Android CheckBoxPreference java.lang.StackOverflowError

I have this CheckBoxPreference in my code. I have implemented onSharedPreferenceChanged() in my code to perform some action. The problem is that when i click on the checkbox preference, the function gets called in a loop with same value. Can anyone help me with this?
Here are the relevant code snippets:
onSharedPreferenceChanged() section in preference activity:
if(key.equals(LOCATION_UPDATE_KEY)) {
boolean update = sharedPreferences.getBoolean(LOCATION_UPDATE_KEY, false);
Log.v("preferences", update + "");
editor.putBoolean(LOCATION_UPDATE_KEY, update);
editor.commit();
}
preference activity's xml section:
<PreferenceCategory
android:title="Location">
<CheckBoxPreference
android:title="Track Location"
android:defaultValue="false"
android:summary="Keep track of handset location (Consumes Battery)"
android:key="track_option" />
<ListPreference
android:title="Location Update Source"
android:summary=""
android:key="track_source"
android:defaultValue="2"
android:entries="#array/location_sources"
android:entryValues="#array/location_sources_values"
android:dependency="track_option" />
<ListPreference
android:title="Location Update Interval"
android:summary=""
android:key="track_interval"
android:defaultValue="2"
android:entries="#array/location_update_interval"
android:entryValues="#array/location_update_interval_values"
android:dependency="track_option" />
</PreferenceCategory>
simple: if you change the SharedPreference in onSharedPreferenceChanged you create a loop because you trigger yourself. The loop is actually a recursion and if you call yourself endlessly you fill up the memory (not the normal one - the "stack") until you get a stackoverflow.
a normal (somewhat useful) recursion looks like this:
public int sumAllNumbersUpTo (int number) {
if (number > 0) {
return number + sumAllNumbersUpTo(number - 1);
} else {
return 0;
}
}
int result = sumAllNumbersUpTo(3);
// result is 3 + ( 2 + ( 1 + ( 0 ) ) )
it is calling itself until some condition is met. If you remove that condition then this method will never end.

Categories

Resources