Learn Pain Less

HomeOur TeamContact
Android
Use custom layout in Material Toolbar Android
Pawneshwer Gupta
Pawneshwer Gupta
December 28, 2016
1 min

Table Of Contents

01
below is example of normal toolbar layout.
02
and below is Java :
03
So below is code to add custom layout inside Toolbar.
04
java code to find views :
Use custom layout in Material Toolbar Android

Hello Coder,

In this tutorial I will show you that how we can use custom layout in material toolbar layout. This is very easy just put your layout directly inside tag.

below is example of normal toolbar layout.

<android.support.v7.widget.Toolbar
android:id="@+id/toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:background="?attr/colorPrimary"
app:popupTheme="@style/MenuTheme" />

and below is Java :

Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);

So below is code to add custom layout inside Toolbar.

<android.support.v7.widget.Toolbar
android:id="@+id/toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:background="?attr/colorPrimary"
app:popupTheme="@style/MenuTheme">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<ImageView
android:id="@+id/logo"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_centerVertical="true"
android:padding="8dp" />
<TextView
android:id="@+id/title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true" />
</RelativeLayout>
</android.support.v7.widget.Toolbar>

java code to find views :

Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
ImageView logo = (ImageView) toolbar.findViewById(R.id.logo);
TextView title = (TextView) toolbar.findViewById(R.id.title);

This way we can use custom layout inside Toolbar, like custom logo,icons, titles etc according to your need.

if you are getting extra margin from left in toolbar layout then read this post to Remove extra paddings from toolbar custom layout.

Subscribe to our newsletter!

We'll send you the best of our blog just once a month. We promise.

Tags

materialtoolbar

Share


Pawneshwer Gupta

Pawneshwer Gupta

Software Developer

Pawneshwer Gupta works as a software engineer who is enthusiastic in creating efficient and innovative software solutions.

Expertise

Python
Flutter
Laravel
NodeJS

Social Media

Related Posts

how to make fully Android Transparent Status bar
how to make fully Android Transparent Status bar
January 10, 2018
1 min
Learn Pain Less  © 2024, All Rights Reserved.
Crafted with by Prolong Services

Quick Links

Advertise with usAbout UsContact Us

Social Media