Update preference example to show switching with fragments.
Change-Id: I2b1db31b5e1a0e83d843ad68b0a177048d2914f4
This commit is contained in:
@@ -20,7 +20,7 @@
|
|||||||
android:orientation="horizontal"
|
android:orientation="horizontal"
|
||||||
android:layout_width="match_parent"
|
android:layout_width="match_parent"
|
||||||
android:layout_height="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:id="@+id/categories"
|
||||||
android:layout_width="0px"
|
android:layout_width="0px"
|
||||||
android:layout_height="match_parent"
|
android:layout_height="match_parent"
|
||||||
|
|||||||
@@ -420,6 +420,9 @@
|
|||||||
<string name="title_screen_preference">Screen preference</string>
|
<string name="title_screen_preference">Screen preference</string>
|
||||||
<string name="summary_screen_preference">Shows another screen of preferences</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="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>
|
<string name="summary_next_screen_toggle_preference">Preference that is on the next screen but same hierarchy</string>
|
||||||
|
|
||||||
|
|||||||
@@ -23,7 +23,7 @@
|
|||||||
portion of the preference, if the whole preference wanted to be
|
portion of the preference, if the whole preference wanted to be
|
||||||
replaced we would use the layout attribute instead of the widgetLayout
|
replaced we would use the layout attribute instead of the widgetLayout
|
||||||
attribute. -->
|
attribute. -->
|
||||||
<com.example.android.apis.app.MyPreference
|
<com.example.android.apis.preference.MyPreference
|
||||||
android:key="my_preference"
|
android:key="my_preference"
|
||||||
android:title="@string/title_my_preference"
|
android:title="@string/title_my_preference"
|
||||||
android:summary="@string/summary_my_preference"
|
android:summary="@string/summary_my_preference"
|
||||||
|
|||||||
95
samples/ApiDemos/res/xml/fragmented_preferences.xml
Normal file
95
samples/ApiDemos/res/xml/fragmented_preferences.xml
Normal file
@@ -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) -->
|
||||||
26
samples/ApiDemos/res/xml/fragmented_preferences_inner.xml
Normal file
26
samples/ApiDemos/res/xml/fragmented_preferences_inner.xml
Normal file
@@ -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) -->
|
||||||
@@ -14,7 +14,7 @@
|
|||||||
* limitations under the License.
|
* limitations under the License.
|
||||||
*/
|
*/
|
||||||
|
|
||||||
package com.example.android.apis.app;
|
package com.example.android.apis.preference;
|
||||||
|
|
||||||
import com.example.android.apis.R;
|
import com.example.android.apis.R;
|
||||||
|
|
||||||
@@ -33,18 +33,18 @@ import android.widget.TextView;
|
|||||||
*/
|
*/
|
||||||
public class MyPreference extends Preference {
|
public class MyPreference extends Preference {
|
||||||
private int mClickCounter;
|
private int mClickCounter;
|
||||||
|
|
||||||
// This is the constructor called by the inflater
|
// This is the constructor called by the inflater
|
||||||
public MyPreference(Context context, AttributeSet attrs) {
|
public MyPreference(Context context, AttributeSet attrs) {
|
||||||
super(context, attrs);
|
super(context, attrs);
|
||||||
|
|
||||||
setWidgetLayoutResource(R.layout.preference_widget_mypreference);
|
setWidgetLayoutResource(R.layout.preference_widget_mypreference);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
protected void onBindView(View view) {
|
protected void onBindView(View view) {
|
||||||
super.onBindView(view);
|
super.onBindView(view);
|
||||||
|
|
||||||
// Set our custom views inside the layout
|
// Set our custom views inside the layout
|
||||||
final TextView myTextView = (TextView) view.findViewById(R.id.mypreference_widget);
|
final TextView myTextView = (TextView) view.findViewById(R.id.mypreference_widget);
|
||||||
if (myTextView != null) {
|
if (myTextView != null) {
|
||||||
@@ -61,14 +61,14 @@ public class MyPreference extends Preference {
|
|||||||
// They don't want the value to be set
|
// They don't want the value to be set
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Increment counter
|
// Increment counter
|
||||||
mClickCounter = newValue;
|
mClickCounter = newValue;
|
||||||
|
|
||||||
// Save to persistent storage (this method will make sure this
|
// Save to persistent storage (this method will make sure this
|
||||||
// preference should be persistent, along with other useful checks)
|
// preference should be persistent, along with other useful checks)
|
||||||
persistInt(mClickCounter);
|
persistInt(mClickCounter);
|
||||||
|
|
||||||
// Data has changed, notify so UI can be refreshed!
|
// Data has changed, notify so UI can be refreshed!
|
||||||
notifyChanged();
|
notifyChanged();
|
||||||
}
|
}
|
||||||
@@ -100,7 +100,7 @@ public class MyPreference extends Preference {
|
|||||||
* must save the instance state so it is able to, for example, survive
|
* must save the instance state so it is able to, for example, survive
|
||||||
* orientation changes.
|
* orientation changes.
|
||||||
*/
|
*/
|
||||||
|
|
||||||
final Parcelable superState = super.onSaveInstanceState();
|
final Parcelable superState = super.onSaveInstanceState();
|
||||||
if (isPersistent()) {
|
if (isPersistent()) {
|
||||||
// No need to save instance state since it's persistent
|
// No need to save instance state since it's persistent
|
||||||
@@ -120,14 +120,14 @@ public class MyPreference extends Preference {
|
|||||||
super.onRestoreInstanceState(state);
|
super.onRestoreInstanceState(state);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Restore the instance state
|
// Restore the instance state
|
||||||
SavedState myState = (SavedState) state;
|
SavedState myState = (SavedState) state;
|
||||||
super.onRestoreInstanceState(myState.getSuperState());
|
super.onRestoreInstanceState(myState.getSuperState());
|
||||||
mClickCounter = myState.clickCounter;
|
mClickCounter = myState.clickCounter;
|
||||||
notifyChanged();
|
notifyChanged();
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* SavedState, a subclass of {@link BaseSavedState}, will store the state
|
* SavedState, a subclass of {@link BaseSavedState}, will store the state
|
||||||
* of MyPreference, a subclass of Preference.
|
* of MyPreference, a subclass of Preference.
|
||||||
@@ -136,10 +136,10 @@ public class MyPreference extends Preference {
|
|||||||
*/
|
*/
|
||||||
private static class SavedState extends BaseSavedState {
|
private static class SavedState extends BaseSavedState {
|
||||||
int clickCounter;
|
int clickCounter;
|
||||||
|
|
||||||
public SavedState(Parcel source) {
|
public SavedState(Parcel source) {
|
||||||
super(source);
|
super(source);
|
||||||
|
|
||||||
// Restore the click counter
|
// Restore the click counter
|
||||||
clickCounter = source.readInt();
|
clickCounter = source.readInt();
|
||||||
}
|
}
|
||||||
@@ -147,7 +147,7 @@ public class MyPreference extends Preference {
|
|||||||
@Override
|
@Override
|
||||||
public void writeToParcel(Parcel dest, int flags) {
|
public void writeToParcel(Parcel dest, int flags) {
|
||||||
super.writeToParcel(dest, flags);
|
super.writeToParcel(dest, flags);
|
||||||
|
|
||||||
// Save the click counter
|
// Save the click counter
|
||||||
dest.writeInt(clickCounter);
|
dest.writeInt(clickCounter);
|
||||||
}
|
}
|
||||||
@@ -167,5 +167,5 @@ public class MyPreference extends Preference {
|
|||||||
}
|
}
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
@@ -30,21 +30,44 @@ import java.util.List;
|
|||||||
*/
|
*/
|
||||||
//BEGIN_INCLUDE(activity)
|
//BEGIN_INCLUDE(activity)
|
||||||
public class PreferenceWithHeaders extends PreferenceActivity {
|
public class PreferenceWithHeaders extends PreferenceActivity {
|
||||||
|
/**
|
||||||
|
* Populate the activity with the top-level headers.
|
||||||
|
*/
|
||||||
@Override
|
@Override
|
||||||
public void onBuildHeaders(List<Header> target) {
|
public void onBuildHeaders(List<Header> target) {
|
||||||
loadHeadersFromResource(R.xml.preference_headers, target);
|
loadHeadersFromResource(R.xml.preference_headers, target);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* This fragment shows the preferences for the first header.
|
||||||
|
*/
|
||||||
public static class Prefs1Fragment extends PreferenceFragment {
|
public static class Prefs1Fragment extends PreferenceFragment {
|
||||||
@Override
|
@Override
|
||||||
public void onCreate(Bundle savedInstanceState) {
|
public void onCreate(Bundle savedInstanceState) {
|
||||||
super.onCreate(savedInstanceState);
|
super.onCreate(savedInstanceState);
|
||||||
|
|
||||||
// Load the preferences from an XML resource
|
// 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 {
|
public static class Prefs2Fragment extends PreferenceFragment {
|
||||||
@Override
|
@Override
|
||||||
public void onCreate(Bundle savedInstanceState) {
|
public void onCreate(Bundle savedInstanceState) {
|
||||||
|
|||||||
Reference in New Issue
Block a user