Home | History | Annotate | Download | only in preference
      1 /*
      2  * Copyright (C) 2010 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.os.Bundle;
     22 import android.preference.PreferenceActivity;
     23 import android.preference.PreferenceFragment;
     24 import android.preference.PreferenceManager;
     25 import android.util.Log;
     26 import android.widget.Button;
     27 
     28 import java.util.List;
     29 
     30 /**
     31  * Demonstration of PreferenceActivity to make a top-level preference
     32  * panel with headers.
     33  */
     34 //BEGIN_INCLUDE(activity)
     35 public class PreferenceWithHeaders extends PreferenceActivity {
     36     @Override
     37     protected void onCreate(Bundle savedInstanceState) {
     38         super.onCreate(savedInstanceState);
     39 
     40         // Add a button to the header list.
     41         if (hasHeaders()) {
     42             Button button = new Button(this);
     43             button.setText("Some action");
     44             setListFooter(button);
     45         }
     46     }
     47 
     48     /**
     49      * Populate the activity with the top-level headers.
     50      */
     51     @Override
     52     public void onBuildHeaders(List<Header> target) {
     53         loadHeadersFromResource(R.xml.preference_headers, target);
     54     }
     55 
     56     /**
     57      * This fragment shows the preferences for the first header.
     58      */
     59     public static class Prefs1Fragment extends PreferenceFragment {
     60         @Override
     61         public void onCreate(Bundle savedInstanceState) {
     62             super.onCreate(savedInstanceState);
     63 
     64             // Make sure default values are applied.  In a real app, you would
     65             // want this in a shared function that is used to retrieve the
     66             // SharedPreferences wherever they are needed.
     67             PreferenceManager.setDefaultValues(getActivity(),
     68                     R.xml.advanced_preferences, false);
     69 
     70             // Load the preferences from an XML resource
     71             addPreferencesFromResource(R.xml.fragmented_preferences);
     72         }
     73     }
     74 
     75     /**
     76      * This fragment contains a second-level set of preference that you
     77      * can get to by tapping an item in the first preferences fragment.
     78      */
     79     public static class Prefs1FragmentInner extends PreferenceFragment {
     80         @Override
     81         public void onCreate(Bundle savedInstanceState) {
     82             super.onCreate(savedInstanceState);
     83 
     84             // Can retrieve arguments from preference XML.
     85             Log.i("args", "Arguments: " + getArguments());
     86 
     87             // Load the preferences from an XML resource
     88             addPreferencesFromResource(R.xml.fragmented_preferences_inner);
     89         }
     90     }
     91 
     92     /**
     93      * This fragment shows the preferences for the second header.
     94      */
     95     public static class Prefs2Fragment extends PreferenceFragment {
     96         @Override
     97         public void onCreate(Bundle savedInstanceState) {
     98             super.onCreate(savedInstanceState);
     99 
    100             // Can retrieve arguments from headers XML.
    101             Log.i("args", "Arguments: " + getArguments());
    102 
    103             // Load the preferences from an XML resource
    104             addPreferencesFromResource(R.xml.preference_dependencies);
    105         }
    106     }
    107 }
    108 //END_INCLUDE(activity)
    109