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.

Tim
  • 5,767
  • 9
  • 45
  • 60
  • Did you try using the `` tag? I'm not sure if it works for PreferenceScreen, but that is the way to include ordinary layouts within other layouts. See http://developer.android.com/resources/articles/layout-tricks-reuse.html – Cheryl Simon Jan 12 '11 at 18:10
  • 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! Cheers – Tim Jan 13 '11 at 10:41

2 Answers2

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

TomTasche
  • 5,448
  • 7
  • 41
  • 67
  • Doing it in code is absolutely fine. That great. Thanks for your help Tom – Tim Jan 21 '11 at 15:01
  • 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. – Hiren Dabhi Apr 11 '19 at 13:12
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>
Patrick
  • 3,578
  • 5
  • 31
  • 53
  • 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. – Ben Pearson Mar 04 '16 at 16:58