I have a OnPreferenceClickListener which should remove a specific Preference (with key preference_to_remove) from a PreferenceScreen.
The problem is that my solution works when preference_to_remove is not located inside a nested PreferenceScreen but does not work, when it is inside a nested screen AND the screen orientation changes. Before screen orientation changes, nested screens are working as expected, too.
The following code contains two version, one with a flat non-nested PreferenceScreen and the broken nested PreferenceScreen.
What is the reason that the nested version is not able to remove the Preference with key preference_to_remove after screen orientation changes? What would be a solution besides using only flat PreferenceScreens and Intents to start new PreferenceScreens as pseudo children?
PS: I am using PreferenceActivity for FroYo compatibility.
How to reproduce with Test-App
Open App → Click Flat-Button → Click preference_to_click which should remove preference_to_remove. → Orientation change → Click preference_to_click to remove preference_to_remove again. Preference removed? Success!
Open App → Click Subscreen-Button → Click Test → Now repeat the steps from the first test, but this time preference_to_remove will be not not removable after orientation changing.
Download App (Source)
pref_flat.xml
<?xml version="1.0" encoding="utf-8"?>
<PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android" >
<Preference
android:key="preference_to_click"
android:persistent="false"
android:title="preference_to_click" />
<Preference
android:key="preference_to_remove"
android:title="preference_to_remove" />
</PreferenceScreen>
pref_subscreen.xml (Nested PreferenceScreen)
<?xml version="1.0" encoding="utf-8"?>
<PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android" >
<PreferenceScreen
android:key="subscreen"
android:persistent="false"
android:title="Test" >
<Preference
android:key="preference_to_click"
android:persistent="false"
android:title="preference_to_click" />
<Preference
android:key="preference_to_remove"
android:title="preference_to_remove" />
</PreferenceScreen>
</PreferenceScreen>
Diff of PrefFlatActivity.java and PrefSubscreenActivity.java
1c1
< public class PrefFlatActivity extends PreferenceActivity {
---
> public class PrefSubscreenActivity extends PreferenceActivity {
5,6c5,7
< public static final String PREFERENCE_TO_CLICK = "preference_to_click";
< public static final String PREFERENCE_TO_REMOVE = "preference_to_remove";
---
> private static final String PREFERENCE_TO_CLICK = PrefFlatActivity.PREFERENCE_TO_CLICK;
> private static final String PREFERENCE_TO_REMOVE = PrefFlatActivity.PREFERENCE_TO_REMOVE;
> private static final String PREFERENCE_SUBSCREEN = "subscreen";
15c16
< addPreferencesFromResource(R.xml.pref_flat);
---
> addPreferencesFromResource(R.xml.pref_subscreen);
28c29
< PreferenceScreen screen = getPreferenceScreen();
---
> PreferenceScreen screen = (PreferenceScreen) findPreference(PREFERENCE_SUBSCREEN);
PrefFlatActivity.java (Working)
/**
* Works as expected. Clicking toggles the "visibility" of the PREFERENCE_TO_REMOVE Preference.
*/
public class PrefFlatActivity extends PreferenceActivity {
/**
* Preference keys.
*/
public static final String PREFERENCE_TO_CLICK = "preference_to_click";
public static final String PREFERENCE_TO_REMOVE = "preference_to_remove";
private final String PREF_NAME = getClass().getName() + ".pref";
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
getPreferenceManager().setSharedPreferencesName(PREF_NAME);
addPreferencesFromResource(R.xml.pref_flat);
findPreference(PREFERENCE_TO_CLICK)
.setOnPreferenceClickListener(new OnFlatClickListener());
}
/**
* Removes or adds Preference with key PREFERENCE_TO_REMOVE when clicked.
*/
private class OnFlatClickListener implements OnPreferenceClickListener {
private Preference mRescuedPreference;
public boolean onPreferenceClick(Preference preference) {
PreferenceScreen screen = getPreferenceScreen();
Preference prefToRemove = screen.findPreference(PREFERENCE_TO_REMOVE);
Log.d("test", "Found PREFERENCE_TO_REMOVE: " + (prefToRemove != null));
if (prefToRemove != null) {
screen.removePreference(prefToRemove);
mRescuedPreference = prefToRemove; // Rescue reference to re-add it later.
}
else {
screen.addPreference(mRescuedPreference);
}
return true;
}
}
}
PrefSubscreenActivity.java (Nested, broken after orientation change)
/**
* Broken after orientation change. Clicking does not remove/add PREFERENCE_TO_REMOVE.
*/
public class PrefSubscreenActivity extends PreferenceActivity {
/**
* Preference keys.
*/
private static final String PREFERENCE_TO_CLICK = PrefFlatActivity.PREFERENCE_TO_CLICK;
private static final String PREFERENCE_TO_REMOVE = PrefFlatActivity.PREFERENCE_TO_REMOVE;
private static final String PREFERENCE_SUBSCREEN = "subscreen";
private final String PREF_NAME = getClass().getName() + ".pref";
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
getPreferenceManager().setSharedPreferencesName(PREF_NAME);
addPreferencesFromResource(R.xml.pref_subscreen);
findPreference(PREFERENCE_TO_CLICK)
.setOnPreferenceClickListener(new OnFlatClickListener());
}
/**
* Removes or adds Preference with key PREFERENCE_TO_REMOVE when clicked.
*/
private class OnFlatClickListener implements OnPreferenceClickListener {
private Preference mRescuedPreference;
public boolean onPreferenceClick(Preference preference) {
PreferenceScreen screen = (PreferenceScreen) findPreference(PREFERENCE_SUBSCREEN);
Preference prefToRemove = screen.findPreference(PREFERENCE_TO_REMOVE);
Log.d("test", "Found PREFERENCE_TO_REMOVE: " + (prefToRemove != null));
if (prefToRemove != null) {
screen.removePreference(prefToRemove);
mRescuedPreference = prefToRemove; // Rescue reference to re-add it later.
}
else {
screen.addPreference(mRescuedPreference);
}
return true;
}
}
}
edit: I've been having trouble getting this to work as you requested. I read through the source for Preferences and how they handle state without any luck, so I'm probably missing something obvious (isn't that how it often goes?)
I did some testing, and I believe that the state of the PreferenceScreen and/or the Dialog it uses to display the nested PreferenceScreen is behaving in a way that neither of us expects. This behavior is not observed when using a PreferenceCategory instead of a PreferenceScreen, for example.
Since this way of working with Preferences is deprecated, you can always try using fragments:
http://developer.android.com/reference/android/preference/PreferenceActivity.html
However, I suspect you are avoiding this for legacy reasons.
Another option would be to create your Preference programmatically:
Building Preference screen in code depending on another setting
Or, you could handle configuration changes yourself:
http://developer.android.com/reference/android/app/Activity.html#ConfigurationChanges
https://stackoverflow.com/questions/3542333/how-to-prevent-custom-views-from-losing-state-across-screen-orientation-changes/8127813#8127813
<activity ..
android:configChanges="orientation|keyboardHidden" .. >
Hopefully you get this figured out soon. I'm sure the solution will pop out at us eventually; it can't be that hard! Here's hoping ;)
Original source (mostly) retained below:
package com.example.removepref;
import android.preference.Preference;
import android.preference.Preference.OnPreferenceClickListener;
import android.preference.PreferenceActivity;
import android.preference.PreferenceScreen;
import android.util.Log;
public class PrefSubscreenActivity extends PreferenceActivity {
/**
* Preference keys.
*/
private static final String PREFERENCE_TO_CLICK = PrefFlatActivity.PREFERENCE_TO_CLICK;
private static final String PREFERENCE_TO_REMOVE = PrefFlatActivity.PREFERENCE_TO_REMOVE;
private static final String PREFERENCE_SUBSCREEN = "subscreen";
private final String PREF_NAME = getClass().getName() + ".pref";
#Override
protected void onResume() {
super.onResume();
getPreferenceManager().setSharedPreferencesName(PREF_NAME);
addPreferencesFromResource(R.xml.pref_subscreen);
findPreference(PREFERENCE_TO_CLICK).setOnPreferenceClickListener(new OnFlatClickListener());
}
/**
* Removes or adds Preference with key PREFERENCE_TO_REMOVE when clicked.
*/
private class OnFlatClickListener implements OnPreferenceClickListener {
private Preference mRescuedPreference;
private boolean mPrefToRemoveVisible = true;
public boolean onPreferenceClick(Preference preference) {
// toggle visibility
mPrefToRemoveVisible = !mPrefToRemoveVisible;
PreferenceScreen screen = (PreferenceScreen) findPreference(PREFERENCE_SUBSCREEN);
Preference prefToRemove = screen.findPreference(PREFERENCE_TO_REMOVE);
Log.d("test", "Found PREFERENCE_TO_REMOVE: " + (prefToRemove != null));
Log.d("test", "And noted mPrefToRemoveVisible: " + mPrefToRemoveVisible);
//Replaced the conditional blocks:
if (mPrefToRemoveVisible && null == prefToRemove) {
boolean added = screen.addPreference(mRescuedPreference);
Log.d("test", "screen.addPreference(mRescuedPreference) success?" + added);
} else if (!mPrefToRemoveVisible && null != prefToRemove) {
mRescuedPreference = prefToRemove;
boolean removed = screen.removePreference(prefToRemove);
Log.d("test", "screen.removePreference(mRescuedPreference) success?" + removed);
}
return true;
}
}
}
Recommended Reading: http://developer.android.com/guide/topics/resources/runtime-changes.html
Related
Here is some of the code in my custom-made public class Preferences that stores and retrieves values correctly in SharedPreferences:
public static final String DIR_SP_FILTERS = "filters";
public static final String KEY_SP_FILTERS_LIST = "flist";
public static final String DEFAULT_FILTERS = "1111110";
private static SharedPreferences f_sP;
public static void setUpPrefs(Context context, MapDisplay myMap) {
// Load filter preferences
f_sP = context.getSharedPreferences(DIR_SP_FILTERS, Context.MODE_PRIVATE);
String masterFilters = f_sP.getString(KEY_SP_FILTERS_LIST, "");
Menu menu = MainActivity.menu;
if(masterFilters.length() != 0) {
for(int i = 0; i < masterFilters.length(); i++) {
int value = Integer.parseInt(masterFilters.charAt(i) + "");
// Dans l'ordre du menu!
switch(i) {
case 0: // Feu
MapDisplay.feuFilter = filterTypeBtn(value, menu.findItem(R.id.cB_fire));
case 1: // Eau
MapDisplay.eauFilter = filterTypeBtn(value, menu.findItem(R.id.cB_water));
case 2: // Terrain
MapDisplay.terrainFilter = filterTypeBtn(value, menu.findItem(R.id.cB_terrain));
case 3: // Météo
MapDisplay.meteoFilter = filterTypeBtn(value, menu.findItem(R.id.cB_meteo));
case 4: // MZ
MapDisplay.showMonitoredZones = filterTypeBtn(value, menu.findItem(R.id.cB_zones));
case 5: // UserPins
MapDisplay.showUserPins = filterTypeBtn(value, menu.findItem(R.id.cB_users));
case 6: // Histo
MapDisplay.historiqueFilter = filterTypeBtn(value, menu.findItem(R.id.cB_histo));
}
}
} else {
// If first time launching app: initialize default filter string
SharedPreferences.Editor editor = f_sP.edit();
editor.putString(KEY_SP_FILTERS_LIST, DEFAULT_FILTERS);
editor.apply();
}
}
The most important part of the code here, for you guys, I believe are the constants declared at the beginning, which represent the names of the folders and sub-folders where I am saving information.
The set up SharedPreferences are working fine and I made sure that the specified values can be retrieved after the application is closed, and that modification from user input are saved properly (I won't show these methods to keep the thing simple).
I then proceeded to get AndroidStudio to automatically generate a SettingsActivity for me and wanted to add the possibility for the user to at least see the content of this String he is saving in memory ("1111110").
I thus added this to my SettingsActivity.java:
#TargetApi(Build.VERSION_CODES.HONEYCOMB)
public static class FiltersPreferenceFragment extends PreferenceFragment {
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
addPreferencesFromResource(R.xml.pref_monitored_zones);
setHasOptionsMenu(true);
// Bind the summaries of EditText/List/Dialog/Ringtone preferences
// to their values. When their values change, their summaries are
// updated to reflect the new value, per the Android Design
// guidelines.
bindPreferenceSummaryToValue(findPreference(Preferences.DIR_SP_FILTERS));
bindPreferenceSummaryToValue(findPreference(Preferences.KEY_SP_FILTERS_LIST)););
}
// ...
}
And made sure to add my Fragment in this list:
protected boolean isValidFragment(String fragmentName) {
return PreferenceFragment.class.getName().equals(fragmentName)
|| GeneralPreferenceFragment.class.getName().equals(fragmentName)
|| DataSyncPreferenceFragment.class.getName().equals(fragmentName)
|| NotificationPreferenceFragment.class.getName().equals(fragmentName)
|| FiltersPreferenceFragment.class.getName().equals(fragmentName); // here
}
Notice those two important lines which I believe is my culprit:
bindPreferenceSummaryToValue(findPreference(Preferences.DIR_SP_FILTERS));
bindPreferenceSummaryToValue(findPreference(Preferences.KEY_SP_FILTERS_LIST)););
They are using this method which was already defined when I generated the Activity:
private static void bindPreferenceSummaryToValue(Preference preference) {
// Set the listener to watch for value changes.
preference.setOnPreferenceChangeListener(sBindPreferenceSummaryToValueListener);
// Trigger the listener immediately with the preference's
// current value.
sBindPreferenceSummaryToValueListener.onPreferenceChange(preference,
PreferenceManager
.getDefaultSharedPreferences(preference.getContext())
.getString(preference.getKey(), ""));
}
I did add my Fragment into the pref_headers.xml file, and created my pref_filters.xml (mostly copy-pasting from another pref_*.xml since I am still unsure how to deal with this):
<SwitchPreference
android:defaultValue="true"
android:key="example_switch"
android:summary="#string/pref_description_social_recommendations"
android:title="#string/pref_title_social_recommendations" />
<!-- NOTE: EditTextPreference accepts EditText attributes. -->
<!-- NOTE: EditTextPreference's summary should be set to its value by the activity code. -->
<EditTextPreference
android:capitalize="words"
android:defaultValue="#string/pref_default_display_name"
android:inputType="textCapWords"
android:key="filters"
android:maxLines="1"
android:selectAllOnFocus="true"
android:singleLine="true"
android:title="#string/pref_title_display_name" />
<!-- NOTE: Hide buttons to simplify the UI. Users can touch outside the dialog to
dismiss it. -->
<!-- NOTE: ListPreference's summary should be set to its value by the activity code. -->
<ListPreference
android:defaultValue="-1"
android:entries="#array/pref_example_list_titles"
android:entryValues="#array/pref_example_list_values"
android:key="example_list"
android:negativeButtonText="#null"
android:positiveButtonText="#null"
android:title="#string/pref_title_add_friends_to_messages" />
My understanding is that the key to making this all work is to use the android:key="filters" properly by making it point to the proper folder or sub-folder as listed in the two lines that I showed earlier (bindPreferenceSummaryToValue(findPreference(Preferences.DIR_SP_FILTERS));), but I'm unsure what value to use there to get what I want.
Thanks for helping ! :)
Because I want an AppCompat Action Bar on all of my settings submenus, I had to implement a workaround and my Settings Activity extends AppCompatActivity, not PreferenceActivity. I'm using a PreferenceFragment in the activity to handle the preferences, and each PreferenceScreen has its own xml file, which the PreferenceFragment switches out for each submenu in the settings. All of this was necessary to get the Action Bar to stay put through all of my submenus.
I'm trying to read a string value from the shared preferences file from within my MainActivity, and I've tried three different methods for getting that information, none of which have worked:
SharedPreferences sharedPref = PreferenceManager.getDefaultSharedPreferences(this);
String btSelectPref = sharedPref.getString(getString(R.string.bt_select_key), "");
,
SharedPreferences sharedPref = getSharedPreferences(name, MODE_PRIVATE);
String btSelectPref = sharedPref.getString(getString(R.string.bt_select_key), "");
and
SharedPreferences sharedPref = getPreferences(MODE_PRIVATE);
String btSelectPref = sharedPref.getString(getString(R.string.bt_select_key), "");
Here is the relevant section of my preferences.xml:
<PreferenceCategory
android:title="Bluetooth"
android:key="pref_bt">
<Preference
android:title="Select Bluetooth Device"
android:key="#string/bt_select_key"
android:defaultValue="0">
</Preference>
</PreferenceCategory>
This should fill the btSelectPref string with a "0", but it's always empty when I test it. I have included PreferenceManager.setDefaultValues(this, R.xml.preferences, false); in onCreate in my MainActivity, so the default values should be set.
I'm not sure which of these methods I should be using since I have multiple resource files for my settings, but none of them seem to be working for me. In the case of getSharedPreferences(name, MODE_PRIVATE), I have no idea what the name parameter should be referencing, since I've never named my shared preferences file.
EDIT: It turns out my issue was not related to getting values from the shared preferences file. I just had the wrong xml tag on the preference I was trying to check the value of. I changed it from a generic <Preference> tag to a <ListPreference> and my code started working with PreferenceManager.getDefaultSharedPreferences().
What you want to do and what you are doing differs. If you just want to put default shared preference for a key then consider this example. If your whole activity has just one shared pref file then you need not specify any name. It will automatically get it.
public MainActivity extends AppCompatActivity {
SharedPreferences mPrefs;
int test;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_counter);
mPrefs = this.getPreferences(Context.MODE_PRIVATE);
test = mPrefs.getInt("pref_bt_select", 0);}
}
For the above example you can define the key and default value in your strings.xml and then you can refer to it while looking for the prefs you want.
Hey I have used AppCompat for my preference screen too.I did this because I wanted to use Vintage Chroma and this was the only way. But I am able to use PreferenceManager.getDefaultSharedPreference() without any errors.
Also if you want to use default shared preferences in the Fragment you can use :
SharedPreferences preferences = this.getActivity().getSharedPreferences("pref", Context.MODE_PRIVATE);
Here is my full code :
public class PreferencesActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
getFragmentManager()
.beginTransaction()
.replace(android.R.id.content, new PreferencesScreen())
.commit();
ActionBar toolbar = getSupportActionBar();
if (toolbar != null) {
toolbar.setDisplayHomeAsUpEnabled(true);
}
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
if (item.getItemId() == android.R.id.home) {
onBackPressed();
}
return super.onOptionsItemSelected(item);
}
public static class PreferencesScreen extends PreferenceFragment {
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
addPreferencesFromResource(R.xml.settings_xml);
}
}
}
Here is my code snippet for MAinActivity
Just the initial part where I set the default text theme.
`public class MainActivity extends AppCompatActivity
implements NavigationView.OnNavigationItemSelectedListener {
AutoCompleteTextView mytextview;
public static String[] list;
ArrayList<String> recent = new ArrayList<String>();
public int recent_index = 0;
Menu mMenu;
#Override
protected void onCreate(Bundle savedInstanceState) {
/*Setting default theme.*/
SharedPreferences Sp= PreferenceManager.getDefaultSharedPreferences(getApplicationContext());
int firstRun=Sp.getInt("firstRun",0);
if(firstRun==0)
{
SharedPreferences.Editor editor=Sp.edit();
editor.putInt("paragraphFontColor", Color.parseColor("#ffffff"));
editor.putInt("headingFontColor",Color.parseColor("#DE5246"));
editor.putInt("subheadingFontColor",Color.parseColor("#597d5e"));
editor.putInt("hyperlinksFontColor",Color.parseColor("#A5D8F5"));
editor.putInt("bodyColor",Color.parseColor("#2b2b2b"));
editor.putString("paragraphFont","PrintClearly.otf");
editor.putString("headingFont","PrintBold.otf");
editor.putString("subheadingFont","PrintBold.otf");
editor.putString("hyperlinkFont","PrintBold.otf");
editor.putString("paragraphFontStyle","normal");
editor.putString("headingFontStyle","normal");
editor.putString("subheadingFontStyle","normal");
editor.putString("hyperlinkFontStyle","normal");
editor.putString("actionBarColor","#597d5e");
editor.putString("paragraphFontSize","20px");
editor.putString("headingFontSize","30px");
editor.putString("subheadingFontSize","20px");
editor.putString("hyperlinkFontSize","20px");
editor.putString("firstRun",0);
editor.commit();
}
`
The answer is at the bottom.
I've added a modification to the 4.0.4 browser which adds a new preferences section. When the browser is launched, the value is read in correctly and seems to get cached. I added a log event any time getMaxTabs is used by Controller.java and it's only read on first launch. If I change the setting nothing happens. But if I kill the browser and re-launch after changing the setting, the new value is read in correctly. Is there a way to force this setting to be re-cached on change?
I am looking at SharedPreferences.OnSharedPreferenceChangeListener but I don't see HOW to have that variable re-read forcibly.
int getMaxTabs() {
Context mContext = mActivity.getApplicationContext();
SharedPreferences mPrefs = PreferenceManager.getDefaultSharedPreferences(mContext);
String numTabs = mPrefs.getString(PreferenceKeys.PREF_MAX_TABS, "20");
int t = Integer.parseInt(numTabs);
Log.i("max open tabs", "max: " + t);
return t;
// The original content of getMaxTabs is below
// return mActivity.getResources().getInteger(R.integer.max_tabs);
}
So the log item only appears the first time the browser is launched. The new preference is set because if I change it and kill/relaunch the browser, the new value is reflected. I need to either not cache this setting or force the cache to be refreshed on change, which would be more efficient.
src/com/android/browser/preferences/TabsPreferencesFragment.java
package com.android.browser.preferences;
import android.os.Bundle;
import android.preference.Preference;
import android.preference.PreferenceFragment;
import com.android.browser.BrowserSettings;
import com.android.browser.PreferenceKeys;
import com.android.browser.R;
import com.android.browser.search.SearchEngine;
public class TabsPreferencesFragment extends PreferenceFragment {
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// Load the XML preferences file
addPreferencesFromResource(R.xml.tabs_preferences);
}
}
res/xml/tabs_preferences.xml
<?xml version="1.0" encoding="utf-8"?>
<PreferenceScreen
xmlns:android="http://schemas.android.com/apk/res/android" >
<ListPreference
android:key="max_open_tabs"
android:defaultValue="20"
android:title="#string/pref_max_open_tabs"
android:entries="#array/pref_max_open_tabs_choices"
android:entryValues="#array/pref_max_open_tabs_values"
/>
</PreferenceScreen>
This is how it is called in src/com/android/browser/TabControl.java
private int mMaxTabs;
TabControl(Controller controller) {
mController = controller;
mMaxTabs = mController.getMaxTabs();
mTabs = new ArrayList<Tab>(mMaxTabs);
mTabQueue = new ArrayList<Tab>(mMaxTabs);
}
boolean canCreateNewTab() {
return mMaxTabs > mTabs.size();
}
SOLUTION
The answer lies here, in packages/apps/Browser/src/com/android/browser/TabControl.java
Around line 55 we see:
/**
* Construct a new TabControl object
*/
TabControl(Controller controller) {
mController = controller;
mMaxTabs = mController.getMaxTabs();
mTabs = new ArrayList<Tab>(mMaxTabs);
mTabQueue = new ArrayList<Tab>(mMaxTabs);
}
The TabControl object is constructed one time at browser launch. Here we define mMaxTabs by calling mController.getMaxTabs().
Around line 155 canCreateNewTab is defined, using mMaxTabs.
boolean canCreateNewTab() {
return mMaxTabs > mTabs.size();
}
Since mMaxTabs is a static value it will never change on preference update.
The solution is this.
/**
* Construct a new TabControl object
*/
TabControl(Controller controller) {
mController = controller;
mTabs = new ArrayList<Tab>(mMaxTabs);
mTabQueue = new ArrayList<Tab>(mMaxTabs);
}
boolean canCreateNewTab() {
return mController.getMaxTabs() > mTabs.size();
}
Now every time a new browser tab is requested, the pref setting is verified.
I am working with a PreferenceActivity that will be fully compatible with tablets.
For this, I will work as advised by Google in this page.
#Override
public void onBuildHeaders(List<Header> target) {
loadHeadersFromResource(R.xml.preference_headers, target);
}
My problem is that I would like to be able to select the default header when the activity is launched.
For instance, I have several headers;
General Settings
UI Settings
Network settings
And depending on which activity I come from, I would like to display the correct settings.
Is there a way to achieve that?
When creating the Intent to invoke the PreferenceActivity, you can add the extra string 'EXTRA_SHOW_FRAGMENT' to specify which fragment should be initially displayed. You pass the name of the fragment you would like to select.
For instance, if you would like to select the General Settings header (and its contents) you can use the following code:
final Intent intent = new Intent(this, ExtendedPreferenceActivity.class); // Assume we call it from an other activty
intent.putExtra(EXTRA_SHOW_FRAGMENT, GeneralSettingsFragment.class.getName());
startActivity(intent);
More information on this can be found here: http://developer.android.com/reference/android/preference/PreferenceActivity.html
In an issue report to Google it is reported that for Android version 3.0 the correct header will not be automatically selected as well. For the issue report and its workaround look here: issue report.
You can create PreferenceHeaders dynamically using PreferenceActivity.Header class
http://developer.android.com/reference/android/preference/PreferenceActivity.Header.html
You can use a fragment by default:
Here is what I've done:
public class PreferencesActivity extends SherlockPreferenceActivity {
/** Variables **/
/** Constants **/
private static final String CLASSTAG = PreferencesActivity.class.getSimpleName();
/** Class Methods **/
#Override
public void onCreate(Bundle savedInstanceState) {
Log.v(CLASSTAG, "onCreate");
super.onCreate(savedInstanceState);
initializeUI();
}
#Override
public Intent getIntent() {
Log.v(CLASSTAG, "getIntent");
final Intent modIntent = new Intent(super.getIntent());
modIntent.putExtra(EXTRA_SHOW_FRAGMENT, SettingsFragment.class.getName());
modIntent.putExtra(EXTRA_NO_HEADERS, true);
return modIntent;
}
/** Private Functions **/
private void initializeUI() {
getSupportActionBar().hide();
}
/** Classes **/
public static class SettingsFragment extends PreferenceFragment {
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// Load the preferences from an XML resource
addPreferencesFromResource(R.xml.settings_preference);
initializeUI();
}
private void initializeUI() { }
}
}
and the default xml (as prior HoneyComb versions...):
<PreferenceScreen
xmlns:android="http://schemas.android.com/apk/res/android">
<PreferenceCategory
android:title="#string/preferences_category_1">
<com.taptime.ui.preferences.ClickPreference
android:key="#string/preferences_conditions_key"
android:title="#string/preferences_conditions_title"/>
</PreferenceCategory>
<PreferenceCategory
android:title="#string/preferences_category_2">
<com.newin.android.ui.widget.ClickPreference
android:key="#string/preferences_logout_key"
android:title="#string/preferences_logout_title"
android:summary="#string/preferences_logout_summary"/>
</PreferenceCategory>
</PreferenceScreen>
New to Android, I have some code when the user changes a preference I update the Summary field in the UI preference to be the value they entered. However, when the preference activity is created I'd like to set the Summary fields to be the values in the corresponding preferences.
Please advise. Thanks.
public class MyPreferenceActivity extends PreferenceActivity implements
OnSharedPreferenceChangeListener {
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
addPreferencesFromResource(R.xml.preference);
SharedPreferences sp = getPreferenceScreen().getSharedPreferences();
sp.registerOnSharedPreferenceChangeListener(this);
}
public void onSharedPreferenceChanged(SharedPreferences sharedPreferences,
String key) {
Preference pref = findPreference(key);
if (pref instanceof EditTextPreference) {
EditTextPreference etp = (EditTextPreference) pref;
pref.setSummary(etp.getText());
}
}
}
I am new too so may not be the best code but this is similar to what I am doing. You probably want to register you listener onResume and unregister it onPause though rather than onCreate. I hope this helps.
Mainly you just need to grab the pref, the pref value and set the summary.
public class MyPreferenceActivity extends PreferenceActivity implements
OnSharedPreferenceChangeListener {
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
addPreferencesFromResource(R.xml.preference);
SharedPreferences sp = getPreferenceScreen().getSharedPreferences();
EditTextPreference editTextPref = (EditTextPreference) findPreference("thePrefKey");
editTextPref
.setSummary(sp.getString("thePrefKey", "Some Default Text"));
}
protected void onResume() {
super.onResume();
getPreferenceScreen().getSharedPreferences()
.registerOnSharedPreferenceChangeListener(this);
}
protected void onPause() {
super.onPause();
getPreferenceScreen().getSharedPreferences()
.unregisterOnSharedPreferenceChangeListener(this);
}
public void onSharedPreferenceChanged(SharedPreferences sharedPreferences,
String key) {
Preference pref = findPreference(key);
if (pref instanceof EditTextPreference) {
EditTextPreference etp = (EditTextPreference) pref;
pref.setSummary(etp.getText());
}
}
}
This worked for me.
public class PrefsActivity extends PreferenceActivity implements OnSharedPreferenceChangeListener
{
private Preference pref;
private String summaryStr;
String prefixStr;
#Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
addPreferencesFromResource(R.xml.prefs);
SharedPreferences sharedPref = getPreferenceScreen().getSharedPreferences();
sharedPref.registerOnSharedPreferenceChangeListener(this);
}
public void onSharedPreferenceChanged(SharedPreferences sharedPreferences, String key)
{
//Get the current summary
pref = findPreference(key);
summaryStr = (String)pref.getSummary();
//Get the user input data
prefixStr = sharedPreferences.getString(key, "");
//Update the summary with user input data
pref.setSummary(summaryStr.concat(": [").concat(prefixStr).concat("]"));
}
}
What I usually do is:
1 - Make a new class which extends the kind of preference I need to show (1 per preference type)
2 - Inside its code, do the appropriate actiob to show the updated summary
3 - Refer this class in the res/xml/preferences.xml file
Let me swo a small example, good for an EditTextPreference:
CLS_Prefs_Edit.java
/**
* CLS_Prefs_Edit class
*
* This is the class that allows for a custom EditTextPrefence
* (auto refresh summary).
*
* #category Custom Preference
* #author Luca Crisi (luca.crisi.lc#gmail.com)
* #copyright Luca Crisi
* #version 1.0
*/
package com.your_name.your_app;
/* -------------------------------- Imports --------------------------------- */
import android.content.Context;
import android.preference.EditTextPreference;
import android.util.AttributeSet;
public final class CLS_Prefs_Edit
extends EditTextPreference
{
/* ---------------------------- Constructors ---------------------------- */
public CLS_Prefs_Edit(final Context ctx, final AttributeSet attrs)
{
super(ctx, attrs);
}
public CLS_Prefs_Edit(final Context ctx)
{
super(ctx);
}
/* ----------------------------- Overrides ------------------------------ */
#Override
public void setText(final String value)
{
super.setText(value);
setSummary(getText());
}
}
res/xml/preferences.xml
<?xml version="1.0" encoding="utf-8"?>
<PreferenceScreen
xmlns:android="http://schemas.android.com/apk/res/android"
>
<PreferenceCategory android:title="#string/pref_phone_cat">
<!-- NORMAL EditTextPreference, NO summary update -->
<!-- <EditTextPreference -->
<!-- android:widgetLayout="#layout/arr_dn" -->
<!-- android:key="phone" -->
<!-- android:title="#string/pref_phone_title" -->
<!-- android:summary="#string/pref_phone_summ" -->
<!-- android:defaultValue="" -->
<!-- android:inputType="phone" -->
<!-- android:digits="+1234567890" -->
<!-- /> -->
<!-- MY EditTextPreference, WITH summary update -->
<com.your_name.your_app.CLS_Prefs_Edit
android:widgetLayout="#layout/arr_dn"
android:key="phone"
android:title="#string/pref_phone_title"
android:summary="#string/pref_phone_summ"
android:defaultValue=""
android:inputType="phone"
android:digits="+1234567890"
/>
</PreferenceCategory>
</PreferenceScreen>
Of course, set your strings in /res/values/strings, and you're done.
Note that this solution works for both PreferenceFragments and PreferenceActivities.
I'm using it for an app tha runs on 2.2 Froyo (showing a PreferenceActivity) as well as on 4.4 KitKat (showing a PreferenceFragment)
I hope it helps.
First, for your class:
class SettingsFragment : SettingsBaseFragment(),
// Make sure you implement the OnSharedPreferenceChangeListener
SharedPreferences.OnSharedPreferenceChangeListener,
Preference.OnPreferenceClickListener {}
Next, in the onSharedPreferenceChanged, you have a reference to the sharedPreference, which you can use to get the preference result. Then you can use the latest result you get to alter the summary:
override fun onSharedPreferenceChanged(sharedPreferences: SharedPreferences?, key: String?) {
when (key) {
SETTINGS_AUTO_SETTLEMENT -> refreshSummaryForAutoSettlement(sharedPreferences?.getBoolean(SETTINGS_AUTO_SETTLEMENT, false))
}
}
// helper function for altering the summary for the preference
private fun refreshSummaryForAutoSettlement(isAutoSettlementEnabled: Boolean?) {
when (isAutoSettlementEnabled != null && isAutoSettlementEnabled) {
true -> findPreference(SETTINGS_AUTO_SETTLEMENT).summary = getString(R.string.auto_settlement_enabled_summary)
false -> findPreference(SETTINGS_AUTO_SETTLEMENT).summary = getString(R.string.auto_settlement_disabled_summary)
}
}