Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to show and hide preferences on Android dynamically?

Is there a way to dynamically show and hide preferences? In my case, I have a checkbox preference that would disable or enable one of 2 preference groups ("with-" and "without-handicap" groups). While this would be the ideal GUI in a desktop environment, the "with-handicap" takes up nearly the whole screen, while the other, "without-handicap" takes up only a small portion of the screen.

Rather than showing both groups at the same time, I'd like to show only one of them at a time, and dynamically show or hide the 2 groups when the checkbox changes. Is there a way to do this?

like image 644
Japtar Avatar asked Jan 16 '11 02:01

Japtar


People also ask

How do I set custom preferences on Android?

It's still possible to customise the appearance of a Preference item though. In your XML you have to declare the root element as android:id="@android:id/widget_frame , and then declare TextView as android:title and android:summary . You can then declare other elements you want to appear in the layout.

How do I get to preferences in Android Studio?

From the menu bar, click File > Settings (on macOS, click Android Studio > Preferences).

What is preference framework in Android?

What is the Preferences Framework? Android system provides many ways to save data for your application. One of them is Preferences Framework , through Android preference framework we can easily develop screen that allows user to modify preferences.


2 Answers

Not exactly hiding/showing but if you only want disabling/enabling preference depending on another preference you can specify android:dependency="preferenceKey" or Preference.setDependency(String)

Example from developer.android.com:

<?xml version="1.0" encoding="utf-8"?> <PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android">     <CheckBoxPreference         android:key="pref_sync"         android:title="@string/pref_sync"         android:summary="@string/pref_sync_summ"         android:defaultValue="true" />     <ListPreference         android:dependency="pref_sync"         android:key="pref_syncConnectionType"         android:title="@string/pref_syncConnectionType"         android:dialogTitle="@string/pref_syncConnectionType"         android:entries="@array/pref_syncConnectionTypes_entries"         android:entryValues="@array/pref_syncConnectionTypes_values"         android:defaultValue="@string/pref_syncConnectionTypes_default" /> </PreferenceScreen> 
like image 20
hidro Avatar answered Sep 29 '22 03:09

hidro


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 a PreferenceScreen. If there is a PreferenceCategory in between, you need to remove the preference from its parent PreferenceCategory, not the PreferenceScreen. First to ensure the PreferenceCategory has an android:key attribute set in the XML file. Then:

Preference somePreference = findPreference(SOME_PREFERENCE_KEY); PreferenceCategory preferenceCategory = (PreferenceCategory) findPreference(SOME_PREFERENCE_CATEGORY_KEY); preferenceCategory.removePreference(somePreference); 

and:

preferenceCategory.addPreference(somePreference); 
like image 87
dhaag23 Avatar answered Sep 29 '22 02:09

dhaag23