Alternatives to PreferenceFragment with android-support-v4

AndroidAndroid Fragments

Android Problem Overview


I've come to a sudden halt in the development of my app as I realized that PreferenceFragments weren't supported in this library. Are there any alternatives that a rookie android developer can use to overcome this obstacle ?

This is my main window as of right now

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent">
<TabHost
    android:id="@android:id/tabhost"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    >
    <LinearLayout
        android:orientation="vertical"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        >

        <FrameLayout
            android:id="@android:id/tabcontent"
            android:layout_width="0dp"
            android:layout_height="0dp"
            android:layout_weight="0"/>

        <FrameLayout
            android:id="@+android:id/realtabcontent"
            android:layout_width="fill_parent"
            android:layout_height="0dp"
            android:layout_weight="1"/>
    </LinearLayout>
    
    	        <TabWidget
            android:id="@android:id/tabs"
            android:orientation="horizontal"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:layout_weight="0"
            />
    
</TabHost>
</LinearLayout>

For my TabActivity I'm using something I found online. Here's a snippet:

public class TabControlActivity extends FragmentActivity implements TabHost.OnTabChangeListener 
{
public static final int INSERT_ID = Menu.FIRST;
public static TabControlActivity thisCtx;
private TabHost mTabHost;
private HashMap mapTabInfo = new HashMap();
private TabInfo mLastTab = null;

private class TabInfo {
	 private String tag;
     private Class clss;
     private Bundle args;
     private Fragment fragment;
     TabInfo(String tag, Class clazz, Bundle args) {
    	 this.tag = tag;
    	 this.clss = clazz;
    	 this.args = args;
     }

}

class TabFactory implements TabContentFactory 
{

	private final Context mContext;

    /**
     * @param context
     */
    public TabFactory(Context context) {
        mContext = context;
    }

    /** (non-Javadoc)
     * @see android.widget.TabHost.TabContentFactory#createTabContent(java.lang.String)
     */
    public View createTabContent(String tag) {
        View v = new View(mContext);
        v.setMinimumWidth(0);
        v.setMinimumHeight(0);
        return v;
    }

}

...*snip*...

Is there any to implement something that resembles a preferencefragment(or preferenceActivity) using the android-support-v4 compatibility library ?

Android Solutions


Solution 1 - Android

I know this is an old question, but you can now use PreferenceFragmentCompat from com.android.support:preference-v7:23.3.0

Solution 2 - Android

UPDATE - 6/11/2015

Support-v7 library now includes PreferenceFragmentCompat. So it will be a better idea to use it.


Add the following project as a library project to your application.

https://github.com/kolavar/android-support-v4-preferencefragment

You can keep everything including your fragment transaction as it is. When importing the PreferenceFragment class, make sure the correct import header is user.

import android.support.v4.preference.PreferenceFragment;

instead of

import android.preference.PreferenceFragment;

Solution 3 - Android

Important Update: The latest revision of the v7 support library now has a native PreferenceFragmentCompat.

I am using this library, which has an AAR in mavenCentral so you can easily include it if you are using Gradle.

compile 'com.github.machinarius:preferencefragment:0.1.1'

Solution 4 - Android

You can use my own PreferenceFragment.

It's simple and I had no issues with it so far. I think you can only display one of them at a time in a single Activity, which should be OK.

Solution 5 - Android

Preferences Support Library: Preference Fragments for API 7+, no matter the Activity

A simple implementation would include a PreferenceFragmentCompat such as:

public class PreferencesFragment extends PreferenceFragmentCompat {
    @Override
    public void onCreatePreferences(Bundle savedInstanceState, String rootKey) {
        addPreferencesFromResource(R.xml.preferences);
    }
}

You’ll also need to set preferenceTheme in your theme:

<style name="AppTheme" parent="@style/Theme.AppCompat.Light">
  <item name="preferenceTheme">@style/PreferenceThemeOverlay</item>
</style>

And add this in dependencies: http://developer.android.com/tools/support-library/features.html

com.android.support:preference-v14:23.1.0

Solution 6 - Android

You coul maybe use a real activity and use fragment, but I don't think it's a good choice. You should use simple PreferenceActivity and wait for improvements in retro compat libs.

Solution 7 - Android

You can use third party libraries such as UnifiedPreferences to use a single API for all versions of Android.

Solution 8 - Android

You can extend from PreferenceActivity instead, and if you wish to have an ActionBar, you can add it using a Toolbar as being above the ListView used for the preferences.

This can be done by using a vertical LinearLayout which holds the Toolbar and a ListView with android:id="@android:id/list" .

If you wish, you can see my solution here .

Solution 9 - Android

As Lucius said you can use PreferenceFragmentCompat :

public class MyPreferenceFragment extends PreferenceFragmentCompat {
        @Override
        public void onCreatePreferences(Bundle bundle, String s) {               
            PreferenceManager.setDefaultValues(getActivity(), R.xml.preferences, false);
            addPreferencesFromResource(R.xml.preferences);
       }
}

You must include its dependency in gradle:

dependencies {
...
    compile 'com.android.support:preference-v7:23.1.X' (wherX = 0,1,...)
...
}

This way you can also can use FragmentTransaction of android.support.v4.app.FragmentTransaction and PrefernceFragment. However, you maybe have problems with themes. If it is the case, you can solve it having into account this post:

Solution to make the lib work on API 7+ while keeping material theme on API 14+

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionCodePrimateView Question on Stackoverflow
Solution 1 - AndroidLuciusView Answer on Stackoverflow
Solution 2 - AndroidC--View Answer on Stackoverflow
Solution 3 - AndroidtheblangView Answer on Stackoverflow
Solution 4 - AndroidBladeCoderView Answer on Stackoverflow
Solution 5 - Androidli2View Answer on Stackoverflow
Solution 6 - AndroidSnicolasView Answer on Stackoverflow
Solution 7 - AndroidMarcoDuffView Answer on Stackoverflow
Solution 8 - Androidandroid developerView Answer on Stackoverflow
Solution 9 - AndroidvicasdiaView Answer on Stackoverflow