Add sample code for PreferenceFragment.

Change-Id: I64222124daa117a2ea61dc55f2e26af78d3689c2
This commit is contained in:
Dianne Hackborn
2010-07-28 14:33:04 -07:00
parent 3a5897a4fe
commit 05c2cb0748
6 changed files with 201 additions and 11 deletions

View File

@@ -211,14 +211,16 @@
<!-- Fragment Samples --> <!-- Fragment Samples -->
<activity android:name=".app.FragmentAlertDialog" android:label="@string/fragment_alert_dialog"> <activity android:name=".app.FragmentAlertDialog"
android:label="@string/fragment_alert_dialog">
<intent-filter> <intent-filter>
<action android:name="android.intent.action.MAIN" /> <action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.SAMPLE_CODE" /> <category android:name="android.intent.category.SAMPLE_CODE" />
</intent-filter> </intent-filter>
</activity> </activity>
<activity android:name=".app.FragmentAnim" android:label="@string/fragment_anim"> <activity android:name=".app.FragmentAnim"
android:label="@string/fragment_anim">
<intent-filter> <intent-filter>
<action android:name="android.intent.action.MAIN" /> <action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.SAMPLE_CODE" /> <category android:name="android.intent.category.SAMPLE_CODE" />
@@ -233,14 +235,16 @@
</intent-filter> </intent-filter>
</activity> </activity>
<activity android:name=".app.FragmentDialog" android:label="@string/fragment_dialog"> <activity android:name=".app.FragmentDialog"
android:label="@string/fragment_dialog">
<intent-filter> <intent-filter>
<action android:name="android.intent.action.MAIN" /> <action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.SAMPLE_CODE" /> <category android:name="android.intent.category.SAMPLE_CODE" />
</intent-filter> </intent-filter>
</activity> </activity>
<activity android:name=".app.FragmentLayout" android:label="@string/fragment_layout"> <activity android:name=".app.FragmentLayout"
android:label="@string/fragment_layout">
<intent-filter> <intent-filter>
<action android:name="android.intent.action.MAIN" /> <action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.SAMPLE_CODE" /> <category android:name="android.intent.category.SAMPLE_CODE" />
@@ -249,42 +253,56 @@
<activity android:name=".app.FragmentLayout$DialogActivity" /> <activity android:name=".app.FragmentLayout$DialogActivity" />
<activity android:name=".app.FragmentListCursorLoader" android:label="@string/fragment_list_cursor_loader"> <activity android:name=".app.FragmentListCursorLoader"
android:label="@string/fragment_list_cursor_loader">
<intent-filter> <intent-filter>
<action android:name="android.intent.action.MAIN" /> <action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.SAMPLE_CODE" /> <category android:name="android.intent.category.SAMPLE_CODE" />
</intent-filter> </intent-filter>
</activity> </activity>
<activity android:name=".app.FragmentListArray" android:label="@string/fragment_list_array"> <activity android:name=".app.FragmentListArray"
android:label="@string/fragment_list_array">
<intent-filter> <intent-filter>
<action android:name="android.intent.action.MAIN" /> <action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.SAMPLE_CODE" /> <category android:name="android.intent.category.SAMPLE_CODE" />
</intent-filter> </intent-filter>
</activity> </activity>
<activity android:name=".app.FragmentMenu" android:label="@string/fragment_menu"> <activity android:name=".app.FragmentMenu"
android:label="@string/fragment_menu">
<intent-filter> <intent-filter>
<action android:name="android.intent.action.MAIN" /> <action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.SAMPLE_CODE" /> <category android:name="android.intent.category.SAMPLE_CODE" />
</intent-filter> </intent-filter>
</activity> </activity>
<activity android:name=".app.FragmentRetainInstance" android:label="@string/fragment_retain_instance"> <activity android:name=".app.FragmentPreferences"
android:label="@string/fragment_preferences">
<intent-filter> <intent-filter>
<action android:name="android.intent.action.MAIN" /> <action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.SAMPLE_CODE" /> <category android:name="android.intent.category.SAMPLE_CODE" />
</intent-filter> </intent-filter>
</activity> </activity>
<activity android:name=".app.FragmentReceiveResult" android:label="@string/fragment_receive_result"> <activity android:name=".app.FragmentRetainInstance"
android:label="@string/fragment_retain_instance">
<intent-filter> <intent-filter>
<action android:name="android.intent.action.MAIN" /> <action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.SAMPLE_CODE" /> <category android:name="android.intent.category.SAMPLE_CODE" />
</intent-filter> </intent-filter>
</activity> </activity>
<activity android:name=".app.FragmentStack" android:label="@string/fragment_stack"> <activity android:name=".app.FragmentReceiveResult"
android:label="@string/fragment_receive_result">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.SAMPLE_CODE" />
</intent-filter>
</activity>
<activity android:name=".app.FragmentStack"
android:label="@string/fragment_stack">
<intent-filter> <intent-filter>
<action android:name="android.intent.action.MAIN" /> <action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.SAMPLE_CODE" /> <category android:name="android.intent.category.SAMPLE_CODE" />

View File

@@ -0,0 +1,32 @@
<?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.
-->
<!-- Top-level content view for the layout fragment sample when displayed
in landscape. -->
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="match_parent">
<fragment android:name="com.example.android.apis.app.FragmentPreferences$CategoriesFragment"
android:id="@+id/categories"
android:layout_width="0px"
android:layout_height="match_parent"
android:layout_weight=".3" />
<FrameLayout android:id="@+id/prefs"
android:layout_width="0px"
android:layout_height="match_parent"
android:layout_weight=".7" />
</LinearLayout>

View File

@@ -19,7 +19,7 @@
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android" <FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent" android:layout_height="match_parent"> android:layout_width="match_parent" android:layout_height="match_parent">
<fragment xmlns:android="http://schemas.android.com/apk/res/android" <fragment
android:name="com.example.android.apis.app.FragmentLayout$TitlesFragment" android:name="com.example.android.apis.app.FragmentLayout$TitlesFragment"
android:id="@+id/titles" android:id="@+id/titles"
android:layout_width="match_parent" android:layout_height="match_parent" /> android:layout_width="match_parent" android:layout_height="match_parent" />

View File

@@ -0,0 +1,34 @@
<?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.
-->
<!-- Top-level content view for the preference fragment sample. -->
<!-- BEGIN_INCLUDE(layout) -->
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<fragment android:name="com.example.android.apis.app.FragmentPreferences$CategoriesFragment"
android:id="@+id/categories"
android:layout_width="match_parent"
android:layout_height="0px"
android:layout_weight=".3" />
<FrameLayout android:id="@+id/prefs"
android:layout_width="match_parent"
android:layout_height="0px"
android:layout_weight=".7" />
</LinearLayout>
<!-- END_INCLUDE(layout) -->

View File

@@ -107,6 +107,8 @@
<string name="fragment1menu">Show fragment 1 menu</string> <string name="fragment1menu">Show fragment 1 menu</string>
<string name="fragment2menu">Show fragment 2 menu</string> <string name="fragment2menu">Show fragment 2 menu</string>
<string name="fragment_preferences">App/Fragment/Preferences</string>
<string name="fragment_retain_instance">App/Fragment/Retain Instance</string> <string name="fragment_retain_instance">App/Fragment/Retain Instance</string>
<string name="fragment_retain_instance_msg">Current progress of retained fragment; <string name="fragment_retain_instance_msg">Current progress of retained fragment;
restarts if fragment is re-created.</string> restarts if fragment is re-created.</string>

View File

@@ -0,0 +1,104 @@
/*
* 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.
*/
package com.example.android.apis.app;
import com.example.android.apis.R;
import com.example.android.apis.Shakespeare;
import android.app.Activity;
import android.app.Fragment;
import android.app.ListFragment;
import android.content.Context;
import android.content.Intent;
import android.content.res.TypedArray;
import android.net.Uri;
import android.os.Bundle;
import android.preference.CheckBoxPreference;
import android.preference.EditTextPreference;
import android.preference.ListPreference;
import android.preference.PreferenceCategory;
import android.preference.PreferenceFragment;
import android.preference.PreferenceScreen;
import android.util.Log;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.ListView;
/**
* Demonstration of PreferenceFragment, splitting the activity into a
* list categories and preferences.
*/
//BEGIN_INCLUDE(activity)
public class FragmentPreferences extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// The content for this activity places CategoriesFragment to one
// side, and leaves the rest to dynamically add the prefs fragment.
setContentView(R.layout.fragment_preferences);
}
/**
* This fragment shows a list of categories the user can pick. When they
* pick one, the corresponding preferences fragment is created and shown.
*/
public static class CategoriesFragment extends ListFragment {
@Override
public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
setListAdapter(new ArrayAdapter<String>(getActivity(),
android.R.layout.simple_list_item_1,
new String[] { "Prefs 1", "Prefs 2" }));
switchPreferences(0);
}
@Override
public void onListItemClick(ListView l, View v, int position, long id) {
switchPreferences(position);
}
/**
* Show the given preferences, replacing whatever was last shown.
*/
void switchPreferences(int which) {
Fragment f = which == 0 ? new Prefs1Fragment() : new Prefs2Fragment();
getActivity().openFragmentTransaction().replace(R.id.prefs, f).commit();
}
}
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);
}
}
public static class Prefs2Fragment extends PreferenceFragment {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// Load the preferences from an XML resource
addPreferencesFromResource(R.xml.preference_dependencies);
}
}
}
//END_INCLUDE(activity)