References a separate Android preference screen from within another Preference Screen in XML
Asked Answered
S

2

9

I have two Android Preference Screens defined in my Android app in XML.

For example, Screen 1

<?xml version="1.0" encoding="utf-8"?>
<PreferenceScreen 
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:key="screen1">
    <PreferenceCategory android:title="Preferences">
        <CheckBoxPreference 
            android:defaultValue="true"
            android:title="test"
            android:key="test_pref"/>
    </PreferenceCategory>
</PreferenceScreen>

and Screen 2

<?xml version="1.0" encoding="utf-8"?>
<PreferenceScreen
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:key="screen2">
    <CheckBoxPreference 
        android:key="checkbox" 
        android:title="Checkbox">
    </CheckBoxPreference>
</PreferenceScreen>

I would like screen 2 to be a separate screen to be accessible in its own right but I would also like its preferences to be a part of screen one also. Is there a simple way I can simply reference screen 2 from within screen 1? Or do I just need to essentially repeat the same preference stuff in a sub preference screen in Screen 1.

Soak answered 12/1, 2011 at 16:44 Comment(2)
Did you try using the <include/> tag? I'm not sure if it works for PreferenceScreen, but that is the way to include ordinary layouts within other layouts. See developer.android.com/resources/articles/…Bioscope
Thanks for the suggestion Mayra. Unfortunately it seems to be limited to reusing layout widgets. But I never knew about it so I still learned something! CheersSoak
S
13

I didn't find a way to "merge" both files directly in XML, but you could try to merge them using Java:

@Override
public void onCreate(final Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    getPreferenceManager().setSharedPreferencesName(Settings.PREFERENCES_NAME);
    getPreferenceManager().setSharedPreferencesMode(Context.MODE_WORLD_READABLE);

    // add the first xml
    addPreferencesFromResource(R.xml.preferences_settings);
    // add another xml
    addPreferencesFromResource(R.xml.preferences_mail_settings);

    // do the things, that need to be done...
}

Good luck

Tom

Satyriasis answered 12/1, 2011 at 16:44 Comment(2)
Doing it in code is absolutely fine. That great. Thanks for your help TomSoak
It merge the UI but, any way to merge two PreferenceFragmentCompat? Each PreferenceFragmentCompat has its own UI and action and i want that two fragment to be merge. Any help would appreciate.Anadem
P
7

You can do this in XML with an Intent:

<PreferenceScreen android:key="screen1">
  <PreferenceScreen android:key="screen2">
    <intent android:action="com.example.PREFERENCE_2" />
  </PreferenceScreen>
</PreferenceScreen>

AndroidManifest.xml:

<activity android:name="com.example.Preference2Activity">
  <intent-filter>
    <category android:name="android.intent.category.DEFAULT" />
    <action android:name="com.example.PREFERENCE_2" />
  </intent-filter>
</activity>
Pictorial answered 29/3, 2011 at 20:23 Comment(1)
I wanted to upvote your answer, but your rep is 1,024 and I couldn't bring myself to change a nice round number.Offshoot

© 2022 - 2024 — McMap. All rights reserved.