Android Toolbar Center Title And Custom Font


Answer :

To use a custom title in your Toolbar all you need to do is remember is that Toolbar is just a fancy ViewGroup so you can add a custom title like so:

<android.support.v7.widget.Toolbar     android:id="@+id/toolbar_top"     android:layout_height="wrap_content"     android:layout_width="match_parent"     android:minHeight="?attr/actionBarSize"     android:background="@color/action_bar_bkgnd"     app:theme="@style/ToolBarTheme" >        <TextView         android:layout_width="wrap_content"         android:layout_height="wrap_content"         android:text="Toolbar Title"         android:layout_gravity="center"         android:id="@+id/toolbar_title" />       </android.support.v7.widget.Toolbar> 

This means that you can style the TextView however you would like because it's just a regular TextView. So in your activity you can access the title like so:

Toolbar toolbarTop = (Toolbar) findViewById(R.id.toolbar_top); TextView mTitle = (TextView) toolbarTop.findViewById(R.id.toolbar_title); 

The ToolBar title is stylable. Any customization you make has to be made in the theme. I'll give you an example.

Toolbar layout:

<?xml version="1.0" encoding="utf-8"?> <android.support.v7.widget.Toolbar     style="@style/ToolBarStyle.Event"     xmlns:android="http://schemas.android.com/apk/res/android"     android:id="@+id/toolbar"     android:layout_width="match_parent"     android:layout_height="wrap_content"     android:background="?attr/colorPrimary"     android:minHeight="@dimen/abc_action_bar_default_height_material" /> 

Styles:

<style name="ToolBarStyle" parent="ToolBarStyle.Base"/>  <style name="ToolBarStyle.Base" parent="">     <item name="popupTheme">@style/ThemeOverlay.AppCompat.Light</item>     <item name="theme">@style/ThemeOverlay.AppCompat.Dark.ActionBar</item> </style>  <style name="ToolBarStyle.Event" parent="ToolBarStyle">     <item name="titleTextAppearance">@style/TextAppearance.Widget.Event.Toolbar.Title</item> </style>  <style name="TextAppearance.Widget.Event.Toolbar.Title" parent="TextAppearance.Widget.AppCompat.Toolbar.Title">     <!--Any text styling can be done here-->     <item name="android:textStyle">normal</item>     <item name="android:textSize">@dimen/event_title_text_size</item> </style> 

This's just to help to join all pieces using @MrEngineer13 answer with @Jonik and @Rick Sanchez comments with the right order to help to achieve title centered easly!!

The layout with TextAppearance.AppCompat.Widget.ActionBar.Title :

    <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/AppTheme.PopupOverlay">          <TextView             android:id="@+id/toolbar_title"             android:layout_width="wrap_content"             android:layout_height="wrap_content"                                   style="@style/TextAppearance.AppCompat.Widget.ActionBar.Title"             android:layout_gravity="center" />      </android.support.v7.widget.Toolbar> 

The way to achieve with the right order:

    Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);     TextView mTitle = (TextView) toolbar.findViewById(R.id.toolbar_title);      setSupportActionBar(toolbar);     mTitle.setText(toolbar.getTitle());      getSupportActionBar().setDisplayShowTitleEnabled(false); 

Please don't forget to upvote @MrEngineer13 answer !!!

Here is a sample project ToolbarCenterTitleSample

enter image description here

Hope to help somebody else ;)


Comments

Popular posts from this blog

Are Regular VACUUM ANALYZE Still Recommended Under 9.1?

Can Feynman Diagrams Be Used To Represent Any Perturbation Theory?