Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Add custom layout to toolbar

I followed this tutorial to give my app a regular toolbar with some tabs.

I want to change the toolbar so it looks more like this:

enter image description here

I want to add some text and images into the toolbar. How can I do this?

activity_main.xml:

<?xml version="1.0" encoding="utf-8"?> <android.support.design.widget.CoordinatorLayout     xmlns:android="http://schemas.android.com/apk/res/android"     xmlns:app="http://schemas.android.com/apk/res-auto"     android:layout_width="match_parent"     android:layout_height="match_parent">      <android.support.design.widget.AppBarLayout         android:id="@+id/appbar"         android:layout_width="match_parent"         android:layout_height="wrap_content"         android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar"         app:elevation="0dp">          <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="?attr/actionBarSize"             android:background="@color/primary"             app:layout_scrollFlags="scroll|enterAlways"             app:theme="@style/ToolbarTheme"             app:popupTheme="@style/ThemeOverlay.AppCompat.Light"             />          <android.support.design.widget.TabLayout             android:id="@+id/tabs"             android:layout_width="match_parent"             android:layout_height="wrap_content"             app:tabTextAppearance="@style/TabLayout"             app:tabSelectedTextColor="@color/white"             />      </android.support.design.widget.AppBarLayout>      <android.support.v4.view.ViewPager         android:id="@+id/pager"         android:layout_width="match_parent"         android:layout_height="match_parent"         app:layout_behavior="@string/appbar_scrolling_view_behavior"         />  </android.support.design.widget.CoordinatorLayout> 
like image 689
user5374735 Avatar asked Sep 25 '15 04:09

user5374735


People also ask

Can we customize Toolbar?

You can customize toolbars in the main window, editors, and dialogs. To display the toolbar, select the Toolbar command on the Options menu. A check mark indicates that the toolbar is active. To modify the toolbar, select Customize Toolbar on the Options menu (or double-click the toolbar area).

What is custom Toolbar in Android?

Toolbar was introduced in Android Lollipop, API 21 release and is the spiritual successor of the ActionBar. It's a ViewGroup that can be placed anywhere in your XML layouts. Toolbar's appearance and behavior can be more easily customized than the ActionBar.


1 Answers

You can try this :-

<android.support.design.widget.CoordinatorLayout     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:id="@+id/main_container"     android:layout_width="match_parent"     android:layout_height="match_parent">           <!-   Your view -->        <android.support.design.widget.AppBarLayout         android:id="@+id/appbar"         android:layout_width="match_parent"         android:layout_height="wrap_content"         android:elevation="6dp"         android:fitsSystemWindows="true"         android:theme="@style/AppTheme">         <android.support.v7.widget.Toolbar          xmlns:android="http://schemas.android.com/apk/res/android"          xmlns:app="http://schemas.android.com/apk/res-auto"          android:layout_width="match_parent"          android:id="@+id/toolbar"          android:layout_height="?attr/actionBarSize"          android:elevation="6dp"          app:layout_collapseMode="pin"          app:layout_scrollFlags="scroll|enterAlways"          app:popupTheme="@style/AppTheme">             <LinearLayout                android:id="@+id/llContainer"                android:layout_width="match_parent"                android:orientation="vertical"                android:background="@android:color/black"                android:gravity="center"                android:layout_height="300dp">                   <!-  Your TextView / ImageView -->              </LinearLayout>        </android.support.v7.widget.Toolbar>        <android.support.design.widget.TabLayout           android:id="@+id/tabs"           android:layout_width="match_parent"           android:layout_height="wrap_content"           app:tabTextAppearance="@style/TabLayout"           app:tabSelectedTextColor="@color/white"/>   </android.support.design.widget.AppBarLayout>   <android.support.v4.view.ViewPager     android:id="@+id/pager"     android:layout_width="match_parent"     android:layout_height="match_parent"     app:layout_behavior="@string/appbar_scrolling_view_behavior"     />  </android.support.design.widget.CoordinatorLayout> 
like image 159
Android Team Avatar answered Sep 29 '22 14:09

Android Team