Search code examples
androidandroid-toolbar

How to create separate ToolBar for each activity in android?


I am developing an app where i need separate Toolbar for every activity, if i create separate toolbar then they collide/merge in some activity. How to do it efficiently ?


Solution

  • Create a toolbar.xml

    <?xml version="1.0" encoding="utf-8"?>
    <android.support.v7.widget.Toolbar
        xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:app="http://schemas.android.com/apk/res-auto"
        android:id="@+id/toolbar"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="?attr/colorPrimary"
        app:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar"
        >
    
    </android.support.v7.widget.Toolbar>
    

    and in the activity add the given toolbar

    <include
            android:id="@+id/toolbar"
            layout="@layout/toolbar"/>
    

    To be precise Toolbar is just a view and you have to add it to your each Activity in which you want to show it.