Search code examples
androidandroid-actionbarandroid-appbarlayout

Android: How to customise App bar (action bar) like this?


I need exactly like this, i saw some example to customise toolbar but could not design, how to use those gradients and all?

enter image description here


Solution

  • Create a drawable something like this drawable

    <?xml version="1.0" encoding="utf-8"?>
    <shape xmlns:android="http://schemas.android.com/apk/res/android"
    android:shape="rectangle">
    
    <gradient
        android:centerColor="@color/app_blue_color"
        android:endColor="@color/app_dark_blue_color"
        android:startColor="@color/app_dark_blue_color" />
    
    <!--<corners-->
        <!--android:bottomLeftRadius="5dp"-->
        <!--android:bottomRightRadius="5dp"-->
        <!--android:topLeftRadius="5dp"-->
        <!--android:topRightRadius="5dp" />-->
    
    <stroke
        android:width="0.7dp"
        android:color="@color/app_blue_color" />
    

    set this as the background of your customize toolbar

    create a ToolbarLayout

      <?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="?attr/actionBarSize"
        android:background="@drawable/toolbarDrawable"
        android:gravity="top"
        app:contentInsetLeft="0dp"
        app:contentInsetStart="0dp"
        app:contentInsetStartWithNavigation="0dp"
        app:layout_collapseMode="pin"
        app:layout_scrollFlags="scroll|enterAlways"
        app:popupTheme="@style/AppTheme"
        app:theme="@style/ToolbarColoredBackArrow">
    
        <RelativeLayout
            android:layout_width="match_parent"
            android:layout_height="?attr/actionBarSize"
            android:gravity="center_vertical">
    
            <ImageView
                android:id="@+id/RefreshButton"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_alignParentRight="true"
                android:layout_centerVertical="true"
                android:layout_marginRight="10dp"
                android:adjustViewBounds="true"
                android:padding="8dp"
                android:src="@drawable/plus_icon"
                android:visibility="gone" />
    
    
            <TextView
                android:id="@+id/toolbarName"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_centerInParent="true"
                android:layout_marginRight="@dimen/margin_10"
                android:gravity="center"
                android:text=""
                android:textColor="@color/white"
                android:textSize="13sp"
                android:textStyle="bold|italic"
                android:visibility="visible" />
    
        </RelativeLayout>
    
    </android.support.v7.widget.Toolbar>
    

    In your Activity include layout using

        <include
            android:id="@+id/toolbar"
            layout="@layout/toolbar_title" /> 
    
     // toolbar_tile is my toolbar layout name