• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2007 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.content.Intent;
22 import android.content.res.TypedArray;
23 import android.net.Uri;
24 import android.os.Bundle;
25 import android.preference.CheckBoxPreference;
26 import android.preference.EditTextPreference;
27 import android.preference.ListPreference;
28 import android.preference.PreferenceActivity;
29 import android.preference.PreferenceCategory;
30 import android.preference.PreferenceScreen;
31 import android.preference.SwitchPreference;
32 
33 public class PreferencesFromCode extends PreferenceActivity {
34 
35     @Override
onCreate(Bundle savedInstanceState)36     protected void onCreate(Bundle savedInstanceState) {
37         super.onCreate(savedInstanceState);
38 
39         setPreferenceScreen(createPreferenceHierarchy());
40     }
41 
createPreferenceHierarchy()42     private PreferenceScreen createPreferenceHierarchy() {
43         // Root
44         PreferenceScreen root = getPreferenceManager().createPreferenceScreen(this);
45 
46         // Inline preferences
47         PreferenceCategory inlinePrefCat = new PreferenceCategory(this);
48         inlinePrefCat.setTitle(R.string.inline_preferences);
49         root.addPreference(inlinePrefCat);
50 
51         // Checkbox preference
52         CheckBoxPreference checkboxPref = new CheckBoxPreference(this);
53         checkboxPref.setKey("checkbox_preference");
54         checkboxPref.setTitle(R.string.title_checkbox_preference);
55         checkboxPref.setSummary(R.string.summary_checkbox_preference);
56         inlinePrefCat.addPreference(checkboxPref);
57 
58         // Switch preference
59         SwitchPreference switchPref = new SwitchPreference(this);
60         switchPref.setKey("switch_preference");
61         switchPref.setTitle(R.string.title_switch_preference);
62         switchPref.setSummary(R.string.summary_switch_preference);
63         inlinePrefCat.addPreference(switchPref);
64 
65         // Dialog based preferences
66         PreferenceCategory dialogBasedPrefCat = new PreferenceCategory(this);
67         dialogBasedPrefCat.setTitle(R.string.dialog_based_preferences);
68         root.addPreference(dialogBasedPrefCat);
69 
70         // Edit text preference
71         EditTextPreference editTextPref = new EditTextPreference(this);
72         editTextPref.setDialogTitle(R.string.dialog_title_edittext_preference);
73         editTextPref.setKey("edittext_preference");
74         editTextPref.setTitle(R.string.title_edittext_preference);
75         editTextPref.setSummary(R.string.summary_edittext_preference);
76         dialogBasedPrefCat.addPreference(editTextPref);
77 
78         // List preference
79         ListPreference listPref = new ListPreference(this);
80         listPref.setEntries(R.array.entries_list_preference);
81         listPref.setEntryValues(R.array.entryvalues_list_preference);
82         listPref.setDialogTitle(R.string.dialog_title_list_preference);
83         listPref.setKey("list_preference");
84         listPref.setTitle(R.string.title_list_preference);
85         listPref.setSummary(R.string.summary_list_preference);
86         dialogBasedPrefCat.addPreference(listPref);
87 
88         // Launch preferences
89         PreferenceCategory launchPrefCat = new PreferenceCategory(this);
90         launchPrefCat.setTitle(R.string.launch_preferences);
91         root.addPreference(launchPrefCat);
92 
93         /*
94          * The Preferences screenPref serves as a screen break (similar to page
95          * break in word processing). Like for other preference types, we assign
96          * a key here so that it is able to save and restore its instance state.
97          */
98         // Screen preference
99         PreferenceScreen screenPref = getPreferenceManager().createPreferenceScreen(this);
100         screenPref.setKey("screen_preference");
101         screenPref.setTitle(R.string.title_screen_preference);
102         screenPref.setSummary(R.string.summary_screen_preference);
103         launchPrefCat.addPreference(screenPref);
104 
105         /*
106          * You can add more preferences to screenPref that will be shown on the
107          * next screen.
108          */
109 
110         // Example of next screen toggle preference
111         CheckBoxPreference nextScreenCheckBoxPref = new CheckBoxPreference(this);
112         nextScreenCheckBoxPref.setKey("next_screen_toggle_preference");
113         nextScreenCheckBoxPref.setTitle(R.string.title_next_screen_toggle_preference);
114         nextScreenCheckBoxPref.setSummary(R.string.summary_next_screen_toggle_preference);
115         screenPref.addPreference(nextScreenCheckBoxPref);
116 
117         // Intent preference
118         PreferenceScreen intentPref = getPreferenceManager().createPreferenceScreen(this);
119         intentPref.setIntent(new Intent().setAction(Intent.ACTION_VIEW)
120                 .setData(Uri.parse("http://www.android.com")));
121         intentPref.setTitle(R.string.title_intent_preference);
122         intentPref.setSummary(R.string.summary_intent_preference);
123         launchPrefCat.addPreference(intentPref);
124 
125         // Preference attributes
126         PreferenceCategory prefAttrsCat = new PreferenceCategory(this);
127         prefAttrsCat.setTitle(R.string.preference_attributes);
128         root.addPreference(prefAttrsCat);
129 
130         // Visual parent toggle preference
131         CheckBoxPreference parentCheckBoxPref = new CheckBoxPreference(this);
132         parentCheckBoxPref.setTitle(R.string.title_parent_preference);
133         parentCheckBoxPref.setSummary(R.string.summary_parent_preference);
134         prefAttrsCat.addPreference(parentCheckBoxPref);
135 
136         // Visual child toggle preference
137         // See res/values/attrs.xml for the <declare-styleable> that defines
138         // TogglePrefAttrs.
139         TypedArray a = obtainStyledAttributes(R.styleable.TogglePrefAttrs);
140         CheckBoxPreference childCheckBoxPref = new CheckBoxPreference(this);
141         childCheckBoxPref.setTitle(R.string.title_child_preference);
142         childCheckBoxPref.setSummary(R.string.summary_child_preference);
143         childCheckBoxPref.setLayoutResource(
144                 a.getResourceId(R.styleable.TogglePrefAttrs_android_preferenceLayoutChild,
145                         0));
146         prefAttrsCat.addPreference(childCheckBoxPref);
147         a.recycle();
148 
149         return root;
150     }
151 }
152