4

I am using ViewPager to swipe through different fragments. It is working but now I want to add settings for each fragment as you can do with Activities in this way:

@Override
protected void onCreate(Bundle savedInstanceState) {
    // TODO Auto-generated method stub
    super.onCreate(savedInstanceState);
    addPreferencesFromResource(R.xml.prefs);
}

But when I put addPreferencesFromResource(R.xml.prefs); in my Fragment Eclipse tells me addPreferencesFromResource(R.xml.prefs); doesn't exist, the same if I use FragmentActivity. So, how do I use preferences in fragments? I am new at designing with Fragments and everything is pretty much different compared to how you do in Activities :/

Thank you :)

==EDIT==

I am not getting a FC but it does nothing, it is the code which calls class LCprefs(), this code is on a Fragment:

@Override
    public boolean onOptionsItemSelected(MenuItem item) {
        if(DEBUG) Log.i(TAG, "onOptionsItemSelected(MenuItem item)");
        switch(item.getItemId())
        {
        case R.id.preferences:
            new LCprefs();
            break;
        case R.id.add:
            break;
        case R.id.load:
            break;
        case R.id.save:
            break;
        }
        return false;
    }

It is LCprefs class:

import android.os.Bundle;
import android.preference.PreferenceFragment;
import android.util.Log;

    public class LCprefs extends PreferenceFragment{

        String TAG;

        @Override
        public void onCreate(Bundle savedInstanceState) {
            // TODO Auto-generated method stub
            super.onCreate(savedInstanceState);
            Log.i(TAG, "LCprefs onCreate()");
            addPreferencesFromResource(R.xml.prefslc);
        }

    }

It is prefslc.xml located on xml folder:

<?xml version="1.0" encoding="utf-8"?>
<PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android" >

    <PreferenceCategory android:title="Varios" >

        <CheckBoxPreference
            android:defaultValue="false"
            android:key="cbFullscreen"
            android:summary="Aplicacion fullscreen"
            android:title="Fullscreen" />

    </PreferenceCategory>

</PreferenceScreen>
Andres Torti
  • 113
  • 1
  • 2
  • 9

1 Answers1

1

The fragment needs to be an instance of PreferencesFragment. Example from the link:

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);
    }
}
Joakim Berglund
  • 2,859
  • 1
  • 22
  • 30