PreferenceFragment with support library
Asked Answered
S

3

7

I'm developing an app where I've been using the support library fragments throughout and I stumbled upon this problem where I can't seem to add a PreferencesFragment (for the settings) using this library?

I've found some suggestions to use v7 PreferenceFragmentCompat, however looks like for some reason I can't add the v7 support library to my build path, therefore I can't locate the PreferenceFragmentCompat...

I've tried rewriting the code to use regular fragments instead of the ones in support library, but I had some issues with that too

In case you're wondering, I'm developing with support library because, while reading The Big Nerd Ranch book on android programming, somewhere early on they advise always using support library for fragments.

Any suggestions on the workarounds, or should I just try to switch to non-support version?

Here are the dependencies from my build.gradle:

dependencies {
    compile fileTree(dir: 'libs', include: ['*.jar'])
    testCompile 'junit:junit:4.12' 
    compile 'com.android.support:appcompat-v7:23.1.1'
    compile 'com.android.support:design:23.1.1'
}
Solana answered 9/4, 2016 at 3:45 Comment(3)
why you can't add support library to your build path?Northwester
Are you using Android Studio or Eclipse? If you're using Android Studio show your build.gradle dependencies....Upas
here's my gradle dependencies dependencies { compile fileTree(include: ['*.jar'], dir: 'libs') testCompile 'junit:junit:4.12' compile 'com.android.support:appcompat-v7:23.1.1' compile 'com.android.support:design:23.1.1' } @DanielNugentSolana
U
23

The appcompat v7 library actually uses the v4 support library, so you need to explicitly import the v7 support library components that you need.

In your case, you just need to add compile 'com.android.support:preference-v7:23.1.1' to your build.gradle:

dependencies {
    compile fileTree(dir: 'libs', include: ['*.jar'])
    compile 'com.android.support:appcompat-v7:23.1.1'
    compile 'com.android.support:design:23.1.1'
    compile 'com.android.support:preference-v7:23.1.1'
}

Then this will work:

import android.os.Bundle;
import android.support.v7.preference.PreferenceFragmentCompat;
import android.view.View;

public class MyPreferenceFragment extends PreferenceFragmentCompat {

    public MyPreferenceFragment() {
        // Required empty public constructor
    }

    @Override
    public void onCreatePreferences(Bundle bundle, String s) {
        addPreferencesFromResource(R.xml.fragment_settings_pref);
    }
}
Upas answered 9/4, 2016 at 18:11 Comment(3)
Thanks Daniel, I did have an issue in the gradle itself, I wasn't paying attention thinking adding the dependencies in project structure will automatically config the gradle file.Solana
You sir, saved my day.Hackathorn
You should place addPreferencesFromResource in onCreatePreferences: "Called during onCreate(Bundle) to supply the preferences for this fragment. Subclasses are expected to call setPreferenceScreen(PreferenceScreen) either directly or via helper methods such as addPreferencesFromResource(int)."Traction
R
1

If you are working with androidx use;

 implementation "androidx.legacy:legacy-preference-v14:1.0.0"
 implementation "androidx.preference:preference:1.1.1"
Rachaba answered 6/7, 2020 at 17:9 Comment(0)
B
0

The gradle dependency for support library preference fragment is:

implementation 'com.android.support:preference-v14:28.0.0'
Bimanous answered 12/4, 2020 at 10:15 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.