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);
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!
Related
If there is a toolbar, it is usually passed into setSupportActionBar(). Why?
As per docs
A Toolbar is a generalization of action bars for use within application layouts. While an action bar is traditionally part of an Activity's opaque window decor controlled by the framework, a 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.
But in simple ways, this is a way of telling the Activity that you are interested in using the features related to Toolbar. It will delegate the functionalities related to your defined toolbar. It helps activity to understand the many of the requirements some of them mentioned below.
1) Setting menu options
2) Setting Navigation drawer
3) Setting common Toolbar
4) Setting back button on the top left
5) Using an icon for brand identification
6) Setting a common title or subtitle
7) And many more
If you don't mention for these functionalities by telling the activity using setSupportActionBar then you have to create all this by your self and support them back to the older version. With Toolbar it comes free and you have to just tell a activity to use it will take of supporting different functionalities itself.
if you want to apply your custom toolbar instead of default toolbar then to set toolbar into that specific screen/activity you must be use setSupportActionBar() along with your toolbar. ;)
I used bottom navigation bar on my app. And I also want to use a toolbar to use a search filter.
I know that I need a menu.xml to use a toolbar but I already made menu.xml to use the bottom navigation bar.
If i use a toolbar on my app, do i have to made a new menu.xml?
Though I included more instances in bottom navigation bar's menu.xml, I couldn't do anything
Both don't need to be the same name (menu.xml) and can have different names (eg: bottom_nav_menu.xml for navigation & top_menu.xml for toolbar)
I have an app, which have an action menu item on the right (2).
I need an ulterior menu in the left(1) or a button in the action bar, is that possible?
I attach a image.
enter image description here
You may create a custom toolbar. The standard height of a toolbar is 89dp.
To create a custom toolbar you should make your activity view container RelativeLayout. Then create a custom toolbar view (it may also be RelativeLayout) which height is 89dp. Then add your toolbar to the main container and set alignParentTop to true. So you have a custom flexible toolbar and you can add any view to it.
This way is very comfortable to use any custom views on your toolbar.
I also faced the same situation of customizing action bar. After a lot of searching I got the solution which is Toolbar.
Checkout: https://developer.android.com/training/appbar/setting-up
I think from now on, you should start using Toolbar as the default action bar for your apps. It provides high level of customization and material design features.
I am little bit confuse about standalone toolbar & toolbar as actionbar.
Standalone toolbar can do the work of toolbar as actionbar then what is the need of toolbar as actionbar ?
take a peek at the documentation for android toolbar and notice that toolbar is portable and generic.
A Toolbar is a generalization of action bars for use within application layouts. While an action bar is traditionally part of an Activity's opaque window decor controlled by the framework, a Toolbar may be placed at any arbitrary level of nesting within a view hierarchy.
I'm looking at the toolbar at the top here:
http://cdn1.tnwcdn.com/wp-content/blogs.dir/1/files/2013/10/digg11.jpg
Does anyone know what the icon on the top left is called and where I can find it? Also, is there anyway of adding this to my toolbar without using an ActionBar? For some reason ActionBar is deprecated for me and I have all sorts of trouble following tutorials since they all link the toolbar with ActionBar.
Appreciate any help!
you should use the action bar for that and just lay the background to the same color of your Activity background so it gives the illusion you have no Action bar. but if for some reason you insist in doing it without an Action Bar then use an image view and align it to parent top and to the left side.
And Just change your extends to AppCompatActivity so you can see your Action Bar