Android: visibility attribute in xml settings 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 android: visibility = "invisible" attribute for the last checkbox does not work; Is this attribute (or gone for that matter) not working for preference?

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

+7
source share
3 answers

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

You can, however, add and remove preferences from PreferenceScreen programmatically.

+11
source

To change the visibility, you must use the setVisible method.

Initialize the checkbox preference first.

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

then

 // Show the check box preference showSigPreference.setVisible(true); // Hide the check box preference showSigPreference.setVisible(false); 
+2
source

I understand that this is an old question that previously had no acceptable answer to do this in XML.

Now with the addition of the AppCompat IS library, you can do this directly in xml. See the full example at fooobar.com/questions/52677 / ...

0
source

Source: https://habr.com/ru/post/922033/


All Articles