Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Single page PreferenceActivity w/no headers/fragments?

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.

like image 806
Billbris Avatar asked Jul 08 '12 01:07

Billbris


2 Answers

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);
        }
    }

}  
like image 165
AJAY PRAKASH Avatar answered Nov 04 '22 07:11

AJAY PRAKASH


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.)

  1. Dump the PreferenceHeaders, you won't need them.
  2. Create a preference screen with the single page settings.
  3. Create a preference activity class (SettingsActivity below).
  4. Create an inline class extending PreferenceFragment (LocationFragment below).
  5. Define the class in the Manifest.
  6. Start the task - see the menu code below.

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.

like image 25
Howard Hodson Avatar answered Nov 04 '22 07:11

Howard Hodson