3

I have my app Toolbar here:

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

When i want to set the screen title, i have to do the following:

ActionBar bar = getSupportActionBar();
if (bar != null) {
    bar.setTitle(SCREEN_TITLE);
}

My questions is, why when calling the toolbar.setTitle() is not working. But instead i need to get the action bar first and then i can set the title?

P.S. In the other hand, I can set the Toolbar icon normally without getting the action bar as so:

toolbar.setNavigationIcon(R.drawable.ic_arrow_back_white);
TareK Khoury
  • 12,721
  • 16
  • 55
  • 78
  • possible duplicate of [In android app Toolbar.setTitle method has no effect – application name is shown as title](http://stackoverflow.com/questions/26486730/in-android-app-toolbar-settitle-method-has-no-effect-application-name-is-shown) – Semyon Danilov Aug 27 '15 at 12:18
  • not a duplicated. I am not asking how to set the title. Code is working fine for setting the title. I need an explanation about how and why this code logic is needed. – TareK Khoury Aug 27 '15 at 12:29
  • look through the answer for that question and you will find the explanation. – Semyon Danilov Aug 27 '15 at 12:34

1 Answers1

2

A Toolbar is a generalization of action bars for use within application layouts.

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

You have freedom to put any widget inside toolbar.

You can define multiple toolbars.

Toolbar may be placed at any arbitrary level of nesting within a view hierarchy. An application may choose to designate a Toolbar as the action bar for an Activity using the setActionBar() method.

As Toolbar is generalization of ActionBar. you have to first set Toolbar as ActionBar then you can use all method of ActionBar.

EDIT:

Here is Link that explains Toolbar concept.

Here is bug link reported in android issues: In issue they suggest that Once you call setSupportActionBar(Toolbar), the Action Bar is then responsible for handling the title, meaning that you need to call getSupportActionBar().setTitle(...) to set a custom title. It also gives explanation about why toolbar.setNavigationIcon(R.drawable.ic_arrow_back_white); works.

I hope it helps!

Rajesh Jadav
  • 12,801
  • 5
  • 53
  • 78
  • Your answer kinda helps me understand better. But the question is, why i can use toolbar.setNavigationIcon() to change the back icon but cannot use toolbar.setTitle(). – TareK Khoury Aug 27 '15 at 12:40
  • Here is bug link reported in android issues: https://code.google.com/p/android/issues/detail?id=77763. In issue they suggest that Once you call setSupportActionBar(Toolbar), the Action Bar is then responsible for handling the title, meaning that you need to call getSupportActionBar().setTitle(...) to set a custom title. – Rajesh Jadav Aug 27 '15 at 12:57
  • I guess it's a design pattern issue from Google, but i got the idea! thanks – TareK Khoury Aug 27 '15 at 13:01