How to detect if changes were made in the preferences?

Do SharedPreferences.OnSharedPreferenceChangeListener spChanged = new SharedPreferences.OnSharedPreferenceChangeListener() { @Override public void onSharedPreferenceChanged(SharedPreferences sharedPreferences, String key) { // your stuff here } }; In your PreferenceActivity, ie make it a member of your PreferenceActivity class and then do registerOnSharedPreferenceChangeListener(spChanged) in the PreferenceActivity.onCreate() method. That’s what I do and I never have a problem. Else it’s your conditional … Read more

PreferenceFragmentCompat has padding on PreferenceCategory that I can’t get rid of

Actually there is a better hack to fix this, also with resource overriding: Create res/values-sw360dp-v13/values-preference.xml: <?xml version=”1.0″ encoding=”utf-8″?> <resources xmlns:tools=”http://schemas.android.com/tools”> <bool name=”config_materialPreferenceIconSpaceReserved” tools:ignore=”MissingDefaultResource,PrivateResource”>false</bool> <dimen name=”preference_category_padding_start” tools:ignore=”MissingDefaultResource,PrivateResource”>0dp</dimen> </resources> The <bool> fixes the default value of iconSpacePreserved for all Preference; The <dimen> fixes the PreferenceCategory. EDIT: If you are using androidx.preference:preference:1.1.0-alpha01 or above, you won’t need the … Read more

Dynamic ListPreference in android

Every XML element in Android can be created programmatically as the element name is also a Java class. Hence you can create a ListPreference in code: CharSequence[] entries = { “One”, “Two”, “Three” }; CharSequence[] entryValues = { “1”, “2”, “3” }; ListPreference lp = new ListPreference(this); lp.setEntries(entries); lp.setEntryValues(entryValues); You could alternatively create it in … Read more

Android – How Do I Set A Preference In Code

I assume by preferences you are referring to your application’s preferences and not Android phone settings. To store preferences between runs of you application you need to do the following Create a SharedPreferences object SharedPreferences settings = getSharedPreferences(String n, MODE_PRIVATE); String n identifies your preferences and the second argument is the mode they’ll be accessed … Read more

How to manage dividers in a PreferenceFragment?

AndroidX makes it simple, but I wish it was better documented. In XML To add/remove dividers between preferences in XML, use the following attributes: <androidx.preference.PreferenceScreen xmlns:app=”http://schemas.android.com/apk/res-auto”> <Preference … app:allowDividerAbove=”true/false” app:allowDividerBelow=”true/false” … /> </androidx.preference.PreferenceScreen> Note, a divider will only be shown between two preferences if the top divider has allowDividerBelow set to true and the bottom … Read more

How to use Percentage for android layout?

UPDATE 2018: PercentRelativeLayout and PercentFrameLayout are deprecated. Consider using ConstraintLayout Old Answer: So far Android Support library has limited on where to use this: with RelativeLayout android.support.percent.PercentRelativeLayout with FrameLayout android.support.percent.PercentFrameLayout How to use it? Well, first make sure to include the dependency at build.grade(Module: app) in your android app. dependencies { compile ‘com.android.support:percent:23.3.0’ } Then … Read more

How to add a button to a PreferenceScreen?

For the xml: <Preference android:title=”Acts like a button” android:key=”@string/myCoolButton” android:summary=”This is a cool button” /> Then for the java in your onCreate() Preference button = findPreference(getString(R.string.myCoolButton)); button.setOnPreferenceClickListener(new Preference.OnPreferenceClickListener() { @Override public boolean onPreferenceClick(Preference preference) { //code for what you want it to do return true; } }); This will appear like a normal Preference, with … Read more

How do you create Preference Activity and Preference Fragment on Android?

I found this post (What to use instead of “addPreferencesFromResource” in a PreferenceActivity?) that help me understand that you have to go through a PreferenceFragment in order to do it. In the following explanation I use your.package. just to show that you have to put the package name. Everybody has its own package so please … Read more