Jika Anda menggunakan PreferenceFragmentCompat, Anda dapat menyetel visiblity dalam xml.
Preferensi di xml Anda akan dikonversi ke versi AppCompat secara otomatis. Anda kemudian dapat menggunakan atribut ' app: isPreferenceVisible ' di xml Anda
preferensi.xml
<PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools">
<CheckBoxPreference
android:defaultValue="false"
android:key="show.navigation"
android:title="Show navigation"
app:isPreferenceVisible="false" />
...
Atribut tersebut didokumentasikan di https://developer.android.com/guide/topics/ui/settings/components-and-attributes
Penambahan PreferenceFragmentCompat
didokumentasikan di https://developer.android.com/guide/topics/ui/settings/#inflate_the_hierarchy
Contoh:
public class MySettingsActivity extends AppCompatActivity {
public static class MySettingsFragment extends PreferenceFragmentCompat {
@Override
public void onCreatePreferences(Bundle savedInstanceState, String rootKey) {
setPreferencesFromResource(R.xml.preferences, rootKey);
}
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
getSupportFragmentManager()
.beginTransaction()
.replace(R.id.settings_container, new MySettingsFragment())
.commit();
}
}