kolavar/android-support-v4-preferencefragment

Sample app code?

AndroidDeveloperLB opened this issue · 6 comments

Can you please put a sample of how to use the library?

I know it's very easy to make it, but I think that every library should offer a sample.

Here's a sample code, if anyone wants to check out:

activity_main.xml

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
 xmlns:tools="http://schemas.android.com/tools"
 android:id="@+id/container"
 android:layout_width="match_parent"
 android:layout_height="match_parent"/>

SettingsActivity.java

public class SettingsActivity extends ActionBarActivity
  {
  public class SettingsFragment extends PreferenceFragment
    {
    @Override
    public void onCreate(final Bundle savedInstanceState)
      {
      super.onCreate(savedInstanceState);
      addPreferencesFromResource(R.xml.pref_general);
      }
    }

  @Override
  protected void onCreate(final Bundle savedInstanceState)
    {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    getSupportFragmentManager().beginTransaction().add(R.id.container,new SettingsFragment()).commit();
    }
  }

pref_general.xml

<PreferenceCategory android:title="PreferenceCategory" >
    <CheckBoxPreference
        android:key="checkbox_preference"
        android:summary="CheckBoxPreference"
        android:title="CheckBoxPreference" />
</PreferenceCategory>

As someone not that familiar with fragments (I'm in the process of porting an old app from Action Bar Sherlock activities to ActionBarActivities with fragments), this was actually really helpful. Thanks.

@gstorer I've also made a library that doesn't require you to use a fragment for the preferences, if you wish:
https://github.com/AndroidDeveloperLB/MaterialStuffLibrary

I have a question:
This PreferenceFragment implementation can by used in a such scheme:

  1. as replacement for preHoneycomb PreferenceActivity enabling adding the same functionality inside ActionBarActivity (with ActionBar) like Preference Fragment -> Preference Screens, but without enabling usage of android PreferenceActivity -> Preference Headers + Preference Fragments -> Preference Screens
    or
  2. it could be used in conjunction with Android PreferenceActivity class postHoneycomb where this Activity provide preference header and associated Prefrence Fragments can be implemented with this android-support-v4-preferencefragment and it will provide preference screens... but if so what with Preference Activity on pre-honycomb, what I know there is only one Preference Activity class so on pre-honycomb it will not provider preference headers/action bard and so on ?

Please clarify scheme of use that works on pre-Honycomb and post-Honeycomb:

  1. it enables implementing only as this replacement for pre-Honycomb PreferenceActivity scheme
    or
  2. more sophisticated usage as post-Honeycomb with PreferenceActivity -> Preference Headers

@michzio Sadly, it's just the simple usage. no headers and such, as they weren't introduced on pre-Honeycomb, and the support library doesn't have those...

@michzio If you wish, you can check out my library that adds actionBar for PreferenceActivity:
https://github.com/AndroidDeveloperLB/ActionBarPreferenceActivity
A similar thing can be done here.