1 /* 2 * Copyright (C) 2010 The Android Open Source Project 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 17 package com.example.android.apis.preference; 18 19 import com.example.android.apis.R; 20 21 import android.os.Bundle; 22 import android.preference.PreferenceActivity; 23 import android.preference.PreferenceFragment; 24 import android.util.Log; 25 import android.widget.Button; 26 27 import java.util.List; 28 29 /** 30 * Demonstration of PreferenceActivity to make a top-level preference 31 * panel with headers. 32 */ 33 //BEGIN_INCLUDE(activity) 34 public class PreferenceWithHeaders extends PreferenceActivity { 35 @Override onCreate(Bundle savedInstanceState)36 protected void onCreate(Bundle savedInstanceState) { 37 super.onCreate(savedInstanceState); 38 39 // Add a button to the header list. 40 if (hasHeaders()) { 41 Button button = new Button(this); 42 button.setText("Some action"); 43 setListFooter(button); 44 } 45 } 46 47 /** 48 * Populate the activity with the top-level headers. 49 */ 50 @Override onBuildHeaders(List<Header> target)51 public void onBuildHeaders(List<Header> target) { 52 loadHeadersFromResource(R.xml.preference_headers, target); 53 } 54 55 /** 56 * This fragment shows the preferences for the first header. 57 */ 58 public static class Prefs1Fragment extends PreferenceFragment { 59 @Override onCreate(Bundle savedInstanceState)60 public void onCreate(Bundle savedInstanceState) { 61 super.onCreate(savedInstanceState); 62 63 // Load the preferences from an XML resource 64 addPreferencesFromResource(R.xml.fragmented_preferences); 65 } 66 } 67 68 /** 69 * This fragment contains a second-level set of preference that you 70 * can get to by tapping an item in the first preferences fragment. 71 */ 72 public static class Prefs1FragmentInner extends PreferenceFragment { 73 @Override onCreate(Bundle savedInstanceState)74 public void onCreate(Bundle savedInstanceState) { 75 super.onCreate(savedInstanceState); 76 77 // Can retrieve arguments from preference XML. 78 Log.i("args", "Arguments: " + getArguments()); 79 80 // Load the preferences from an XML resource 81 addPreferencesFromResource(R.xml.fragmented_preferences_inner); 82 } 83 } 84 85 /** 86 * This fragment shows the preferences for the second header. 87 */ 88 public static class Prefs2Fragment extends PreferenceFragment { 89 @Override onCreate(Bundle savedInstanceState)90 public void onCreate(Bundle savedInstanceState) { 91 super.onCreate(savedInstanceState); 92 93 // Can retrieve arguments from headers XML. 94 Log.i("args", "Arguments: " + getArguments()); 95 96 // Load the preferences from an XML resource 97 addPreferencesFromResource(R.xml.preference_dependencies); 98 } 99 } 100 } 101 //END_INCLUDE(activity) 102