Asthme
Asthme

Reputation: 5353

Preference Activity Vs PreferenceFragment?

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

Upvotes: 5

Views: 7525

Answers (2)

bizimunda
bizimunda

Reputation: 1035

  • 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

Upvotes: 0

Philipp Jahoda
Philipp Jahoda

Reputation: 51421

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().

Upvotes: 4

Related Questions