Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

android:visibility attribute in preferences xml not working? (Android 2.3)

Take for example this small preference.xml file:

<?xml version="1.0" encoding="utf-8"?>
<PreferenceScreen android:title="@string/sig_title" xmlns:android="http://schemas.android.com/apk/res/android">
<ListPreference android:entries="@array/text_display_entries" android:title="@string/sig_style" android:key="text_style" android:entryValues="@array/text_display_values" />
<CheckBoxPreference android:title="@string/custom_font" android:key="tweaks_text" />
<CheckBoxPreference android:title="@string/col_random" android:key="random_color_pref" />
<CheckBoxPreference android:visibility="invisible" android:enabled="false" android:title="@string/sig_show" android:key="show_sig" />
</PreferenceScreen>

The attribute android:visibility="invisible" for the last checkbox doesn't work; this attribute (or gone for that matter) doesn't work for preferences?

I don't have anything in the code to mess with its visibility, just curious why this doesn't work.

like image 813
BlooregardQKazoo Avatar asked Aug 03 '12 10:08

BlooregardQKazoo


Video Answer


4 Answers

android:visibility is used to show and hide Views but it's not valid for a Preference. The documentation for Preference lists the available XML attributes, but none of them are what you want.

It is, however, possible to add and remove preferences from a PreferenceScreen programatically.

like image 133
jdamcd Avatar answered Oct 12 '22 05:10

jdamcd


For AndroidX users, add this directly to your preference XML

app:isPreferenceVisible="false"
like image 44
gcantoni Avatar answered Oct 12 '22 07:10

gcantoni


You have to use setVisible method to change the visibility.

First, initialize the checkbox preference.

CheckBoxPreference showSigPreference = (CheckBoxPreference) findPreference("show_sig");

then

// Show the check box preference
showSigPreference.setVisible(true);

// Hide the check box preference
showSigPreference.setVisible(false);
like image 43
Srikar Reddy Avatar answered Oct 12 '22 05:10

Srikar Reddy


I realize that this is an older question that didn't previously have an acceptable answer to do this in xml.

Now with the addition of the AppCompat Library is IS possible do this directly in xml. See the complete example at https://stackoverflow.com/a/54154665/114549

like image 38
aaronvargas Avatar answered Oct 12 '22 07:10

aaronvargas