• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2012 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 android.preference.cts;
18 
19 import android.os.Bundle;
20 import android.preference.PreferenceActivity;
21 import android.preference.PreferenceFragment;
22 import android.widget.Button;
23 
24 import java.util.List;
25 
26 /**
27  * Demonstration of PreferenceActivity to make a top-level preference
28  * panel with headers.
29  */
30 
31 public class PreferenceWithHeaders extends PreferenceActivity {
32 
33     // For tests to verify if the headers were loaded.
34     public List<Header> loadedHeaders;
35 
36     public boolean onCreateFinished;
37 
38     @Override
onCreate(Bundle savedInstanceState)39     protected void onCreate(Bundle savedInstanceState) {
40         super.onCreate(savedInstanceState);
41         // Add a button to the header list.
42         if (hasHeaders()) {
43             Button button = new Button(this);
44             button.setText("Some action");
45             setListFooter(button);
46         }
47         onCreateFinished = true;
48     }
49 
50     /*
51      * Validate the fragment loaded into this activity. Required for apps built for API 19 and
52      * above.
53      */
isValidFragment(String fragment)54     protected boolean isValidFragment(String fragment) {
55         return PrefsOneFragment.class.getName().equals(fragment)
56                 || PrefsTwoFragment.class.getName().equals(fragment)
57                 || PrefsOneFragmentInner.class.getName().equals(fragment);
58     }
59 
60     /**
61      * Populate the activity with the top-level headers.
62      */
63     @Override
onBuildHeaders(List<Header> target)64     public void onBuildHeaders(List<Header> target) {
65         loadedHeaders = target;
66         loadHeadersFromResource(R.xml.preference_headers, target);
67     }
68 
69     public static class PrefsOneFragment extends PreferenceFragment {
70         @Override
onCreate(Bundle savedInstanceState)71         public void onCreate(Bundle savedInstanceState) {
72             super.onCreate(savedInstanceState);
73             addPreferencesFromResource(R.xml.preferences);
74         }
75     }
76 
77     public static class PrefsTwoFragment extends PreferenceFragment {
78         @Override
onCreate(Bundle savedInstanceState)79         public void onCreate(Bundle savedInstanceState) {
80             super.onCreate(savedInstanceState);
81             addPreferencesFromResource(R.xml.preferences2);
82         }
83     }
84 
85     public static class PrefsOneFragmentInner extends PreferenceFragment {
86         @Override
onCreate(Bundle savedInstanceState)87         public void onCreate(Bundle savedInstanceState) {
88             super.onCreate(savedInstanceState);
89             addPreferencesFromResource(R.xml.fragmented_preferences_inner);
90         }
91     }
92 }
93 
94