what is the difference between standalone toolbar and toolbar as action bar? - android

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.

Related

What is the purpose of setSupportActionBar? What does it do?

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. ;)

Is possible have to different menu in Action Bar? (Kotlin)

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.

Android Toolbar set title explanation

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!

Why did Toolbar replace ActionBar?

As of Android L, we have a Toolbar instead of the ActionBar although its usages seems the same. They even made back compatibility for Toolbar via support library.
What was the reason they replaced ActionBar with a Toolbar?
Toolbar was added because UIs have evolved past the limitations of the ActionBar. The main difference is that the Toolbar can be decoupled from an Activity's opaque window decor and placed in your custom layout somewhere. From there, you have the freedom to do some more interesting things with the Toolbar. One common example is growing or shrinking the height based on scrolling.
From the Toolbar Documentation.
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.
Toolbar supports a more focused feature set than ActionBar. From start
to end, a toolbar may contain a combination of the following optional
elements:
A navigation button. This may be an Up arrow, navigation menu toggle, close, collapse, done or another glyph of the app's choosing.
This button should always be used to access other navigational
destinations within the container of the Toolbar and its signified
content or otherwise leave the current context signified by the
Toolbar. The navigation button is vertically aligned within the
Toolbar's minimum height, if set.
A branded logo image. This may extend to the height of the bar and can be arbitrarily wide.
A title and subtitle. The title should be a signpost for the Toolbar's current position in the navigation hierarchy and the content
contained there. The subtitle, if present should indicate any extended
information about the current content. If an app uses a logo image it
should strongly consider omitting a title and subtitle.
One or more custom views. The application may add arbitrary child views to the Toolbar. They will appear at this position within the
layout. If a child view's Toolbar.LayoutParams indicates a Gravity
value of CENTER_HORIZONTAL the view will attempt to center within the
available space remaining in the Toolbar after all other elements have
been measured.
An action menu. The menu of actions will pin to the end of the Toolbar offering a few frequent, important or typical actions along
with an optional overflow menu for additional actions. Action buttons
are vertically aligned within the Toolbar's minimum height, if set.
In modern Android UIs developers should lean more on a visually
distinct color scheme for toolbars than on their application icon. The
use of application icon plus title as a standard layout is discouraged
on API 21 devices and newer.

Toolbar vs appcompat v7-21 toolbar

What is the difference between the two? what all features are included in toolbar thats not in v7-21 toolbar? What all limitations are there in using v7-21 tollbar in place of toolbar?
If you look at the documentation for the Toolbar and the AppCompat Toolbar you can see that functionally, there aren't any differences between the two. Of course the SDK level required differs between the two and to use the Toolbar you have to call setActionBar(mToolbar); but with the support Toolbar you have to call setSupportActionBar(mToolbar);

Categories

Resources