preferenceactivity
How do you refresh PreferenceActivity to show changes in the settings?
Instead of finish(); startActivity(getIntent()); I prefer the following code : setPreferenceScreen(null); addPreferencesFromResource(R.xml.preferences); This will reset the display as well but without the whole finish procedure and its consequences. Plus this code works well with PreferenceActivity and PreferenceFragment. This is interesting if you would like to change dynamically the locale value for example. In this case, … Read more
Issues with dual-pane preference screens
Problem Solved With how popular this question has become, I decided to revisit this issue again and see if I could find a resolution…and I did. Found a nice little work around that solves the single pane showing instead of dual pane and ensuring a header is always pre-selected when in dual pane mode. If … Read more
How to show and hide preferences on Android dynamically?
From a PreferenceActivity call Preference somePreference = findPreference(SOME_PREFERENCE_KEY); PreferenceScreen preferenceScreen = getPreferenceScreen(); preferenceScreen.removePreference(somePreference); you can later call: preferenceScreen.addPreference(somePreference); The only a little bit tricky part is getting the order correct when adding back in. Look at PreferenceScreen documentation, particularly it’s base class, PreferenceGroup for details. Note: The above will only work for immediate children of … Read more
PreferenceActivity Android 4.0 and earlier
PreferenceFragment will not work on 2.2 and 2.3 (only API level 11 and above). If you want to offer the best user experience and still support older Android versions, the best practice here seems to be to implement two PreferenceActivity classes and to decide at runtime which one to invoke. However, this method still includes … Read more
Launch new activity from PreferenceActivity
Given you are using xml preferences you can add code right into the xml: <Preference android:title=”Some Title” android:summary=”Some Description”> <intent android:action=”android.intent.action.VIEW” android:targetPackage=”com.package.name” android:targetClass=”com.package.name.ActivityName” /> </Preference>
Number Preferences in Preference Activity in Android
Use an EditTextPreference and set the input type to TYPE_CLASS_NUMBER. This will force the user to enter numbers and not letters. EditTextPreference pref = (EditTextPreference)findPreference(“preference_name”); pref.getEditText().setInputType(InputType.TYPE_CLASS_NUMBER);
When to use CheckBox and when Switch
I actually find this to be quite an interesting question, particularly considering that one can easily emulate the functionality of a switch using a checkbox in Android. According to the Android developers guide, a checkbox is simply a type of switch. Check the quotes below or read the full description here. Checkboxes: Checkboxes allow the … Read more
PreferenceActivity: save value as integer
You could extend EditTextPreference: public class IntEditTextPreference extends EditTextPreference { public IntEditTextPreference(Context context) { super(context); } public IntEditTextPreference(Context context, AttributeSet attrs) { super(context, attrs); } public IntEditTextPreference(Context context, AttributeSet attrs, int defStyle) { super(context, attrs, defStyle); } @Override protected String getPersistedString(String defaultReturnValue) { return String.valueOf(getPersistedInt(-1)); } @Override protected boolean persistString(String value) { return persistInt(Integer.valueOf(value)); } … Read more
Android: “BadTokenException: Unable to add window; is your activity running?” at showing dialog in PreferenceActivity
I had a very similar issue (which landed me here) and found a very simple fix for it. While my code is different, it should be easy to adapt. Here is my fix: public void showBox() { mActive = true; if (! ((Activity) mContext).isFinishing()) { mDialogBox.show(); } } So, in the sample code in the … Read more