SHARE
TWEET

Preference from Code - Android API Demos

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