Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Sample app code? #4

Open
AndroidDeveloperLB opened this issue Jul 19, 2014 · 6 comments
Open

Sample app code? #4

AndroidDeveloperLB opened this issue Jul 19, 2014 · 6 comments

Comments

@AndroidDeveloperLB
Copy link

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.

@AndroidDeveloperLB
Copy link
Author

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>

@gstorer
Copy link

gstorer commented Nov 2, 2014

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.

@AndroidDeveloperLB
Copy link
Author

@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

@michzio
Copy link

michzio commented Mar 7, 2015

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

@AndroidDeveloperLB
Copy link
Author

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

@AndroidDeveloperLB
Copy link
Author

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants