Friday, 31 March 2017

Custom Toolbar in Android

STEP 1 : Create toolbar.xml in layout folder

<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_height="wrap_content"
    android:layout_width="match_parent"
    android:fitsSystemWindows="true"
    android:minHeight="?attr/actionBarSize"
    app:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar"
    android:background="?attr/colorPrimary">

    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content">

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Custom Toolbar"
            android:textSize="@dimen/toolbartext"
            android:textColor="@android:color/white"
            android:layout_centerVertical="true"
            android:layout_centerHorizontal="true"
            />

    </RelativeLayout>

</android.support.v7.widget.Toolbar>

STEP 2 : Include toolbar in xml file where ever you want.

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

STEP 3 : Add this lines in java file

setContentView(R.layout.activity_home);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
getSupportActionBar().setDisplayHomeAsUpEnabled(true);

ITS DONE !!!

No comments:

Post a Comment