In my application I am using the new Action Bar Compatibility sample from Google (located at <sdk>/samples/android-<version>/ActionBarCompat
) which works great. The only problem I have is applying this to my PreferenceActivity
in order to get a screen like the settings in the Android Market (see picture).
To fill the ActionBar
with icons, each Activity
must extend the ActionBarActivity
class. The problem is that my Activity
already extends PreferenceActivity
and in Java classes can not extend more than one class.
There must be a way to get the ActionBar
together with a PreferenceScreen
. I would be glad if anybody could provide a solution for this common issue.
P.S.: A solution like in How to add a button to PreferenceScreen does not fit because the ActionBar
is actually the title bar and so this is more a Java than a layout thing.
Edit: My answer below is rather hacky and it seems like it is now outdated (for pre Android 3.0) Have a look at the other answers for less hacky and more current solutions ~pyko 2014-09-01
I managed to get it working - not sure if this is the nicest/cleanest solution, but it works.
Had to make the following changes:
Make a copy of
ActionBarActivity
and have the new class extendPreferenceActivity
Modify
onCreate()
inActionBarHelperBase.java
slightly - make a special case forPreferenceActivity
classesHave your PreferenceActivity extend this class and add request for
FEATURE_CUSTOM_TITLE
before you callsuper.onCreate()
As far as I can tell, changes 2 and 3 are needed because for
PreferenceActivity
:I guess the order of how components in
PreferenceActivity
activities are created is different to plainActivity
activities .Thanks, just an update, you need to add an if statement before the Custom Title line to support HoneyComb and above.
If you want to try a PreferenceFragment implementation based on support-v4 Fragment:
https://github.com/kolavar/android-support-v4-preferencefragment
I´m using it by myself and it isnt much work turning PreferenceActivity into PreferenceFragment.
You can easily add action bar in preference activity by the following changes:
In AndroidManifest.xml :
In v21/styles.xml
In v14/styles.xml for Back API support:
I used in my application this actionbar https://github.com/johannilsson/android-actionbar and it's work great with this thread How to add a button to PreferenceScreen
I'd like to thank to @pyko providing a great answer, but it has problem that it won't work well on HoneyComb and above. well you can have a hack way to get it around like @AndroidDev said; But @pyko is gonna pollute the ActionBarHelperBase class, and @AndroidDev isn't very transparent.The best way is to create ActionBarActivityPreferences who extends from PreferenceActivity; and in onCreate method, change the order of calling parent method:
why calls 'mActionBarHelper.onCreate(savedInstanceState);' before 'super.onCreate(savedInstanceState);' , that is because super (i.e. PreferenceActivity) is actually setting the content view in its onCreate method, which would cause crash ("requestFeature() must be called before adding content'). SO what you need do is to swap the order, make sure ' mActionBarHelper.onCreate(savedInstanceState);' is called before super. In this way, we don't need to pollute the 'ActionBarHelperBase' yet we keep SettingActivity very clean because we encapsulate the tricky detail to 'ActionBarActivityPreferences' and bang!