• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2015 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.support.v7.preference;
18 
19 import android.content.Context;
20 import android.util.AttributeSet;
21 
22 /**
23  * Represents a top-level {@link Preference} that
24  * is the root of a Preference hierarchy. A {@link PreferenceFragmentCompat}
25  * points to an instance of this class to show the preferences. To instantiate
26  * this class, use {@link PreferenceManager#createPreferenceScreen(android.content.Context)}.
27  * <ul>
28  * This class can appear in two places:
29  * <li> When a {@link PreferenceFragmentCompat} points to this, it is used as the root
30  * and is not shown (only the contained preferences are shown).
31  * <li> When it appears inside another preference hierarchy, it is shown and
32  * serves as the gateway to another screen of preferences (either by showing
33  * another screen of preferences as a {@link android.app.Dialog} or via a
34  * {@link android.content.Context#startActivity(android.content.Intent)} from the
35  * {@link Preference#getIntent()}). The children of this {@link PreferenceScreen}
36  * are NOT shown in the screen that this {@link PreferenceScreen} is shown in.
37  * Instead, a separate screen will be shown when this preference is clicked.
38  * </ul>
39  * <p>Here's an example XML layout of a PreferenceScreen:</p>
40  * <pre>
41  &lt;PreferenceScreen
42  xmlns:android="http://schemas.android.com/apk/res/android"
43  android:key="first_preferencescreen"&gt;
44  &lt;CheckBoxPreference
45  android:key="wifi enabled"
46  android:title="WiFi" /&gt;
47  &lt;PreferenceScreen
48  android:key="second_preferencescreen"
49  android:title="WiFi settings"&gt;
50  &lt;CheckBoxPreference
51  android:key="prefer wifi"
52  android:title="Prefer WiFi" /&gt;
53  ... other preferences here ...
54  &lt;/PreferenceScreen&gt;
55  &lt;/PreferenceScreen&gt; </pre>
56  * <p>
57  * In this example, the "first_preferencescreen" will be used as the root of the
58  * hierarchy and given to a {@link android.support.v14.preference.PreferenceFragment}
59  * or {@link PreferenceFragmentCompat}.
60  * The first screen will
61  * show preferences "WiFi" (which can be used to quickly enable/disable WiFi)
62  * and "WiFi settings". The "WiFi settings" is the "second_preferencescreen" and when
63  * clicked will show another screen of preferences such as "Prefer WiFi" (and
64  * the other preferences that are children of the "second_preferencescreen" tag).
65  *
66  * <div class="special reference">
67  * <h3>Developer Guides</h3>
68  * <p>For information about building a settings UI with Preferences,
69  * read the <a href="{@docRoot}guide/topics/ui/settings.html">Settings</a>
70  * guide.</p>
71  * </div>
72  *
73  * @see PreferenceCategory
74  */
75 public final class PreferenceScreen extends PreferenceGroup  {
76 
77     /**
78      * Do NOT use this constructor, use {@link PreferenceManager#createPreferenceScreen(Context)}.
79      * @hide-
80      */
PreferenceScreen(Context context, AttributeSet attrs)81     public PreferenceScreen(Context context, AttributeSet attrs) {
82         super(context, attrs, R.attr.preferenceScreenStyle);
83     }
84 
85     @Override
onClick()86     protected void onClick() {
87         if (getIntent() != null || getFragment() != null || getPreferenceCount() == 0) {
88             return;
89         }
90         final PreferenceManager.OnNavigateToScreenListener listener =
91                 getPreferenceManager().getOnNavigateToScreenListener();
92         if (listener != null) {
93             listener.onNavigateToScreen(this);
94         }
95     }
96 
97     @Override
isOnSameScreenAsChildren()98     protected boolean isOnSameScreenAsChildren() {
99         return false;
100     }
101 
102 }
103