Search code examples
androidandroid-actionbarandroid-toolbar

Android Toolbar instead of action bar


I'm trying to use Toolbar instead of ActionBar. In Android 5.0.+ it works but in 4.4 the status bar is placed on top of the Toolbar.

Screenshot:

Screenshot

Layout:

<android.support.v7.widget.Toolbar
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:id="@+id/toolbar"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:minHeight="?attr/actionBarSize"   
    app:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar"     
    app:popupTheme="@style/ThemeOverlay.AppCompat.Light"
    android:background="@color/actionbar_color"/>

Adding Toolbar in OnCreate():

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

Full layout:

<?xml version="1.0" encoding="utf-8"?>
<android.support.v4.widget.DrawerLayout 
    android:id="@+id/drawer_layout"
    android:layout_width="match_parent"
    android:fitsSystemWindows="true"
    android:layout_height="match_parent" >

    <LinearLayout
        android:id="@+id/drawer_content"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical">

        <android.support.v7.widget.Toolbar
            android:id="@+id/toolbar"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:minHeight="?attr/actionBarSize"   
            app:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar"     
            app:popupTheme="@style/ThemeOverlay.AppCompat.Light"
            android:background="@color/actionbar_color"/>

        <com.astuetz.PagerSlidingTabStrip
            android:id="@+id/tabs"
            android:textColor="@android:color/white"
            android:layout_width="match_parent"
            android:layout_height="48dip"
            android:background="@color/actionbar_color" />

        <android.support.v4.view.ViewPager
            android:id="@+id/pager"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_above="@+id/colors"
            android:layout_below="@+id/tabs"/>

    </LinearLayout>

    <LinearLayout
        android:id="@+id/left_drawer"
        style="@style/drawer_list"
        android:fitsSystemWindows="true"
        android:orientation="vertical">

         <ListView 
             android:id="@+id/mainMenu"
             android:layout_marginTop="10dp"
             android:layout_marginBottom="10dp"
             android:layout_width="match_parent"
             android:divider="@android:color/transparent"
             android:layout_height="wrap_content" />

    </LinearLayout>
</android.support.v4.widget.DrawerLayout>

Solution

  • Add a frameLayout and in its height use @dimen/statusBarHeight. Then you can create an attrs.xml file for each version (normal, v19 and v21). Inside of each attrs.xml put <dimen name="statusBarHeight">25dp</dimen> Change 25dp to 0 in normal, 25 in v19 and 0 in v21. You can create attrs.xml for landscape mode too (values-land, values-land-v19, etc).

    <FrameLayout
            android:id="@+id/statusBar"
            android:layout_width="match_parent"
            android:layout_height="@dimen/statusBarHeight"
            android:background="?colorPrimaryDark"
            android:translationZ="4dp" />
    
        <android.support.v7.widget.Toolbar xmlns:android="http://schemas.android.com/apk/res/android"
            android:id="@+id/toolbar"
            android:layout_width="match_parent"
            android:layout_height="?attr/actionBarSize"
            android:background="?attr/colorPrimary"
            android:elevation="4dp"
            app:popupTheme="@style/ThemeOverlay.AppCompat.Light"
            app:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar" />
    

    Take a look this link: Github