android - How to show PreferenceScreen within PreferenceFragment -
i new android , appreciate help. first question =d sorry giving 2 links , no images, have not enought reputation made question more clear
acording android api guides settings, using preferencefragment because developing android 3.0 (api level 11) , higher.
i want achieve effect shown @ figure 4 , 5 android api guides settings link.
on tablet emulator looks ok, on handset emulator (and real device) can not achieve it. instead "sound", "display", "storage", "battery"... preferencecategory (like "device" seems) followed options should in new screen (activity). in 1 screen.
here files, in advance
settings.java (note: autogenerated , completed myself)
package com.example; import android.annotation.targetapi; import android.content.context; import android.content.res.configuration; import android.media.ringtone; import android.media.ringtonemanager; import android.net.uri; import android.os.build; import android.os.bundle; import android.preference.listpreference; import android.preference.preference; import android.preference.preferenceactivity; import android.preference.preferencecategory; import android.preference.preferencefragment; import android.preference.preferencemanager; import android.preference.ringtonepreference; import android.text.textutils; import java.util.list; import java.util.concurrent.futuretask; /** * {@link preferenceactivity} presents set of application settings. on * handset devices, settings presented single list. on tablets, * settings split category, category headers shown left of * list of settings. * <p> * see <a href="http://developer.android.com/design/patterns/settings.html"> * android design: settings</a> design guidelines , <a * href="http://developer.android.com/guide/topics/ui/settings.html">settings * api guide</a> more information on developing settings ui. */ public class settings extends preferenceactivity { // stackoverflow avoid error api 19 protected boolean isvalidfragment(string fragmentname) { if (profilepreferencefragment.class.getname().equals(fragmentname)) { return true; } else if (chatpreferencefragment.class.getname().equals(fragmentname)) { return true; } else if (notificationpreferencefragment.class.getname().equals( fragmentname)) { return true; } else if (contactpreferencefragment.class.getname().equals( fragmentname)) { return true; } else if (paymentpreferencefragment.class.getname().equals( fragmentname)) { return true; } else if (accessibilitypreferencefragment.class.getname().equals( fragmentname)) { return true; } else { return false; } }// stackoverflow avoid error api 19 /** * determines whether show simplified settings ui, * settings presented in single list. when false, settings shown * master/detail two-pane view on tablets. when true, single pane * shown on tablets. */ private static final boolean always_simple_prefs = false; @override protected void onpostcreate(bundle savedinstancestate) { super.onpostcreate(savedinstancestate); setupsimplepreferencesscreen(); } /** * shows simplified settings ui if device configuration * dictates simplified, single-pane ui should * shown. */ private void setupsimplepreferencesscreen() { if (!issimplepreferences(this)) { return; } // in simplified ui, fragments not used @ , instead // use older preferenceactivity apis. // add 'profile' preferences. addpreferencesfromresource(r.xml.pref_profile); // add 'chats' preferences, , corresponding header. preferencecategory fakeheader = new preferencecategory(this); fakeheader.settitle(r.string.pref_header_chat); getpreferencescreen().addpreference(fakeheader); addpreferencesfromresource(r.xml.pref_chat); // add 'notification' preferences, , corresponding header. fakeheader = new preferencecategory(this); fakeheader.settitle(r.string.pref_header_notification); getpreferencescreen().addpreference(fakeheader); addpreferencesfromresource(r.xml.pref_notification); // add 'contact' preferences, , corresponding header. fakeheader = new preferencecategory(this); fakeheader.settitle(r.string.pref_header_contact); getpreferencescreen().addpreference(fakeheader); addpreferencesfromresource(r.xml.pref_contact); // add 'payment' preferences, , corresponding header. fakeheader = new preferencecategory(this); fakeheader.settitle(r.string.pref_header_payment); getpreferencescreen().addpreference(fakeheader); addpreferencesfromresource(r.xml.pref_payment); // add 'accessibility' preferences, , corresponding header. fakeheader = new preferencecategory(this); fakeheader.settitle(r.string.pref_header_accessibility); getpreferencescreen().addpreference(fakeheader); addpreferencesfromresource(r.xml.pref_accesibility); // bind summaries of edittext/list/dialog/ringtone preferences // values. when values change, summaries updated // reflect new value, per android design guidelines. // bindpreferencesummarytovalue(findpreference("example_text")); // bindpreferencesummarytovalue(findpreference("example_list")); // bindpreferencesummarytovalue(findpreference("notifications_new_message_ringtone")); // bindpreferencesummarytovalue(findpreference("sync_frequency")); } /** {@inheritdoc} */ @override public boolean onismultipane() { return isxlargetablet(this) && !issimplepreferences(this); } /** * helper method determine if device has extra-large screen. * example, 10" tablets extra-large. */ private static boolean isxlargetablet(context context) { return (context.getresources().getconfiguration().screenlayout & configuration.screenlayout_size_mask) >= configuration.screenlayout_size_xlarge; } /** * determines whether simplified settings ui should shown. * true if forced via {@link #always_simple_prefs}, or device * doesn't have newer apis {@link preferencefragment}, or device * doesn't have extra-large screen. in these cases, single-pane * "simplified" settings ui should shown. */ private static boolean issimplepreferences(context context) { return always_simple_prefs || build.version.sdk_int < build.version_codes.honeycomb || !isxlargetablet(context); } /** {@inheritdoc} */ @override @targetapi(build.version_codes.honeycomb) public void onbuildheaders(list<header> target) { if (!issimplepreferences(this)) { loadheadersfromresource(r.xml.pref_headers, target); } } /** * preference value change listener updates preference's summary * reflect new value. */ private static preference.onpreferencechangelistener sbindpreferencesummarytovaluelistener = new preference.onpreferencechangelistener() { @override public boolean onpreferencechange(preference preference, object value) { string stringvalue = value.tostring(); if (preference instanceof listpreference) { // list preferences, correct display value in // preference's 'entries' list. listpreference listpreference = (listpreference) preference; int index = listpreference.findindexofvalue(stringvalue); // set summary reflect new value. preference .setsummary(index >= 0 ? listpreference.getentries()[index] : null); } else if (preference instanceof ringtonepreference) { // ringtone preferences, correct display value // using ringtonemanager. if (textutils.isempty(stringvalue)) { // empty values correspond 'silent' (no ringtone). } else { ringtone ringtone = ringtonemanager.getringtone( preference.getcontext(), uri.parse(stringvalue)); if (ringtone == null) { // clear summary if there lookup error. preference.setsummary(null); } else { // set summary reflect new ringtone display // name. string name = ringtone .gettitle(preference.getcontext()); preference.setsummary(name); } } } else { // other preferences, set summary value's // simple string representation. preference.setsummary(stringvalue); } return true; } }; /** * binds preference's summary value. more specifically, when * preference's value changed, summary (line of text below * preference title) updated reflect value. summary * updated upon calling method. exact display format * dependent on type of preference. * * @see #sbindpreferencesummarytovaluelistener */ private static void bindpreferencesummarytovalue(preference preference) { // set listener watch value changes. preference .setonpreferencechangelistener(sbindpreferencesummarytovaluelistener); // trigger listener preference's // current value. sbindpreferencesummarytovaluelistener.onpreferencechange( preference, preferencemanager.getdefaultsharedpreferences( preference.getcontext()).getstring(preference.getkey(), "")); } /** * fragment shows profile preferences only. used when * activity showing two-pane settings ui. */ @targetapi(build.version_codes.honeycomb) public static class profilepreferencefragment extends preferencefragment { @override public void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); addpreferencesfromresource(r.xml.pref_profile); // bind summaries of edittext/list/dialog/ringtone preferences // values. when values change, summaries // updated reflect new value, per android design // guidelines. // bindpreferencesummarytovalue(findpreference("")); // bindpreferencesummarytovalue(findpreference("")); } } /** * fragment shows chat preferences only. used when activity * showing two-pane settings ui. */ @targetapi(build.version_codes.honeycomb) public static class chatpreferencefragment extends preferencefragment { @override public void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); addpreferencesfromresource(r.xml.pref_chat); // bind summaries of edittext/list/dialog/ringtone preferences // values. when values change, summaries // updated reflect new value, per android design // guidelines. bindpreferencesummarytovalue(findpreference("pref_chat_enter_send")); bindpreferencesummarytovalue(findpreference("pref_chat_download_message")); bindpreferencesummarytovalue(findpreference("pref_chat_download_multimedia")); bindpreferencesummarytovalue(findpreference("pref_chat_font_size")); bindpreferencesummarytovalue(findpreference("pref_chat_wallpaper")); bindpreferencesummarytovalue(findpreference("pref_chat_cloud")); } } /** * fragment shows notification preferences only. used when * activity showing two-pane settings ui. */ @targetapi(build.version_codes.honeycomb) public static class notificationpreferencefragment extends preferencefragment { @override public void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); addpreferencesfromresource(r.xml.pref_notification); // bind summaries of edittext/list/dialog/ringtone preferences // values. when values change, summaries // updated reflect new value, per android design // guidelines. bindpreferencesummarytovalue(findpreference("pref_notification_new_contact")); bindpreferencesummarytovalue(findpreference("pref_notification_cat_individual_vibration")); bindpreferencesummarytovalue(findpreference("pref_notification_cat_individual_vibration_repeat")); bindpreferencesummarytovalue(findpreference("pref_notification_cat_individual_led")); bindpreferencesummarytovalue(findpreference("pref_notification_cat_individual_led_repeat")); bindpreferencesummarytovalue(findpreference("pref_notification_cat_individual_screen")); bindpreferencesummarytovalue(findpreference("pref_notification_cat_individual_screen_repeat")); bindpreferencesummarytovalue(findpreference("pref_notification_cat_group_vibration")); bindpreferencesummarytovalue(findpreference("pref_notification_cat_group_vibration_repeat")); bindpreferencesummarytovalue(findpreference("pref_notification_cat_group_led")); bindpreferencesummarytovalue(findpreference("pref_notification_cat_group_led_repeat")); bindpreferencesummarytovalue(findpreference("pref_notification_cat_group_screen")); bindpreferencesummarytovalue(findpreference("pref_notification_cat_group_screen_repeat")); } } /** * fragment shows contact preferences only. used when * activity showing two-pane settings ui. */ @targetapi(build.version_codes.honeycomb) public static class contactpreferencefragment extends preferencefragment { @override public void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); addpreferencesfromresource(r.xml.pref_contact); // bind summaries of edittext/list/dialog/ringtone preferences // values. when values change, summaries // updated reflect new value, per android design // guidelines. // bindpreferencesummarytovalue(findpreference("")); // bindpreferencesummarytovalue(findpreference("")); } } /** * fragment shows payment preferences only. used when * activity showing two-pane settings ui. */ @targetapi(build.version_codes.honeycomb) public static class paymentpreferencefragment extends preferencefragment { @override public void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); addpreferencesfromresource(r.xml.pref_payment); // bind summaries of edittext/list/dialog/ringtone preferences // values. when values change, summaries // updated reflect new value, per android design // guidelines. // bindpreferencesummarytovalue(findpreference("")); // bindpreferencesummarytovalue(findpreference("")); } } }
pref_headers.xml
<preference-headers xmlns:android="http://schemas.android.com/apk/res/android" > <!-- these settings headers used on tablets. --> <header android:fragment="com.example.settings$profilepreferencefragment" android:title="@string/pref_header_profile" /> <header android:fragment="com.example.settings$chatpreferencefragment" android:title="@string/pref_header_chat" /> <header android:fragment="com.example.settings$notificationpreferencefragment" android:title="@string/pref_header_notification" /> <header android:fragment="com.example.settings$contactpreferencefragment" android:title="@string/pref_header_contact" /> <header android:fragment="com.example.settings$paymentpreferencefragment" android:title="@string/pref_header_payment" /> </preference-headers>
and 1 section example, others this. pref_notification.xml
<preferencescreen xmlns:android="http://schemas.android.com/apk/res/android" > <switchpreference android:key="pref_notification_new_contact" android:summary="@string/pref_notification_new_contact_summary" android:title="@string/pref_notification_new_contact_title" /> <preferencecategory android:key="pref_notification_cat_individual" android:title="@string/pref_notification_cat_individual" > <switchpreference android:defaultvalue="true" android:key="pref_notification_cat_individual_vibration" android:title="@string/pref_notification_cat_individual_vibration_title" /> <checkboxpreference android:defaultvalue="true" android:dependency="pref_notification_cat_individual_vibration" android:key="pref_notification_cat_individual_vibration_repeat" android:summary="@string/pref_notification_cat_individual_vibration_repeat_summary" android:title="@string/pref_notification_cat_individual_vibration_repeat" /> <switchpreference android:defaultvalue="true" android:key="pref_notification_cat_individual_led" android:title="@string/pref_notification_cat_individual_led_title" /> <checkboxpreference android:defaultvalue="true" android:dependency="pref_notification_cat_individual_led" android:key="pref_notification_cat_individual_led_repeat" android:summary="@string/pref_notification_cat_individual_led_repeat_summary" android:title="@string/pref_notification_cat_individual_led_repeat" /> <switchpreference android:defaultvalue="true" android:key="pref_notification_cat_individual_screen" android:title="@string/pref_notification_cat_individual_screen_title" /> <checkboxpreference android:defaultvalue="true" android:dependency="pref_notification_cat_individual_screen" android:key="pref_notification_cat_individual_screen_repeat" android:summary="@string/pref_notification_cat_individual_screen_repeat_summary" android:title="@string/pref_notification_cat_individual_screen_repeat" /> </preferencecategory> <preferencecategory android:key="pref_notification_cat_group" android:title="@string/pref_notification_cat_group" > <switchpreference android:defaultvalue="true" android:key="pref_notification_cat_group_vibration" android:title="@string/pref_notification_cat_group_vibration_title" /> <checkboxpreference android:defaultvalue="true" android:dependency="pref_notification_cat_group_vibration" android:key="pref_notification_cat_group_vibration_repeat" android:summary="@string/pref_notification_cat_group_vibration_repeat_summary" android:title="@string/pref_notification_cat_group_vibration_repeat" /> <switchpreference android:defaultvalue="true" android:key="pref_notification_cat_group_led" android:title="@string/pref_notification_cat_group_led_title" /> <checkboxpreference android:defaultvalue="true" android:dependency="pref_notification_cat_group_led" android:key="pref_notification_cat_group_led_repeat" android:summary="@string/pref_notification_cat_group_led_repeat_summary" android:title="@string/pref_notification_cat_group_led_repeat" /> <switchpreference android:defaultvalue="true" android:key="pref_notification_cat_group_screen" android:title="@string/pref_notification_cat_group_screen_title" /> <checkboxpreference android:defaultvalue="true" android:dependency="pref_notification_cat_group_screen" android:key="pref_notification_cat_group_screen_repeat" android:summary="@string/pref_notification_cat_group_screen_repeat_summary" android:title="@string/pref_notification_cat_group_screen_repeat" /> </preferencecategory> </preferencescreen>
Comments
Post a Comment