Links to a separate Android preferences screen from another XML settings screen

I have two Android preference screens defined in an Android app in XML format.

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 that would be accessible on its own, but I would also like its preferences to be part of the screen. Is there an easy way I can just refer to screen 2 from screen 1? Or I just need to essentially repeat the same preference stuff on the preferences screen on screen 1.

+8
android reference xml preferencescreen
source share
2 answers

I did not find a way to "combine" both files directly in XML, but you could try to combine 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

+12
source share

You can do this in XML with the intention of:

 <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> 
+6
source share

Source: https://habr.com/ru/post/650984/


All Articles