Add new sample for multi-pane PreferenceActivity.

Also re-arrange all preference API demos to go in their own
package, since the preferfence APIs themselves are in their own package.

Change-Id: I305f77dc09748bb60d1de8a23d063db64c11bb1e
This commit is contained in:
Dianne Hackborn
2010-08-02 18:23:18 -07:00
parent 95a3a10dfb
commit 721a184e0a
16 changed files with 212 additions and 115 deletions

View File

@@ -67,7 +67,7 @@ public class FragmentDialog extends Activity {
mStackLevel++;
DialogFragment newFragment = new MyDialogFragment(mStackLevel);
FragmentTransaction ft = openFragmentTransaction();
Fragment prev = this.findFragmentByTag("dialog");
Fragment prev = findFragmentByTag("dialog");
if (prev != null) {
ft.remove(prev);
}

View File

@@ -14,7 +14,7 @@
* limitations under the License.
*/
package com.example.android.apis.app;
package com.example.android.apis.preference;
import com.example.android.apis.R;
@@ -35,7 +35,7 @@ public class AdvancedPreferences extends PreferenceActivity implements OnSharedP
private CheckBoxPreference mCheckBoxPreference;
private Handler mHandler = new Handler();
/**
* This is a simple example of controlling a preference from code.
*/
@@ -44,19 +44,19 @@ public class AdvancedPreferences extends PreferenceActivity implements OnSharedP
if (mCheckBoxPreference != null) {
mCheckBoxPreference.setChecked(!mCheckBoxPreference.isChecked());
}
// Force toggle again in a second
mHandler.postDelayed(this, 1000);
}
};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// Load the XML preferences file
addPreferencesFromResource(R.xml.advanced_preferences);
// Get a reference to the checkbox preference
mCheckBoxPreference = (CheckBoxPreference)getPreferenceScreen().findPreference(
KEY_ADVANCED_CHECKBOX_PREFERENCE);
@@ -68,7 +68,7 @@ public class AdvancedPreferences extends PreferenceActivity implements OnSharedP
// Start the force toggle
mForceCheckBoxRunnable.run();
// Set up a listener whenever a key changes
getPreferenceScreen().getSharedPreferences().registerOnSharedPreferenceChangeListener(this);
}
@@ -79,7 +79,7 @@ public class AdvancedPreferences extends PreferenceActivity implements OnSharedP
// Unregister the listener whenever a key changes
getPreferenceScreen().getSharedPreferences().unregisterOnSharedPreferenceChangeListener(this);
mHandler.removeCallbacks(mForceCheckBoxRunnable);
}
@@ -90,5 +90,5 @@ public class AdvancedPreferences extends PreferenceActivity implements OnSharedP
+ sharedPreferences.getInt(key, 0), Toast.LENGTH_SHORT).show();
}
}
}

View File

@@ -14,7 +14,7 @@
* limitations under the License.
*/
package com.example.android.apis.app;
package com.example.android.apis.preference;
import com.example.android.apis.ApiDemosApplication;
import com.example.android.apis.R;
@@ -46,7 +46,7 @@ public class DefaultValues extends PreferenceActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
addPreferencesFromResource(R.xml.default_values);
}

View File

