Tim
Tim

Reputation: 5767

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

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.

Upvotes: 9

Views: 5232

Answers (2)

Patrick
Patrick

Reputation: 3759

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>

Upvotes: 7

TomTasche
TomTasche

Reputation: 5526

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

Upvotes: 13

Related Questions