Home » Android » Android Toolbar for API 19? (for API 21 works ok)

Android Toolbar for API 19? (for API 21 works ok)

Posted by: admin June 15, 2020 Leave a comment


I used How do I use DrawerLayout to display over the ActionBar/Toolbar and under the status bar?
answer of Suyash (I also added a toolbar, maybe incorrectly) to put Navigation Drawer over the “action bar”.

For API level 21 instead of “action bar” I used toolbar, and it works fine.

But for API 19 this is not working:

    if(Build.VERSION.SDK_INT > 19) {
      final Toolbar toolbar = (Toolbar) findViewById(R.id.my_awesome_toolbar);

Do you have idea how I can put NavigationDrawer over “actionbar” (or toolbar) for API level 19?

How to&Answers:

If you use Toolbar then you should be able to view the exact same Toolbar in any API.

For doing that you should have a XML in res/layout:


And in your main layout you should include it:

    layout="@layout/app_bar" />

Also you should set your style as No Action Bar on your styles.xml

<style name="AppTheme.Base" parent="Theme.AppCompat.NoActionBar">
    <item name="colorPrimary">@color/primary</item>
    <item name="colorPrimaryDark">@color/primaryDark</item>
    <item name="colorAccent">@color/accent</item>

But for API 21 you should have another styles.xml:

<style name="AppTheme" parent="AppTheme.Base">
    <item name="android:colorPrimary">@color/primary</item>
    <item name="android:colorPrimaryDark">@color/primaryDark</item>
    <item name="android:colorAccent">@color/accent</item>

And finally in your Main Activity

toolbar = (Toolbar) findViewById(R.id.app_bar);

And finally to any thing you want to do to the toolbar, obtain it and treat it like the old Action Bar:



Material Design for Pre-Lollipop Devices :

All of your themes (that want an Action Bar/Toolbar) must inherit
from Theme.AppCompat. There are variants available, including Light
and NoActionBar.

When inflating anything to be displayed on the action
bar (such as a SpinnerAdapter for list navigation in the toolbar),
make sure you use the action bar’s themed context, retrieved via

Android Support Library 22.1 :

AppCompat allows you to use android:theme for Toolbars (deprecating
the app:theme used previously) and, even better, brings android:theme
support to all views on API 11+ devices.


First , you need to add com.android.support:appcombat-v7:25.3.0 as a dependency . Then import android.support.v7.widget.Toolbar to the activity that you want to add the toolbar. With this way you can implement the toolbar for api 19 .



If you don’t want to use android support library. Here is a better work through now available for this problem


Migrating to AndroidX Class Mappings available through this official link

Further you can also directly refractor your whole project to AndroidX from Android Studio:
From Menu you can do
Refactor > Migrate to AndroidX

More information here

Further, for me the Refractor > Migrate to AndroidX feature did not convert my Toolbars in xml to AndroidX so, I found former link really helpful.