5

In preference Activity we can use these two methods SetContentView(R.layout.main) and addXmlFromResources(R.xml.Preferences) for customizing the preference screen. For example see this Adding a button on Prefernce Screen

Is this possible in PreferenceFragment?

In PreferenceFragment, I have added addPreferencesFromResource(R.xml.PreferenceScreen); in onCreate method. When I use onCreateView it's getting force closes. I have tried Layout Inflator also. It's not working.

So is this possible only on preference Activity? not Preference Fragment?

P.S- I am using support V 13 Library. So I have created Preference Fragment in Fragment Pager Adapter. Please don't suggest to me to create Preference Activity for preference Fragment

Community
  • 1
  • 1
Asthme
  • 5,163
  • 6
  • 47
  • 65

2 Answers2

4

You can use

addPreferencesFromResource(int res);

in both PreferenceFragment and PreferenceActivity, inside the onCreate() method. Bear in mind that PreferenceFragment should be used in post Honeycomb Android Versions as a replacement for PreferenceActivity.

If you want a custom-layout for your PreferenceActivity, you can call setContentView() in the onBuildHeaders() method, but not in the onCreate().

Philipp Jahoda
  • 50,880
  • 24
  • 180
  • 187
  • Yes. Use the onCreate() method to call addPreferencesFromResource() for both Activity and Fragment. PreferenceFragment also has an onCreate() method. – Philipp Jahoda May 08 '14 at 11:38
  • @phillip Jahoda I am using support V13 library fragments i have added preference Fragment in Fragment Pager Adapter.So i could not initialize the Preference Activity.. – Asthme May 08 '14 at 11:41
0
  • If your app supports versions of Android older than 3.0 (API level 10 and lower), you must build the activity as an extension of the PreferenceActivity class.
  • On Android 3.0 and later, you should instead use a traditional Activity that hosts a PreferenceFragment that displays your app settings. However, you can also use PreferenceActivity to create a two-pane layout for large screens when you have multiple groups of settings.

see this link

bizimunda
  • 819
  • 2
  • 9
  • 26