Skip to content Skip to sidebar Skip to footer

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: Layout:

Solution 1:

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

Solution 2:

For Kitkat add:

android:fitsSystemWindows="true"

Solution 3:

Tutorial: http://www.viralandroid.com/2015/08/android-toolbar-example.html

Add in your XML layout

<android.support.v7.widget.Toolbar
        android:id="@+id/toolbar"
        android:minHeight="?attr/actionBarSize"
        android:background="#2196F3"
        android:layout_width="match_parent"
        android:layout_height="wrap_content">
    </android.support.v7.widget.Toolbar>

In Java

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

Solution 4:

activity_main.xml

<?xml version="1.0" encoding="utf-8"?><android.support.design.widget.CoordinatorLayoutxmlns:android="http://schemas.android.com/apk/res/android"xmlns:app="http://schemas.android.com/apk/res-auto"xmlns:tools="http://schemas.android.com/tools"android:layout_width="match_parent"android:layout_height="match_parent"android:fitsSystemWindows="true"tools:context="com.azim.innovation.MainActivity"><android.support.design.widget.AppBarLayoutandroid:layout_width="match_parent"android:layout_height="wrap_content"android:theme="@style/AppTheme.AppBarOverlay"><android.support.v7.widget.Toolbarandroid:id="@+id/toolbar"android:layout_width="match_parent"android:layout_height="?attr/actionBarSize"android:background="?attr/colorPrimary"app:popupTheme="@style/AppTheme.PopupOverlay" /></android.support.design.widget.AppBarLayout><includelayout="@layout/content_main" /><android.support.design.widget.FloatingActionButtonandroid:id="@+id/fab"android:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_gravity="bottom|end"android:layout_margin="@dimen/fab_margin"android:src="@android:drawable/ic_dialog_email" /></android.support.design.widget.CoordinatorLayout>

content_main.xml

 <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    app:layout_behavior="@string/appbar_scrolling_view_behavior"
    tools:context="com.azim.innovation.MainActivity"
    tools:showIn="@layout/activity_main">

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Hello World!" />
</RelativeLayout>

Colors

<colorname="colorPrimary">#3F51B5</color><colorname="colorPrimaryDark">#303F9F</color><colorname="colorAccent">#FF4081</color>

Styles

<resources><!-- Base application theme. --><stylename="AppTheme"parent="Theme.AppCompat.Light.DarkActionBar"><!-- Customize your theme here. --><itemname="colorPrimary">@color/colorPrimary</item><itemname="colorPrimaryDark">@color/colorPrimaryDark</item><itemname="colorAccent">@color/colorAccent</item></style><stylename="AppTheme.NoActionBar"><itemname="windowActionBar">false</item><itemname="windowNoTitle">true</item></style><stylename="AppTheme.AppBarOverlay"parent="ThemeOverlay.AppCompat.Dark.ActionBar" /><stylename="AppTheme.PopupOverlay"parent="ThemeOverlay.AppCompat.Light" />

Styles-v21

<resources><stylename="AppTheme.NoActionBar"><itemname="windowActionBar">false</item><itemname="windowNoTitle">true</item><itemname="android:windowDrawsSystemBarBackgrounds">true</item><itemname="android:statusBarColor">@android:color/transparent</item></style>

Dependency

compile'com.android.support:appcompat-v7:23.3.0'compile'com.android.support:design:23.3.0'

Solution 5:

Try using the ThemeOverlay.NoActionBar theme. It dosen't add the Action Bar

Post a Comment for "Android Toolbar Instead Of Action Bar"