android-preferences

Where to store Android preference keys?

好久不见. 提交于 2019-11-29 20:24:03
When I create preference activity I define all preferences in xml file. Every preference has a key defined in this xml. But when I access preference I write: SharedPreferences appPreferences = PreferenceManager.getDefaultSharedPreferences(this); boolean foo_value = appPreferences.getBoolean("foo_key_defined_in_xml", false); Is there any way to avoid referencing "foo_key_defined_in_xml" in hard-coded way? Maybe there is a possibility to reference it in R style way (not to refer to string)? pixel I've found that it's possible to store keys in strings.xml and refer to them from preferences.xml

Strange Error using onCreateView in PreferenceFragment when calling addPreferencesFromResource from onCreate

自作多情 提交于 2019-11-29 20:09:23
问题 I'm trying to add an ImageView to a preference fragment in order to show a preview of a color setting. I'm accessing the instance of the imageview via the onCreateView method toset the test color, and it will display. However it only works if I don't call addPreferencesFromResource in the onCreate method - which is a problem since the preferences must be added. Also if I leave the call to addPreferencesFromResource, but remove the entire onCreateView method the program will run (albiet

Initialize preferences from XML in the main Activity [duplicate]

痞子三分冷 提交于 2019-11-29 19:58:53
This question already has an answer here: Android Preferences: How to load the default values when the user hasn't used the preferences-screen? 5 answers My problem is that when I start application and user didn't open my PreferenceActivity so when I retrieve them don't get any default values defined in my preference.xml file. preference.xml file: <?xml version="1.0" encoding="utf-8"?> <PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android" android:key="applicationPreference" android:title="@string/config" > <ListPreference android:key="pref1" android:defaultValue="default

How to get all keys of SharedPreferences programmatically in Android?

本小妞迷上赌 提交于 2019-11-29 19:56:46
How to get all keys in SharedPreferences , not the value of the preference just key only? prefA = getSharedPreferences("MyAttack", MODE_PRIVATE); prefB= getSharedPreferences("MySkill", MODE_PRIVATE); Blackbelt SharedPreferences has the method getAll() that returns a Map<String, ?> . From the Map you can retrieve easily the keys with keySet() and the key/value mappings with entrySet() : Map<String, ?> allEntries = prefA.getAll(); for (Map.Entry<String, ?> entry : allEntries.entrySet()) { Log.d("map values", entry.getKey() + ": " + entry.getValue().toString()); } Bhanu Sharma What you can do is

android: How to change ListPreference title colour?

泄露秘密 提交于 2019-11-29 19:45:38
问题 I would like to change the title and line color of my ListPreference from blue to pink, to match the line of my action bar. Any ideas? Thanks in advance! I've been looking through Android's themes.xml and styles.xml looking at things like dialogPreferenceStyle , but haven't figured it out yet. 回答1: Had the same problem today. Old thread, but I found this: and it works perfectly. Simply modify your styles.xml <resources> <!-- Base application theme. --> <style name="AppTheme" parent="Theme

ClassCastException in PreferenceActivity

若如初见. 提交于 2019-11-29 13:55:38
I am trying to get an example from the Android 2 Application Development book by Reto Meier to work (page 202). As per the instructions I have created a userpreferences.xml as follows: <PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android" > <CheckBoxPreference android:key="PREF_AUTO_UPDATE" android:title="Auto refresh" android:summary="Select to turn on automatic updating" android:defaultValue="true" /> <ListPreference android:key="PREF_UPDATE_FREQ" android:title="Refresh frequency" android:summary="Frequency at which to refresh earthquake list" android:entries="@array

PreferenceFragment - Difference between getPreferenceManager() and getPreferenceScreen()?

﹥>﹥吖頭↗ 提交于 2019-11-29 10:36:42
问题 I've implemented my own PreferenceFragment subclass (detailed here), and want to listen for preference changes within it. PreferenceFragment provides you with two ways of doing this: getPreferenceManager().getSharedPreferences().registerOnSharedPreferenceChangeListener(this); and getPreferenceScreen().getSharedPreferences().registerOnSharedPreferenceChangeListener(this); Which one should be used? What's the difference? I don't really understand the distinction made in the Android docs. 回答1:

Extending Preference classes in Android Lollipop = losing animation

拈花ヽ惹草 提交于 2019-11-29 10:22:40
Just for extending CheckBoxPreference or SwitchPreference on Android Lollipop, the widget (the checkbox or the switch) won't have animation anymore. I'd like to extend SwitchPreference to force api < 21 to use SwitchCompat instead of the default one they are using (which is obviously wrong). I am using the new AppCompatPreferenceActivity with appcompat-v7:22.1.1 but that doesn't seem to affect the switches. The thing is that with just extending those classes, without adding any custom layout or widget resource layout, the animation is gone. I know I can write two instances of my preference.xml

Is it Possible to Use PreferenceActivity with SQLite instead of res/xml?

允我心安 提交于 2019-11-29 08:10:33
The beauty of PreferenceActivity is its tight integration with Android's res/xml . All you need to do achieve the magic of self-managed preference reading/saving, along with the UI, is define: public class MyPreferenceActivity extends PreferenceActivity { @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); addPreferencesFromResource(R.xml.settings); } } And define any <PreferenceScreen> structure you need in XML file(s). But this also seems to be its weakness: It is so tightly integrated, that I have no idea whether it is possible to use

How to remove Android preferences from the screen

这一生的挚爱 提交于 2019-11-29 06:38:54
问题 I'm trying to remove a preference from the screen so it isn't visible if the user is using an SDK greater than 2.2. I've found several answers saying that getPreferenceScreen().removePreference(thePreference) will work, but I'm getting FALSE returned every time I try it. Am I using it in the wrong place? Any clue from my code? public class Preferences extends PreferenceActivity implements OnSharedPreferenceChangeListener { private static final String POLLING_PREFERENCE = "update_frequency