The toolbar is a generalization of action bars for use in application layouts, now there are two practices for answering your question:
Bad practice:
Bad practice is to define a toolbar in each layout.
Standard practice:
Standard practice is to identify the layout and indicate it in the base activity. You just need to include this toolbar layout in any layout you want (using <include> ), and extend a certain base activity depending on your activity.
This standard practice will help you maintain a single code base for the toolbar and save time from defining the toolbar every time.
Example: Google I / O 2014 Android App
toolbar_actionbar_with_headerbar.xml
<android.support.v7.widget.Toolbar xmlns:android="http://schemas.android.com/apk/res/android" xmlns:iosched="http://schemas.android.com/apk/res-auto" style="@style/HeaderBar" iosched:theme="@style/ActionBarThemeOverlay" iosched:popupTheme="@style/ActionBarPopupThemeOverlay" android:id="@+id/toolbar_actionbar" iosched:titleTextAppearance="@style/ActionBar.TitleText" iosched:contentInsetStart="?actionBarInsetStart" android:layout_width="match_parent" android:layout_height="?actionBarSize" />
This toolbar layout is referenced in the options action, as shown below:
activity_settings.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" tools:context=".ui.SettingsActivity"> <include layout="@layout/toolbar_actionbar_with_headerbar" /> <FrameLayout android:id="@+id/container" android:layout_width="match_parent" android:layout_height="0dp" android:layout_weight="1" /> </LinearLayout>
Paresh mayani
source share