am 2fd75c63: Move switch preference into its own demo.

* commit '2fd75c63684e4ada9e00e1d31304519e60f5a22a':
  Move switch preference into its own demo.
This commit is contained in:
Dianne Hackborn
2011-11-14 14:35:56 -08:00
committed by Android Git Automerger
11 changed files with 123 additions and 50 deletions

View File

@@ -952,6 +952,15 @@
</intent-filter> </intent-filter>
</activity> </activity>
<activity android:name=".preference.SwitchPreference"
android:label="@string/switch_preference"
android:enabled="@bool/atLeastIceCreamSandwich">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.SAMPLE_CODE" />
</intent-filter>
</activity>
<!-- ************************************* --> <!-- ************************************* -->
<!-- CONTENT PACKAGE SAMPLES --> <!-- CONTENT PACKAGE SAMPLES -->
<!-- ************************************* --> <!-- ************************************* -->

View File

@@ -525,6 +525,7 @@
<string name="advanced_preferences">Preference/6. Advanced preferences</string> <string name="advanced_preferences">Preference/6. Advanced preferences</string>
<string name="fragment_preferences">Preference/7. Fragment</string> <string name="fragment_preferences">Preference/7. Fragment</string>
<string name="preference_with_headers">Preference/8. Headers</string> <string name="preference_with_headers">Preference/8. Headers</string>
<string name="switch_preference">Preference/9. Switch</string>
<string name="launch_preference_activity">Launch PreferenceActivity</string> <string name="launch_preference_activity">Launch PreferenceActivity</string>
<string name="counter_value_is">The counter value is</string> <string name="counter_value_is">The counter value is</string>

View File

@@ -25,12 +25,6 @@
android:title="@string/title_checkbox_preference" android:title="@string/title_checkbox_preference"
android:summary="@string/summary_checkbox_preference" /> android:summary="@string/summary_checkbox_preference" />
<SwitchPreference
android:key="default_switch"
android:defaultValue="false"
android:title="@string/title_switch_preference"
android:summary="@string/summary_switch_preference" />
<EditTextPreference <EditTextPreference
android:key="default_edittext" android:key="default_edittext"
android:defaultValue="@string/default_value_edittext_preference" android:defaultValue="@string/default_value_edittext_preference"

View File

@@ -27,11 +27,6 @@
android:title="@string/title_checkbox_preference" android:title="@string/title_checkbox_preference"
android:summary="@string/summary_checkbox_preference" /> android:summary="@string/summary_checkbox_preference" />
<SwitchPreference
android:key="checkbox_preference"
android:title="@string/title_switch_preference"
android:summary="@string/summary_switch_preference" />
</PreferenceCategory> </PreferenceCategory>
<PreferenceCategory <PreferenceCategory

View File

@@ -0,0 +1,46 @@
<?xml version="1.0" encoding="utf-8"?>
<!-- Copyright (C) 2011 The Android Open Source Project
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
-->
<!-- This is a primitive example showing the different types of preferences available. -->
<!-- BEGIN_INCLUDE(preferences) -->
<PreferenceScreen
xmlns:android="http://schemas.android.com/apk/res/android">
<PreferenceCategory
android:title="@string/inline_preferences">
<CheckBoxPreference
android:key="checkbox_preference"
android:title="@string/title_checkbox_preference"
android:summary="@string/summary_checkbox_preference" />
<SwitchPreference
android:key="checkbox_preference"
android:title="@string/title_switch_preference"
android:summary="@string/summary_switch_preference" />
<SwitchPreference
android:key="checkbox_preference"
android:title="@string/title_switch_preference"
android:summary="@string/summary_switch_preference_yes_no"
android:switchTextOn = "YES"
android:switchTextOff = "NO"
android:defaultValue="true" />
</PreferenceCategory>
</PreferenceScreen>
<!-- END_INCLUDE(preferences) -->

View File

@@ -27,18 +27,6 @@
android:title="@string/title_checkbox_preference" android:title="@string/title_checkbox_preference"
android:summary="@string/summary_checkbox_preference" /> android:summary="@string/summary_checkbox_preference" />
<SwitchPreference
android:key="checkbox_preference"
android:title="@string/title_switch_preference"
android:summary="@string/summary_switch_preference" />
<SwitchPreference
android:key="checkbox_preference"
android:title="@string/title_switch_preference"
android:summary="@string/summary_switch_preference_yes_no"
android:switchTextOn = "YES"
android:switchTextOff = "NO" />
</PreferenceCategory> </PreferenceCategory>
<PreferenceCategory <PreferenceCategory

View File

@@ -17,12 +17,12 @@
package com.example.android.apis; package com.example.android.apis;
import android.app.Application; import android.app.Application;
import android.preference.PreferenceManager;
/** /**
* This is an example of a {@link android.app.Application} class. Ordinarily you would use * This is an example of a {@link android.app.Application} class. This can
* a class like this as a central repository for information that might be shared between multiple * be used as a central repository for per-process information about your app;
* activities. * however it is recommended to use singletons for that instead rather than merge
* all of these globals from across your application into one place here.
* *
* In this case, we have not defined any specific work for this Application. * In this case, we have not defined any specific work for this Application.
* *
@@ -30,17 +30,7 @@ import android.preference.PreferenceManager;
* of how to perform unit tests on an Application object. * of how to perform unit tests on an Application object.
*/ */
public class ApiDemosApplication extends Application { public class ApiDemosApplication extends Application {
@Override @Override
public void onCreate() { public void onCreate() {
/*
* This populates the default values from the preferences XML file. See
* {@link DefaultValues} for more details.
*/
PreferenceManager.setDefaultValues(this, R.xml.default_values, false);
}
@Override
public void onTerminate() {
} }
} }

