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