android - Dynamically load a preference depending on SDK version -
i have preferenceactivity in android app use show settings screen. have "multiselectlistpreference" in it, ran problem doesn't work on pre-api11 versions of android, because got introduced in api11. no problem, there, worked around having 2 xml layouts, 1 "multiselectlistpreference" residing in res/xml-v11 , 1 "preference" residing in res/xml, handle custom dialog.
the xml file in res/xml-v11 looks this:
<?xml version="1.0" encoding="utf-8"?> <preferencescreen xmlns:android="http://schemas.android.com/apk/res/android"> <preference android:key="time" android:persistent="false" android:title="@string/time" /> <multiselectlistpreference android:key="days_multi" android:persistent="false" android:title="@string/days" /> <listpreference android:key="action" android:persistent="false" android:title="@string/action" /> </preferencescreen>
and 1 in res/xml looks this:
<?xml version="1.0" encoding="utf-8"?> <preferencescreen xmlns:android="http://schemas.android.com/apk/res/android"> <preference android:key="time" android:persistent="false" android:title="@string/time" /> <preference android:key="days_dialog" android:persistent="false" android:title="@string/days" /> <listpreference android:key="action" android:persistent="false" android:title="@string/action" /> </preferencescreen>
as can see, differense second element in list, rest same. there better way handle reuse same code in 1 file , reference remaining piece dynamically, depending on android version? , ideal solution this:
<?xml version="1.0" encoding="utf-8"?> <preferencescreen xmlns:android="http://schemas.android.com/apk/res/android"> <preference android:key="time" android:persistent="false" android:title="@string/time" /> <preference source="days.xml" /> <listpreference android:key="action" android:persistent="false" android:title="@string/action" /> </preferencescreen>
while have files called days.xml under res/xml-v11 , res/xml containing piece different.
i couldn't find related in documentation , after searching here on stackoverflow. android offer way this? or perhaps there other way factor out common code?
what comes mind use <include/>
tag. seems cannot use in preference xmls, can create preferences in parts. need create prefs in preferencesactivity
(where r.xml.prefs_days
version specific file 2 versions):
addpreferencesfromresource(r.xml.prefs_time); addpreferencesfromresource(r.xml.prefs_days); addpreferencesfromresource(r.xml.prefs_action);
Comments
Post a Comment