@@ -14,26 +14,15 @@
* limitations under the License.
*/
package com.example.android.apis.app;
package com.example.android.apis.preference;
import com.example.android.apis.R;
import com.example.android.apis.Shakespeare;
import android.app.Activity;
import android.app.Fragment;
import android.app.ListFragment;
import android.content.Context;
import android.content.Intent;
import android.content.res.TypedArray;
import android.net.Uri;
import android.os.Bundle;
import android.preference.CheckBoxPreference;
import android.preference.EditTextPreference;
import android.preference.ListPreference;
import android.preference.PreferenceCategory;
import android.preference.PreferenceFragment;
import android.preference.PreferenceScreen;
import android.util.Log;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.ListView;
@@ -81,6 +70,7 @@ public class FragmentPreferences extends Activity {
}
}
//BEGIN_INCLUDE(fragment)
public static class Prefs1Fragment extends PreferenceFragment {
@Override
public void onCreate(Bundle savedInstanceState) {
@@ -90,6 +80,7 @@ public class FragmentPreferences extends Activity {
addPreferencesFromResource(R.xml.preferences);
}
}
//END_INCLUDE(fragment)
public static class Prefs2Fragment extends PreferenceFragment {
@Override

View File

@@ -14,7 +14,7 @@
* limitations under the License.
*/
package com.example.android.apis.app;
package com.example.android.apis.preference;
import com.example.android.apis.R;
@@ -36,9 +36,9 @@ import android.widget.LinearLayout.LayoutParams;
public class LaunchingPreferences extends Activity implements OnClickListener {
private static final int REQUEST_CODE_PREFERENCES = 1;
private TextView mCounterText;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
@@ -50,33 +50,33 @@ public class LaunchingPreferences extends Activity implements OnClickListener {
* 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);
// Simple layout
LinearLayout layout = new LinearLayout(this);
layout.setOrientation(LinearLayout.VERTICAL);
setContentView(layout);
// Create a simple button that will launch the preferences
Button launchPreferences = new Button(this);
launchPreferences.setText(getString(R.string.launch_preference_activity));
launchPreferences.setOnClickListener(this);
layout.addView(launchPreferences, new LayoutParams(LayoutParams.MATCH_PARENT,
LayoutParams.WRAP_CONTENT));
mCounterText = new TextView(this);
layout.addView(mCounterText, new LayoutParams(LayoutParams.MATCH_PARENT,
LayoutParams.WRAP_CONTENT));
updateCounterText();
}
public void onClick(View v) {
// When the button is clicked, launch an activity through this intent
Intent launchPreferencesIntent = new Intent().setClass(this, AdvancedPreferences.class);
// Make it a subactivity so we know when it returns
startActivityForResult(launchPreferencesIntent, REQUEST_CODE_PREFERENCES);
}
@@ -84,7 +84,7 @@ public class LaunchingPreferences extends Activity implements OnClickListener {
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
// The preferences returned if the request code is what we had given
// earlier in startSubActivity
if (requestCode == REQUEST_CODE_PREFERENCES) {

View File

@@ -14,7 +14,7 @@
* limitations under the License.
*/
package com.example.android.apis.app;
package com.example.android.apis.preference;
import com.example.android.apis.R;
@@ -26,7 +26,7 @@ public class PreferenceDependencies extends PreferenceActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
addPreferencesFromResource(R.xml.preference_dependencies);
}

View File

@@ -0,0 +1,58 @@
/*
* Copyright (C) 2010 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.PreferenceFragment;
import java.util.List;
/**
* Demonstration of PreferenceActivity to make a top-level preference
* panel with headers.
*/
//BEGIN_INCLUDE(activity)
public class PreferenceWithHeaders extends PreferenceActivity {
@Override
public void onBuildHeaders(List<Header> target) {
loadHeadersFromResource(R.xml.preference_headers, target);
}
public static class Prefs1Fragment extends PreferenceFragment {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// Load the preferences from an XML resource
addPreferencesFromResource(R.xml.preferences);
}
}
public static class Prefs2Fragment extends PreferenceFragment {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// Load the preferences from an XML resource
addPreferencesFromResource(R.xml.preference_dependencies);
}
}
}
//END_INCLUDE(activity)

View File

@@ -14,7 +14,7 @@
* limitations under the License.
*/
package com.example.android.apis.app;
package com.example.android.apis.preference;
import android.content.Intent;
import android.content.res.TypedArray;
@@ -34,26 +34,26 @@ public class PreferencesFromCode extends PreferenceActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setPreferenceScreen(createPreferenceHierarchy());
}
private PreferenceScreen createPreferenceHierarchy() {
// Root
PreferenceScreen root = getPreferenceManager().createPreferenceScreen(this);
// Inline preferences
// Inline preferences
PreferenceCategory inlinePrefCat = new PreferenceCategory(this);
inlinePrefCat.setTitle(R.string.inline_preferences);
root.addPreference(inlinePrefCat);
// Toggle preference
CheckBoxPreference togglePref = new CheckBoxPreference(this);
togglePref.setKey("toggle_preference");
togglePref.setTitle(R.string.title_toggle_preference);
togglePref.setSummary(R.string.summary_toggle_preference);
inlinePrefCat.addPreference(togglePref);
// Dialog based preferences
PreferenceCategory dialogBasedPrefCat = new PreferenceCategory(this);
dialogBasedPrefCat.setTitle(R.string.dialog_based_preferences);
@@ -66,7 +66,7 @@ public class PreferencesFromCode extends PreferenceActivity {
editTextPref.setTitle(R.string.title_edittext_preference);
editTextPref.setSummary(R.string.summary_edittext_preference);
dialogBasedPrefCat.addPreference(editTextPref);
// List preference
ListPreference listPref = new ListPreference(this);
listPref.setEntries(R.array.entries_list_preference);
@@ -76,7 +76,7 @@ public class PreferencesFromCode extends PreferenceActivity {
listPref.setTitle(R.string.title_list_preference);
listPref.setSummary(R.string.summary_list_preference);
dialogBasedPrefCat.addPreference(listPref);
// Launch preferences
PreferenceCategory launchPrefCat = new PreferenceCategory(this);
launchPrefCat.setTitle(R.string.launch_preferences);
@@ -93,19 +93,19 @@ public class PreferencesFromCode extends PreferenceActivity {
screenPref.setTitle(R.string.title_screen_preference);
screenPref.setSummary(R.string.summary_screen_preference);
launchPrefCat.addPreference(screenPref);
/*
* You can add more preferences to screenPref that will be shown on the
* next screen.
*/
// Example of next screen toggle preference
CheckBoxPreference nextScreenCheckBoxPref = new CheckBoxPreference(this);
nextScreenCheckBoxPref.setKey("next_screen_toggle_preference");
nextScreenCheckBoxPref.setTitle(R.string.title_next_screen_toggle_preference);
nextScreenCheckBoxPref.setSummary(R.string.summary_next_screen_toggle_preference);
screenPref.addPreference(nextScreenCheckBoxPref);
// Intent preference
PreferenceScreen intentPref = getPreferenceManager().createPreferenceScreen(this);
intentPref.setIntent(new Intent().setAction(Intent.ACTION_VIEW)
@@ -113,18 +113,18 @@ public class PreferencesFromCode extends PreferenceActivity {
intentPref.setTitle(R.string.title_intent_preference);
intentPref.setSummary(R.string.summary_intent_preference);
launchPrefCat.addPreference(intentPref);
// Preference attributes
PreferenceCategory prefAttrsCat = new PreferenceCategory(this);
prefAttrsCat.setTitle(R.string.preference_attributes);
root.addPreference(prefAttrsCat);
// Visual parent toggle preference
CheckBoxPreference parentCheckBoxPref = new CheckBoxPreference(this);
parentCheckBoxPref.setTitle(R.string.title_parent_preference);
parentCheckBoxPref.setSummary(R.string.summary_parent_preference);
prefAttrsCat.addPreference(parentCheckBoxPref);
// Visual child toggle preference
// See res/values/attrs.xml for the <declare-styleable> that defines
// TogglePrefAttrs.
@@ -137,7 +137,7 @@ public class PreferencesFromCode extends PreferenceActivity {
0));
prefAttrsCat.addPreference(childCheckBoxPref);
a.recycle();
return root;
}
}

View File

@@ -14,7 +14,7 @@
* limitations under the License.
*/
package com.example.android.apis.app;
package com.example.android.apis.preference;
import com.example.android.apis.R;
@@ -26,7 +26,7 @@ public class PreferencesFromXml extends PreferenceActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// Load the preferences from an XML resource
addPreferencesFromResource(R.xml.preferences);
}