9

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?

Irfan S
  • 267
  • 5
  • 14
tauri
  • 293
  • 2
  • 6
  • 18
  • Before I added toolbar, in the solution of Suyash, it looked just like : hideActionBar + android:windowTranslucentStatus. For api21 action bar dissappeared, for api19 didn't change except getting grey. Did I do something wrongly? – tauri Mar 15 '15 at 00:51

4 Answers4

7

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);
Joaquin Iurchuk
  • 5,499
  • 2
  • 48
  • 64
  • Changing your theme on API 21 from AppCompat to something else is unnecessary (and actually breaks much of the theming of `ActionBarActivity`). – ianhanniballake Mar 15 '15 at 04:16
  • Thank you for help. It seemed that I didn't add to styles.xml . – tauri Mar 15 '15 at 11:20
  • @joaquin. I use AppCompat toolbar for my project (like you say). but for lower APIs (lower than 20),my app has stopped.why? – Mina Dahesh Sep 11 '16 at 11:55
  • @MinaDahesh can you provide some useful data? Like the exception you're getting. – Joaquin Iurchuk Sep 11 '16 at 11:57
  • @joaquin.all my activitys extends *AppCompatActivity* . and my app theme is *Theme.AppCompat.Light* . although i use *setSupportActionBar(toolbar);* but now time, as i run it in api 16, it has stopped. – Mina Dahesh Sep 11 '16 at 12:56
  • @joaquin. java.lang.RuntimeException: Unable to start activity ComponentInfo{PACKAGE}: java.lang.IllegalStateException: This Activity already has an action bar supplied by the window decor. Do not request Window.FEATURE_SUPPORT_ACTION_BAR and set windowActionBar to false in your theme to use a Toolbar instead. – Mina Dahesh Sep 11 '16 at 14:56
  • 1
    @MinaDahesh that's because I'm sure you're not configuring the `styles.xml` file in the correct way. Try to set it up to No Action Bar. Check my answer carefully. – Joaquin Iurchuk Sep 12 '16 at 14:06
4

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.

Community
  • 1
  • 1
Amit Vaghela
  • 22,772
  • 22
  • 86
  • 142
1

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 .

Onur OKYAY
  • 301
  • 3
  • 3
1

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.

Pradeep Singh
  • 432
  • 5
  • 11