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 have managed to create a workaround similar to what the Google Play Store uses. Link to Original Answer
Please find the GitHub Repo: Here
Very Similar to your own code but added xml to allow for set title:
Continuing to use
PreferenceActivity
:settings_toolbar.xml :
SettingsActivity.java :
Result :
UPDATE (Gingerbread Compatibility) :
As pointed out here, Gingerbread Devices are returning NullPointerException on this line:
FIX:
SettingsActivity.java :
Any issues with the above let me know!
UPDATE 2: TINTING WORKAROUND
As pointed out in many dev notes
PreferenceActivity
does not support tinting of elements, however by utilising a few internal classes you CAN achieve this. That is until these classes are removed. (Works using appCompat support-v7 v21.0.3).Add the following imports:
Then override the
onCreateView
method:Result:
AppCompat 22.1
AppCompat 22.1 introduced new tinted elements, meaning that there is no longer a need to utilise the internal classes to achieve the same effect as the last update. Instead follow this (still overriding
onCreateView
):NESTED PREFERENCE SCREENS
A lot of people are experiencing issues with including the Toolbar in nested
<PreferenceScreen />
s however, I have found a solution!! - After a lot of trial and error!Add the following to your
SettingsActivity
:The reason that
PreferenceScreen
's are such a pain is because they are based as a wrapper dialog, so we need to capture the dialog layout to add the toolbar to it.Toolbar Shadow
By design importing the
Toolbar
does not allow for elevation and shadowing in pre-v21 devices, so if you would like to have elevation on yourToolbar
you need to wrap it in aAppBarLayout
:`settings_toolbar.xml :
Not forgetting to add the add the Design Support library as a dependency in
build.gradle
file:Android 6.0
I have investigated the reported overlapping issue and I cannot reproduce the issue.
The full code in use as above produces the following:
If I am missing something please let me know via this repo and I will investigate.
EDIT: In appcompat-v7 22.1.0 Google added the AppCompatDelegate abstract class as a delegate you can use to extend AppCompat's support to any activity.
Use it like this:
No more hacking. Code taken from AppCompatPreferenceActivity.java.
Found a PreferenceFragment implementation based on support-v4 Fragment:
https://github.com/kolavar/android-support-v4-preferencefragment
Edit: I just tested it and its working great!
Integrating
PreferenceActivity
with ABC is not possible, at least for me. I tried the two possibilities I could find but none worked:Option 1:
ActionBarPreferenceActivity
extendsPreferenceActivity
. When you do this you get restricted byActionBarActivityDelegate.createDelegate(ActionBarActivity activity)
. Also you need to implementActionBar.Callbacks
which is not accessibleOption 2:
ActionBarPreferenceActivity
extendsActionBarActivity
. This approach requires rewriting a whole newPreferenceActivity
,PreferenceManager
and may bePreferenceFragment
which means you need access to hidden classes likecom.android.internal.util.XmlUtils
The solution to this can only come from Google devs implementing anActionBarWrapper
that can be added to any activity.If you really need a preference activity, my advice for now is
ActionBarSherlock
.However, I managed to implement it here.
Now the official answer for this problem has been released. It is the v7/v14 Preference Support library.
See How to use the v7/v14 Preference Support library? for the discussion how to use it.
There is currently no way to achieve with AppCompat. I've opened a bug internally.