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

Questions:

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);
        setSupportActionBar(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:

<android.support.v7.widget.Toolbar
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:background="?attr/colorPrimary"
    android:minHeight="?attr/actionBarSize"/>

And in your main layout you should include it:

<include
    android:id="@+id/app_bar"
    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>
</style>

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>
</style>

And finally in your Main Activity

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

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

getSupportActionBar().setHomeAsUpEnabled(true);

Answer:

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


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.

Answer:

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 .

Answer:

AndroidX

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

androidx.appcompat.widget.Toolbar

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.