Should we replace Action Bar by ToolBar?

I have been using ToolBar since it was added into Support v7 library. And I think I used it well. But there is a point I can't understand. Why would Google create such a widget? I mean we can do anything ToolBar can do by using ActionBar. Why do we have to use ToolBar? What are advantages of ToolBar over ActionBar if any? Is it necessary to replace ActionBar by ToolBar?

Any tips are appreciated. And thanks in advance.

PS: I found ToolBar is a decandant of ViewGroup. So, how could we use ToolBar like a Layout? Could somebody post some codes of that?


Solution 1:

Yes, you should replace ActionBar with new toolbar.

Reasons

  1. It looks modern and it follows new material design.

  2. Unlike Action bar, toolbar is not part of window decor. You define it and place it just like any other widget... therefore, you have freedom to place it anywhere in the parent layout.

  3. You have freedom to put any widget inside toolbar.

  4. You can define multiple toolbars.

EDIT

What i meant is you can place other widgets (views) inside toolbar.

Create a separate layout file for the toolbar (good for reusability). In my case file name is main_toolbar.xml

<?xml version="1.0" encoding="utf-8"?>
<android.support.v7.widget.Toolbar
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    xmlns:App="http://schemas.android.com/apk/res-auto"
    xmlns:segmentedgroup="http://schemas.android.com/apk/res-auto"
    android:id="@+id/toolbar"
    android:layout_width="match_parent"
    App:theme="@style/ToolbarColoredBackArrow"
    android:layout_height="56dp"
    android:background="@color/primary_color" >

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textSize="@dimen/drawer_fntsize"
        android:text="Title"
        android:id="@+id/lbl_title"
        android:textColor="@color/title_text_color"
        android:layout_gravity="center" />

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

Then include this toolbar in your main layout like this

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

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

    <FrameLayout
        android:id="@+id/content_frame"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_below="@+id/toolbar" />

</RelativeLayout>

As you can see in this example i placed TextView inside the toolbar

Solution 2:

why Android would create such a widget?

Imagine, if you will, an Android tablet.

This tablet is running an app. That app has, in the lower-right corner of the screen, a rich text editor, where one can enter in some comments and format them with bold, italic, etc.

In a desktop or Web app, a typical approach for those formatting options, besides keyboard shortcuts, would be a toolbar, like the one you see above the answer text area here on Stack Overflow.

Prior to Toolbar, Android developers had to either roll their own toolbar, or put the formatting actions in the action bar. While the latter approach is easy, it puts a strain on the user of the aforementioned fictional app, as the user has to keep switching her visual focus from the editor (bottom of screen) to the action bar (top of screen).

Why have we to use ToolBar?

You do not have to use Toolbar. I have ~300 sample apps as part of my book, and at the moment, precisely zero of them use Toolbar. I'll have to correct that at some point, as I have not yet written a chapter on Toolbar.

Is it necessary to replace ActionBar by ToolBar?

No. There is a way to do this, but it is not necessary.

Solution 3:

Toolbar is much more flexible than standard ActionBar, you can add much more tools in a Toolbar (as it extends ViewGroup), and follow Material Design Guidelines.

For example, with a Toolbar, you can do the following :

My Files with big toolbar

A regular ActionBar isn't intended to be expanded this way.

Also, you can better manipulate Toolbar content as you can include it in your Activity layout xml file. Personally, I use a LinearLayout or a RelativeLayout with at the top, the Toolbar, and below, filling the leftover space, a FrameLayout where my Fragments will be added.

Finally, you can place your Toolbar anywhere you want as you set it in your layout file.

UPDATE:

Google released Android Design Support Library. The recommended way to get an extended Appbar is to wrap Toolbar with AppBarLayout, and add additional view such as TabLayout in. To get a FAB over the Toolbar like in this screenshot, you can use CoordinatorLayout to wrap your layout content, and then use anchor attributes on the FAB.