Action Bar compatibility has been added into support library, revision 18. It now has ActionBarActivity
class for creating activities with Action Bar on older versions of Android.
Is there any way to add Action Bar from support library into PreferenceActivity
?
Previously I used ActionBarSherlock and it has SherlockPreferenceActivity
.
I was able to get
android.app.Actionbar
by usinggetActionBar()
. It returned a null value at first... then I went to the manifest and changed the theme to:Then I was able to have the actionbar again. I'm assuming this will only work for certain build levels. So you might want to do a check for the build number or check if the value returned is null.
It'll be fine for me because the app I'm working on is for
ICS/4.0
+.Problem Background:
The OP wants to know how can we put
MenuItem
s in theActionBar
ofPreferenceActivity
for pre-Honeycomb because Android's support library has a bug which doesn't allow this to happen.My Solution:
I've found a much cleaner way, than already proposed, to achieve the target (and found it in the Android Docs):
Now do what you would normally do in your
onOptionsItemSelected()
. Since it's a part of Android Docs, it has no side-affects.Happy coding. :)
Update:
This solution no longer works if you're targeting Lollipop. If you're using AppCompat, this answer is what you should be looking for.