Home | History | Annotate | Download | only in preference
      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.app.Activity;
     22 import android.content.Intent;
     23 import android.content.SharedPreferences;
     24 import android.os.Bundle;
     25 import android.preference.PreferenceManager;
     26 import android.view.View;
     27 import android.view.View.OnClickListener;
     28 import android.widget.Button;
     29 import android.widget.LinearLayout;
     30 import android.widget.TextView;
     31 import android.widget.LinearLayout.LayoutParams;
     32 
     33 /**
     34  * Demonstrates launching a PreferenceActivity and grabbing a value it saved.
     35  */
     36 public class LaunchingPreferences extends Activity implements OnClickListener {
     37 
     38     private static final int REQUEST_CODE_PREFERENCES = 1;
     39 
     40     private TextView mCounterText;
     41 
     42     @Override
     43     protected void onCreate(Bundle savedInstanceState) {
     44         super.onCreate(savedInstanceState);
     45 
     46         /*
     47          * If this were my app's main activity, I would load the default values
     48          * so they're set even if the user does not go into the preferences
     49          * screen. Another good place to call this method would be from a
     50          * subclass of Application, so your default values would be loaded
     51          * regardless of entry into your application (for example, a service or
     52          * activity).
     53          */
     54         PreferenceManager.setDefaultValues(this, R.xml.advanced_preferences, false);
     55 
     56         // Simple layout
     57         LinearLayout layout = new LinearLayout(this);
     58         layout.setOrientation(LinearLayout.VERTICAL);
     59         setContentView(layout);
     60 
     61         // Create a simple button that will launch the preferences
     62         Button launchPreferences = new Button(this);
     63         launchPreferences.setText(getString(R.string.launch_preference_activity));
     64         launchPreferences.setOnClickListener(this);
     65         layout.addView(launchPreferences, new LayoutParams(LayoutParams.MATCH_PARENT,
     66                 LayoutParams.WRAP_CONTENT));
     67 
     68         mCounterText = new TextView(this);
     69         layout.addView(mCounterText, new LayoutParams(LayoutParams.MATCH_PARENT,
     70                 LayoutParams.WRAP_CONTENT));
     71 
     72         updateCounterText();
     73     }
     74 
     75     public void onClick(View v) {
     76 
     77         // When the button is clicked, launch an activity through this intent
     78         Intent launchPreferencesIntent = new Intent().setClass(this, AdvancedPreferences.class);
     79 
     80         // Make it a subactivity so we know when it returns
     81         startActivityForResult(launchPreferencesIntent, REQUEST_CODE_PREFERENCES);
     82     }
     83 
     84     @Override
     85     protected void onActivityResult(int requestCode, int resultCode, Intent data) {
     86         super.onActivityResult(requestCode, resultCode, data);
     87 
     88         // The preferences returned if the request code is what we had given
     89         // earlier in startSubActivity
     90         if (requestCode == REQUEST_CODE_PREFERENCES) {
     91             // Read a sample value they have set
     92             updateCounterText();
     93         }
     94     }
     95 
     96     private void updateCounterText() {
     97         // Since we're in the same package, we can use this context to get
     98         // the default shared preferences
     99         SharedPreferences sharedPref = PreferenceManager.getDefaultSharedPreferences(this);
    100         final int counter = sharedPref.getInt(AdvancedPreferences.KEY_MY_PREFERENCE, 0);
    101         mCounterText.setText(getString(R.string.counter_value_is) + " " + counter);
    102     }
    103 }
    104