Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 551
Tim Avatar asked Jan 12 '11 16:01

Tim


People also ask

What is preference screen?

This settings screen is used to manage the preferences of the users. For creating this settings screen android provides a feature to make a settings preferences screen. In this article, we will take a look at implementing the preferences setting screen in Android.

What is PreferenceFragmentCompat?

A PreferenceFragmentCompat is the entry point to using the Preference library. This Fragment displays a hierarchy of Preference objects to the user. It also handles persisting values to the device. To retrieve an instance of android.

Where is the preference option in Android Studio?

From the menu bar, click File > Settings (on macOS, click Android Studio > Preferences).

What is preference framework in Android?

What is the Preferences Framework? Android system provides many ways to save data for your application. One of them is Preferences Framework , through Android preference framework we can easily develop screen that allows user to modify preferences.


2 Answers

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

like image 185
TomTasche Avatar answered Oct 24 '22 17:10

TomTasche


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>
like image 36
Patrick Avatar answered Oct 24 '22 16:10

Patrick