View File

@@ -16,9 +16,9 @@
package com.example.android.apis.preference; package com.example.android.apis.preference;
import com.example.android.apis.ApiDemosApplication;
import com.example.android.apis.R; import com.example.android.apis.R;
import android.content.Context;
import android.content.SharedPreferences; import android.content.SharedPreferences;
import android.os.Bundle; import android.os.Bundle;
import android.preference.PreferenceActivity; import android.preference.PreferenceActivity;
@@ -34,20 +34,28 @@ import android.preference.PreferenceManager;
* {@link PreferenceManager#setDefaultValues(android.content.Context, int, boolean)}. * {@link PreferenceManager#setDefaultValues(android.content.Context, int, boolean)}.
* <p> * <p>
* This should be called early, typically when the application is first created. * This should be called early, typically when the application is first created.
* This ensures any of the application's activities, services, etc. will have * An easy way to do this is to have a common function for retrieving the
* the default values present, even if the user has not wandered into the * SharedPreferences that takes care of calling it.
* application's settings. For ApiDemos, this is {@link ApiDemosApplication},
* and you can find the call to
* {@link PreferenceManager#setDefaultValues(android.content.Context, int, boolean)}
* in its {@link ApiDemosApplication#onCreate() onCreate}.
*/ */
public class DefaultValues extends PreferenceActivity { public class DefaultValues extends PreferenceActivity {
// This is the global (to the .apk) name under which we store these
// preferences. We want this to be unique from other preferences so that
// we do not have unexpected name conflicts, and the framework can correctly
// determine whether these preferences' defaults have already been written.
static final String PREFS_NAME = "defaults";
@Override @Override
protected void onCreate(Bundle savedInstanceState) { protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState); super.onCreate(savedInstanceState);
getPrefs(this);
getPreferenceManager().setSharedPreferencesName(PREFS_NAME);
addPreferencesFromResource(R.xml.default_values); addPreferencesFromResource(R.xml.default_values);
} }
static SharedPreferences getPrefs(Context context) {
PreferenceManager.setDefaultValues(context, PREFS_NAME, MODE_PRIVATE,
R.xml.default_values, false);
return context.getSharedPreferences(PREFS_NAME, MODE_PRIVATE);
}
} }

View File

@@ -44,12 +44,9 @@ public class LaunchingPreferences extends Activity implements OnClickListener {
super.onCreate(savedInstanceState); super.onCreate(savedInstanceState);
/* /*
* If this were my app's main activity, I would load the default values * These preferences have defaults, so before using them go apply those
* so they're set even if the user does not go into the preferences * defaults. This will only execute once -- when the defaults are applied
* screen. Another good place to call this method would be from a * a boolean preference is set so they will not be applied again.
* subclass of Application, so your default values would be loaded
* regardless of entry into your application (for example, a service or
* activity).
*/ */
PreferenceManager.setDefaultValues(this, R.xml.advanced_preferences, false); PreferenceManager.setDefaultValues(this, R.xml.advanced_preferences, false);

View File

@@ -21,6 +21,7 @@ import com.example.android.apis.R;
import android.os.Bundle; import android.os.Bundle;
import android.preference.PreferenceActivity; import android.preference.PreferenceActivity;
import android.preference.PreferenceFragment; import android.preference.PreferenceFragment;
import android.preference.PreferenceManager;
import android.util.Log; import android.util.Log;
import android.widget.Button; import android.widget.Button;
@@ -60,6 +61,12 @@ public class PreferenceWithHeaders extends PreferenceActivity {
public void onCreate(Bundle savedInstanceState) { public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState); super.onCreate(savedInstanceState);
// Make sure default values are applied. In a real app, you would
// want this in a shared function that is used to retrieve the
// SharedPreferences wherever they are needed.
PreferenceManager.setDefaultValues(getActivity(),
R.xml.advanced_preferences, false);
// Load the preferences from an XML resource // Load the preferences from an XML resource
addPreferencesFromResource(R.xml.fragmented_preferences); addPreferencesFromResource(R.xml.fragmented_preferences);
} }

View File

@@ -0,0 +1,38 @@
/*
* Copyright (C) 2007 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.example.android.apis.preference;
import com.example.android.apis.R;
import android.os.Bundle;
import android.preference.PreferenceActivity;
import android.preference.PreferenceManager;
public class SwitchPreference extends PreferenceActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
PreferenceManager.setDefaultValues(this, "switch", MODE_PRIVATE,
R.xml.default_values, false);
// Load the preferences from an XML resource
getPreferenceManager().setSharedPreferencesName("switch");
addPreferencesFromResource(R.xml.preference_switch);
}
}