I'm in the process of migrating an app from AppCompat-v20 to AppCompat-v21. As part of that process, I'm trying to move away from the Action Bar, and replace it with a Toolbar so we can take advantage of all the new Material design goodness.
Previously, we had defined all of the information about the AB's displayOptions in a style, and then we set that style as the actionBarStyle in the Activity's theme. However, now that I'm extending Theme.AppCompat.NoActionBar those properties aren't respected anymore, which makes sense, because my theme extends Theme.AppCompat.NoActionBar. At the same time, I would still like to have those displayOptions be applied in the same places they were before (i.e. I'd like to be able to specify whether I want to use a logo or show home as part of the theme).
I know I can probably do this by putting displayOptions onto the theme and parsing them myself, but I'm wondering if there's any support within AppCompat for Toolbars to handle this logic automatically.
With the Toolbar what you have to remember is that it is basically just a view group so you can customize it a lot easier and more so than you could with the ActionBar. That being said, you can use styles to alter the appearance of the Toolbar however there are methods for settings things like title/logo/navigation.
Here's an example of styling your Toolbar:
<android.support.v7.widget.Toolbar
android:layout_height="wrap_content"
android:layout_width="match_parent"
android:minHeight="?attr/actionBarSize"
app:theme="#style/ThemeOverlay.AppCompat.ActionBar" />
If you are using ActionBarActivities and Fragments then you can call the following to let Android know to use your Toolbar as the supportActionBar. This means that all of your calls to getSupportActionBar() will still be valid.
Toolbar toolbar = (Toolbar) findViewById(R.id.my_awesome_toolbar);
setSupportActionBar(toolbar);
The new Material Design guidelines have started to move away from using Logos but if you insist on settings one you can like so:
toolbar.setLogo();
Your question was slightly vague so for more info checkout this post by Chris Banes and Nick Butcher and then the documentation.
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 am newbie in android app developing and learning android UI design.I am trying to make a toolbar like below image.But i don't now what is the xml code for below image toolbar.
Two options. Personally I'd go for the first.
Overriding the view
First approach is overriding the default toolbar / action bar. This has the advantage of being able to use AndroidX navigation, automatic back stack navigation, etc, with the disadvantage of a bit less control.
All you need to do is enable custom views on your action bar, apply a custom view, hide the default title, then set the elevation to 0 so there's no shadow. This will be something like:
ActionBar actionBar = getSupportActionBar();
actionBar.setDisplayShowCustomEnabled(true);
actionBar.setCustomView(R.layout.your_custom_layout);
actionBar.setDisplayShowTitleEnabled(false);
actionBar.elevation = 0f;
R.layout.your_custom_layout can then be whatever layout you want, so long as it fits in the same space. I've written a full tutorial on this previously, as well as a sample project in Kotlin.
Defining your own
The second approach is ignoring the built in toolbar / action bar, and just rolling your own. This gives you full control, and allows setting the bar to any height, but you lose all built in functionality. For this, just use themes without a toolbar, and draw your own bar (perhaps in an activity that then swaps in fragments).
What is the difference between the two following approaches of having an action bar in an Activity?
Manually use Toolbar
Set the app's theme to Theme.AppCompat.NoActionBar and then include a Toolbar manually into the layout like this:
<android.support.v7.widget.Toolbar
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/toolbar" />
In code then use AppCompatActivity's setSupportActionBar() method to use the toolbar.
Use a theme with action bar
Set the app theme to Theme.AppCompat.Light.DarkActionBar (or any other theme with action bar) and don't include a separate Toolbar into the layout. In the debugger I can see that the resulting action bar is an android.support.v7.app.WindowDecorActionBar. Checking the source code of it, I find that it at least "knows" about Toolbar.
My questions are:
In both cases I can use the Activity's callbacks to populate the actions, to get information about a tapped action/option etc. Also the visual appearance seems to be identical. Which makes me wonder...
What is the correct approach?
Is the theme based approach also using the Toolbar or is it something else?
Advantages of either approach?
Alright, I am trying to understand what I would lose if I use Toolbar as a self-managed ActionBar and not use setSupportActionBar.
AFAIK, all that ActionBar does is, provide placeholders for logo, navigation and menu items and also let Fragments add/customize the menu items.
The same functionality could be achieved using Toolbar.setLogo(), Toolbar.setNavigationIcon(), Toolbar.setNavigationOnClickListener() and Toolbar.inflateMenu() apis. Of course, directing the menu handling logic to Fragments might be lost but I think that is not that big of deal if the Activity knows which Fragment is on top and changes the menu items accordingly.
I am trying to make sure :
If I can achieve every ActionBar capability just by using Toolbar and MenuItems (and not using setSupportActionBar()).
Is it ok to not have an exhaustive knowledge of ActionBar apis. Some of the ActionBar apis are very confusing. Using setHomeAsUp api to show hamburger icon, back icon etc., doesn't feel right. If someone starts learning android today, do they even need to understand the framework's ActionBar apis?
Update : In the article Android Design Support Library under section CoordinatorLayout and the app bar, I learnt that the new paradigm app bar is a replacement for the action bar paradigm. I think action bar will soon be deprecated and we should get used to the new app bar paradigm which covers the new material design user experiences.
Yes you can use achieve similar capabilities of ActionBar in Toolbar.
Mainly the difference lies is Toolbar becomes part of View so we can much more fun playing with them to get Scrolling Techniques
You can use Toolbar separately as View & perform ActionBar alike functionalities. For example, in one of my app I use 2 Toolbar one which is set to setSupportActionBar() while other is just used for some other functionalities.
Conclusion: Well it depends upon your requirements if you want to use Toolbar as self or framework. None the less you can use it as both.
I hope this answers your question well.
To me you are right.
From AppCompatDelegate source code, method setSupportActionBar(),
When set to a non-null value the getSupportActionBar() method will return
an ActionBar object that can be used to control the given toolbar as if it were
a traditional window decor action bar. The toolbar's menu will be populated with the
Activity's options menu and the navigation button will be wired through the standard
android.R.id.home menu select action.
So these are most, if not all, the benefits you will have. As you said, it is easy to implement navigation and menu inflating through Toolbar APIs. However, I don't see what you would gain by not calling setSupportActionBar().
YES YES YES
using setSupportToolBar() is the same old Actionbar the only reason ToolBar is ToolBar is for versatility,same as Fragments is to Views, all lies on how you implement stuff, and also the old Actionbar is kinda boring and much restricted as to Toolbar
After Google introduced Material Design, I have heard about a new widget class called Toolbar.
What is the Toolbar, and what is the exact difference between ActionBar and ToolBar?
I found a good explanation from Android Developers Blog post.
In this release, Android introduces a new Toolbar widget. This is a generalization of the Action Bar pattern that gives you much more control and flexibility. Toolbar is a view in your hierarchy just like any other, making it easier to interleave with the rest of your views, animate it, and react to scroll events. You can also set it as your Activity’s action bar, meaning that your standard options menu actions will be display within it.
Yes, we, Android developers, needed more control over ActionBar, right? And Toolbar is just for it.
In other words, the ActionBar now became a special kind of Toolbar. This is an excerpt from Google's official Material Design spec document.
The app bar, formerly known as the action bar in Android, is a special kind of toolbar that’s used for branding, navigation, search, and actions.
More details like how to use Toolbar as an ActionBar are included in above blog post.