Update preference example to show switching with fragments.
Change-Id: I2b1db31b5e1a0e83d843ad68b0a177048d2914f4
diff --git a/samples/ApiDemos/res/layout-land/fragment_preferences.xml b/samples/ApiDemos/res/layout-land/fragment_preferences.xml
index 6b81f3d..8084e9d 100644
--- a/samples/ApiDemos/res/layout-land/fragment_preferences.xml
+++ b/samples/ApiDemos/res/layout-land/fragment_preferences.xml
@@ -20,7 +20,7 @@
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="match_parent">
- <fragment android:name="com.example.android.apis.app.FragmentPreferences$CategoriesFragment"
+ <fragment android:name="com.example.android.apis.preference.FragmentPreferences$CategoriesFragment"
android:id="@+id/categories"
android:layout_width="0px"
android:layout_height="match_parent"
diff --git a/samples/ApiDemos/res/values/strings.xml b/samples/ApiDemos/res/values/strings.xml
index 749a28f..94c931d 100644
--- a/samples/ApiDemos/res/values/strings.xml
+++ b/samples/ApiDemos/res/values/strings.xml
@@ -420,6 +420,9 @@
<string name="title_screen_preference">Screen preference</string>
<string name="summary_screen_preference">Shows another screen of preferences</string>
+ <string name="title_fragment_preference">Fragment preference</string>
+ <string name="summary_fragment_preference">Shows another fragment of preferences</string>
+
<string name="title_next_screen_toggle_preference">Toggle preference</string>
<string name="summary_next_screen_toggle_preference">Preference that is on the next screen but same hierarchy</string>
diff --git a/samples/ApiDemos/res/xml/advanced_preferences.xml b/samples/ApiDemos/res/xml/advanced_preferences.xml
index c362297..dd6de31 100644
--- a/samples/ApiDemos/res/xml/advanced_preferences.xml
+++ b/samples/ApiDemos/res/xml/advanced_preferences.xml
@@ -23,7 +23,7 @@
portion of the preference, if the whole preference wanted to be
replaced we would use the layout attribute instead of the widgetLayout
attribute. -->
- <com.example.android.apis.app.MyPreference
+ <com.example.android.apis.preference.MyPreference
android:key="my_preference"
android:title="@string/title_my_preference"
android:summary="@string/summary_my_preference"
diff --git a/samples/ApiDemos/res/xml/fragmented_preferences.xml b/samples/ApiDemos/res/xml/fragmented_preferences.xml
new file mode 100644
index 0000000..a0a242e
--- /dev/null
+++ b/samples/ApiDemos/res/xml/fragmented_preferences.xml
@@ -0,0 +1,95 @@
+<?xml version="1.0" encoding="utf-8"?>
+<!-- Copyright (C) 2010 The Android Open Source Project
+
+ Licensed under the Apache License, Version 2.0 (the "License");
+ you may not use this file except in compliance with the License.
+ You may obtain a copy of the License at
+
+ http://www.apache.org/licenses/LICENSE-2.0
+
+ Unless required by applicable law or agreed to in writing, software
+ distributed under the License is distributed on an "AS IS" BASIS,
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ See the License for the specific language governing permissions and
+ limitations under the License.
+-->
+
+<!-- This is a primitive example showing the different types of preferences available. -->
+<!-- BEGIN_INCLUDE(preferences) -->
+<PreferenceScreen
+ xmlns:android="http://schemas.android.com/apk/res/android">
+
+ <PreferenceCategory
+ android:title="@string/inline_preferences">
+
+ <CheckBoxPreference
+ android:key="checkbox_preference"
+ android:title="@string/title_toggle_preference"
+ android:summary="@string/summary_toggle_preference" />
+
+ </PreferenceCategory>
+
+ <PreferenceCategory
+ android:title="@string/dialog_based_preferences">
+
+ <EditTextPreference
+ android:key="edittext_preference"
+ android:title="@string/title_edittext_preference"
+ android:summary="@string/summary_edittext_preference"
+ android:dialogTitle="@string/dialog_title_edittext_preference" />
+
+ <ListPreference
+ android:key="list_preference"
+ android:title="@string/title_list_preference"
+ android:summary="@string/summary_list_preference"
+ android:entries="@array/entries_list_preference"
+ android:entryValues="@array/entryvalues_list_preference"
+ android:dialogTitle="@string/dialog_title_list_preference" />
+
+ </PreferenceCategory>
+
+ <PreferenceCategory
+ android:title="@string/launch_preferences">
+
+ <!-- This PreferenceScreen tag sends the user to a new fragment of
+ preferences. If running in a large screen, they can be embedded
+ inside of the overall preferences UI. -->
+ <PreferenceScreen
+ android:fragment="com.example.android.apis.preference.PreferenceWithHeaders$Prefs1FragmentInner"
+ android:title="@string/title_fragment_preference"
+ android:summary="@string/summary_fragment_preference">
+ </PreferenceScreen>
+
+ <!-- This PreferenceScreen tag sends the user to a completely different
+ activity, switching out of the current preferences UI. -->
+ <PreferenceScreen
+ android:title="@string/title_intent_preference"
+ android:summary="@string/summary_intent_preference">
+
+ <intent android:action="android.intent.action.VIEW"
+ android:data="http://www.android.com" />
+
+ </PreferenceScreen>
+
+ </PreferenceCategory>
+
+ <PreferenceCategory
+ android:title="@string/preference_attributes">
+
+ <CheckBoxPreference
+ android:key="parent_checkbox_preference"
+ android:title="@string/title_parent_preference"
+ android:summary="@string/summary_parent_preference" />
+
+ <!-- The visual style of a child is defined by this styled theme attribute. -->
+ <CheckBoxPreference
+ android:key="child_checkbox_preference"
+ android:dependency="parent_checkbox_preference"
+ android:layout="?android:attr/preferenceLayoutChild"
+ android:title="@string/title_child_preference"
+ android:summary="@string/summary_child_preference" />
+
+ </PreferenceCategory>
+
+</PreferenceScreen>
+<!-- END_INCLUDE(preferences) -->
diff --git a/samples/ApiDemos/res/xml/fragmented_preferences_inner.xml b/samples/ApiDemos/res/xml/fragmented_preferences_inner.xml
new file mode 100644
index 0000000..f462c57
--- /dev/null
+++ b/samples/ApiDemos/res/xml/fragmented_preferences_inner.xml
@@ -0,0 +1,26 @@
+<?xml version="1.0" encoding="utf-8"?>
+<!-- Copyright (C) 2010 The Android Open Source Project
+
+ Licensed under the Apache License, Version 2.0 (the "License");
+ you may not use this file except in compliance with the License.
+ You may obtain a copy of the License at
+
+ http://www.apache.org/licenses/LICENSE-2.0
+
+ Unless required by applicable law or agreed to in writing, software
+ distributed under the License is distributed on an "AS IS" BASIS,
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ See the License for the specific language governing permissions and
+ limitations under the License.
+-->
+
+<!-- This is a primitive example showing the different types of preferences available. -->
+<!-- BEGIN_INCLUDE(preferences) -->
+<PreferenceScreen
+ xmlns:android="http://schemas.android.com/apk/res/android">
+ <CheckBoxPreference
+ android:key="next_screen_checkbox_preference"
+ android:title="@string/title_next_screen_toggle_preference"
+ android:summary="@string/summary_next_screen_toggle_preference" />
+</PreferenceScreen>
+<!-- END_INCLUDE(preferences) -->
diff --git a/samples/ApiDemos/src/com/example/android/apis/app/MyPreference.java b/samples/ApiDemos/src/com/example/android/apis/preference/MyPreference.java
similarity index 96%
rename from samples/ApiDemos/src/com/example/android/apis/app/MyPreference.java
rename to samples/ApiDemos/src/com/example/android/apis/preference/MyPreference.java
index 967c181..aeb8bf9 100644
--- a/samples/ApiDemos/src/com/example/android/apis/app/MyPreference.java
+++ b/samples/ApiDemos/src/com/example/android/apis/preference/MyPreference.java
@@ -14,7 +14,7 @@
* limitations under the License.
*/
-package com.example.android.apis.app;
+package com.example.android.apis.preference;
import com.example.android.apis.R;
@@ -33,18 +33,18 @@
*/
public class MyPreference extends Preference {
private int mClickCounter;
-
+
// This is the constructor called by the inflater
public MyPreference(Context context, AttributeSet attrs) {
super(context, attrs);
-
- setWidgetLayoutResource(R.layout.preference_widget_mypreference);
+
+ setWidgetLayoutResource(R.layout.preference_widget_mypreference);
}
@Override
protected void onBindView(View view) {
super.onBindView(view);
-
+
// Set our custom views inside the layout
final TextView myTextView = (TextView) view.findViewById(R.id.mypreference_widget);
if (myTextView != null) {
@@ -61,14 +61,14 @@
// They don't want the value to be set
return;
}
-
+
// Increment counter
mClickCounter = newValue;
-
+
// Save to persistent storage (this method will make sure this
// preference should be persistent, along with other useful checks)
persistInt(mClickCounter);
-
+
// Data has changed, notify so UI can be refreshed!
notifyChanged();
}
@@ -100,7 +100,7 @@
* must save the instance state so it is able to, for example, survive
* orientation changes.
*/
-
+
final Parcelable superState = super.onSaveInstanceState();
if (isPersistent()) {
// No need to save instance state since it's persistent
@@ -120,14 +120,14 @@
super.onRestoreInstanceState(state);
return;
}
-
+
// Restore the instance state
SavedState myState = (SavedState) state;
super.onRestoreInstanceState(myState.getSuperState());
mClickCounter = myState.clickCounter;
notifyChanged();
}
-
+
/**
* SavedState, a subclass of {@link BaseSavedState}, will store the state
* of MyPreference, a subclass of Preference.
@@ -136,10 +136,10 @@
*/
private static class SavedState extends BaseSavedState {
int clickCounter;
-
+
public SavedState(Parcel source) {
super(source);
-
+
// Restore the click counter
clickCounter = source.readInt();
}
@@ -147,7 +147,7 @@
@Override
public void writeToParcel(Parcel dest, int flags) {
super.writeToParcel(dest, flags);
-
+
// Save the click counter
dest.writeInt(clickCounter);
}
@@ -167,5 +167,5 @@
}
};
}
-
+
}
diff --git a/samples/ApiDemos/src/com/example/android/apis/preference/PreferenceWithHeaders.java b/samples/ApiDemos/src/com/example/android/apis/preference/PreferenceWithHeaders.java
index 874e787..2b8febc 100644
--- a/samples/ApiDemos/src/com/example/android/apis/preference/PreferenceWithHeaders.java
+++ b/samples/ApiDemos/src/com/example/android/apis/preference/PreferenceWithHeaders.java
@@ -30,21 +30,44 @@
*/
//BEGIN_INCLUDE(activity)
public class PreferenceWithHeaders extends PreferenceActivity {
+ /**
+ * Populate the activity with the top-level headers.
+ */
@Override
public void onBuildHeaders(List<Header> target) {
loadHeadersFromResource(R.xml.preference_headers, target);
}
+ /**
+ * This fragment shows the preferences for the first header.
+ */
public static class Prefs1Fragment extends PreferenceFragment {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// Load the preferences from an XML resource
- addPreferencesFromResource(R.xml.preferences);
+ addPreferencesFromResource(R.xml.fragmented_preferences);
}
}
+ /**
+ * This fragment contains a second-level set of preference that you
+ * can get to by tapping an item in the first preferences fragment.
+ */
+ public static class Prefs1FragmentInner extends PreferenceFragment {
+ @Override
+ public void onCreate(Bundle savedInstanceState) {
+ super.onCreate(savedInstanceState);
+
+ // Load the preferences from an XML resource
+ addPreferencesFromResource(R.xml.fragmented_preferences_inner);
+ }
+ }
+
+ /**
+ * This fragment shows the preferences for the second header.
+ */
public static class Prefs2Fragment extends PreferenceFragment {
@Override
public void onCreate(Bundle savedInstanceState) {