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.preference2.cts;
18 
19 import com.android.cts.preference2.R;
20 
21 import android.os.Bundle;
22 import android.preference.CheckBoxPreference;
23 import android.preference.EditTextPreference;
24 import android.preference.ListPreference;
25 import android.preference.Preference;
26 import android.preference.PreferenceActivity;
27 import android.preference.PreferenceCategory;
28 import android.preference.PreferenceGroup;
29 import android.preference.PreferenceScreen;
30 import android.preference.SwitchPreference;
31 
32 public class PreferenceFromCodeActivity extends PreferenceActivity {
33     private Preference mPref;
34     protected PreferenceScreen mPrefScreen;
35 
36     @Override
onCreate(Bundle savedInstanceState)37     protected void onCreate(Bundle savedInstanceState) {
38         super.onCreate(savedInstanceState);
39         mPrefScreen = getPreferenceManager().createPreferenceScreen(this);
40         addPreferenceCategory();
41         setPreferenceScreen(mPrefScreen);
42     }
43 
addPreferenceCategory()44     private void addPreferenceCategory() {
45         PreferenceCategory prefCat = new PreferenceCategory(this);
46         prefCat.setTitle(R.string.inline_preferences);
47         prefCat.setKey("pref-1");
48         mPrefScreen.addPreference(prefCat);
49 
50         //CheckBox Preference
51         CheckBoxPreference checkboxPref = new CheckBoxPreference(this);
52         checkboxPref.setKey("checkbox_preference");
53         checkboxPref.setIcon(R.drawable.ic_launcher);
54         checkboxPref.setTitle(R.string.title_checkbox_preference);
55         checkboxPref.setSummary(R.string.summary_checkbox_preference);
56         checkboxPref.setSummaryOn(R.string.summary_on_checkbox_preference);
57         checkboxPref.setSummaryOff(R.string.summary_off_checkbox_preference);
58         checkboxPref.setChecked(true);
59         checkboxPref.shouldDisableDependents();
60         prefCat.addPreference(checkboxPref);
61 
62         // Switch preference
63         SwitchPreference switchPref = new SwitchPreference(this);
64         switchPref.setKey("switch_preference");
65         switchPref.setTitle(R.string.title_switch_preference);
66         switchPref.setSummary(R.string.summary_switch_preference);
67         switchPref.setSummaryOn(R.string.summary_on_switch_preference);
68         switchPref.setSummaryOff(R.string.summary_off_switch_preference);
69         switchPref.setSwitchTextOff(R.string.switchtext_off);
70         switchPref.setSwitchTextOn(R.string.switchtext_on);
71         prefCat.addPreference(switchPref);
72 
73         SwitchPreference switchPref2 = new SwitchPreference(this);
74         switchPref2.setKey("switch_preference_2");
75         switchPref2.setTitle(R.string.title_switch_preference);
76         switchPref2.setSummary(R.string.summary_switch_preference);
77         switchPref2.setSummaryOn(R.string.summary_on_switch_preference);
78         switchPref2.setSummaryOff(R.string.summary_off_switch_preference);
79         prefCat.addPreference(switchPref2);
80 
81         // Dialog based preferences
82         PreferenceCategory dialogBasedPrefCat = new PreferenceCategory(this);
83         dialogBasedPrefCat.setTitle(R.string.dialog_based_preferences);
84         mPrefScreen.addPreference(dialogBasedPrefCat);
85 
86         // Edit text preference
87         EditTextPreference editTextPref = new EditTextPreference(this);
88         editTextPref.setDialogTitle(R.string.dialog_title_edittext_preference);
89         editTextPref.setKey("edittext_preference");
90         editTextPref.setTitle(R.string.title_edittext_preference);
91         editTextPref.setSummary(R.string.summary_edittext_preference);
92         editTextPref.setText(getResources().getString(R.string.text_edittext_preference));
93         dialogBasedPrefCat.addPreference(editTextPref);
94 
95         EditTextPreference dialogPref = new EditTextPreference(this);
96         dialogPref.setDialogTitle(R.string.dialogtitle_dialog_preference);
97         dialogPref.setKey("dialog_preference");
98         dialogPref.setTitle(R.string.title_dialog_preference);
99         dialogPref.setSummary(R.string.summary_dialog_preference);
100         dialogPref.setDialogIcon(R.drawable.ic_launcher);
101         dialogPref.setDialogMessage(R.string.dialog_message);
102         dialogPref.setNegativeButtonText(R.string.negative_button_text);
103         dialogPref.setPositiveButtonText(R.string.positive_button_text);
104 
105         dialogBasedPrefCat.addPreference(dialogPref);
106 
107         ListPreference listPref = new ListPreference(this);
108         listPref.setEntries(R.array.entries_list_preference);
109         listPref.setEntryValues(R.array.entryvalues_list_preference);
110         listPref.setDialogTitle(R.string.dialog_title_list_preference);
111         listPref.setKey("list_preference");
112         listPref.setTitle(R.string.title_list_preference);
113         listPref.setSummary(R.string.summary_list_preference);
114         dialogBasedPrefCat.addPreference(listPref);
115 
116         PreferenceGroup prefGroup = new PreferenceCategory(this);
117         prefGroup.setTitle(R.string.preference_group_title);
118         prefGroup.setKey("pref-group");
119         mPrefScreen.addPreference(prefGroup);
120 
121         CustomPreferenceGroup customPrefGroup = new CustomPreferenceGroup(this);
122         customPrefGroup.setTitle(R.string.custom_preference_group_title);
123         customPrefGroup.setKey("custom-pref-group");
124         mPrefScreen.addPreference(customPrefGroup);
125 
126         CustomPreference customPreference = new CustomPreference(this);
127         customPreference .setTitle(R.string.custom_preference);
128         customPreference.setKey("custom-preference");
129         mPrefScreen.addPreference(customPreference);
130     }
131 }
132