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