I'm trying to accomplish something that I think is quite easy, but I'm baffled on how to accomplish it using the PreferenceActivity class and the xml preference-header tag.
When the user taps the app settings icon I would like them to be presented with a small list of checkboxes and lists. I have only a single preference fragment. So far I can set up the PreferenceActivity xml files (preferences.xml, and preference fragments xmls) to show the single header for the preference fragment. When the user selects that header the preferences screen is exactly how I want it, however, I cannot figure out a way to skip showing the first header screen.
It seems a waste to have to tap the settings/preferences icon, show a single header that is then tapped to get to the actual settings/preferences.
I understand how this can be very helpful if you want to categorize your preferences, but for something simple, this adds overhead and seems rather clunky.
I hope that I was clear. In short here's my question:
What is the new preferred way to use the PreferenceActivity class and preference-header tag to simply show a single screen of options with no header?
After looking around some more it looks like I am trying to do what the older methods did in a straight-forward manner. I'm trying to do this with without using any of the deprecated functions.
Thanks in advance, B.
New preferred way is to show a single PreferenceFragment
as the main content of any activity. It doesn't need to be PreferenceActivity
. See the APIs demo sample
public class FragmentPreferences extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// Display the fragment as the main content.
getFragmentManager().beginTransaction().replace(android.R.id.content,
new PrefsFragment()).commit();
}
public static class PrefsFragment extends PreferenceFragment {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// Load the preferences from an XML resource
addPreferencesFromResource(R.xml.preferences);
}
}
}
I was looking for an answer that matched this question. Eventually, I worked it out from several other sources. For those that may still want it answered, here's what worked for me. (Note - both min and target SDKs are set to 15 in this project.)
The preference class that displays the single settings screen.
public class SettingsActivity extends PreferenceActivity {
private final static String TAG = "SettingsAcitivity";
public SettingsActivity() {}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
MyLog.d(TAG, "onCreate");
getFragmentManager().beginTransaction()
.replace(android.R.id.content, new LocationFragment()).commit();
}
public class LocationFragment extends PreferenceFragment {
private final static String TAG = "LocationFragment";
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
MyLog.d(TAG, "onCreate");
addPreferencesFromResource(R.xml.locationsettings);
}
}
}
The code to display the Settings:
@Override
public boolean onOptionsItemSelected(MenuItem item) {
MyLog.d(TAG, "onOptionsItemSelected");
switch (item.getItemId()) {
case R.id.menu_main_help:
break;
case R.id.menu_main_about:
break;
case R.id.menu_main_settings:
MyLog.d(TAG, "Settings");
Intent settingsIntent = new Intent(this, SettingsActivity.class);
startActivity(settingsIntent);
break;
}
return true;
}
The Back key terminates the SettingsActivity. The built in preference routines save any changes. The onResume function I have does a getSettings() that updates any changed settings used by the calling activity (MainActivity in this case).
That's it.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With