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

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

Related

Custom Toolbar in Navigation Component

I have used Google's solution for multiple back stacks in Navigation Component. I want to setup a custom AppBar at the activity level(Idea is to have a Toolbar, A FragmentContainerView, and BottomNavigationView)
This works fine except this sets the title of the Toolbar automatically (doesn't call any particular method in the file I have linked as far as I know) .
I want to build a custom toolbar which has a TextView with multiple colors(As in half of the title is black , other half is yellow).
Any solution how to build a custom toolbar in this solution would be greatly appreciated
You can read this document about handle navigation component back stack with appBar configuration
https://developer.android.com/guide/navigation/navigation-ui
and also you can watch this course for make custom toolbar
https://m.youtube.com/watch?v=e5_C9e_gKOM

How do i customise the android toolbar shape

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 exact difference between appbar, toolbar, actionbar ? and when to use them specifically?

what is exact difference between appbar, Toolbar, Actionbar? and when to use them specifically?
I try to find about them but it make me confuse so can any buddy explain to me what is exact difference between them and when to use them or are this are the same name of the single component?
Toolbar
A standard toolbar for use within application content.
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.
2.Actionbar
The action bar is a dedicated piece of real estate at the top of each screen that is generally persistent throughout the app.
It provides several key functions:
Makes important actions prominent and accessible in a predictable way (such as New or Search).
Supports consistent navigation and view switching within apps.
Reduces clutter by providing an action overflow for rarely used actions.
Provides a dedicated space for giving your app an identity.
3.Appbar
The app bar, also known as the action bar, is one of the most important design elements in your app's activities, because it provides a visual structure and interactive elements that are familiar to users. Using the app bar makes your app consistent with other Android apps, allowing users to quickly understand how to operate your app and have a great experience. The key functions of the app bar are as follows:
A dedicated space for giving your app an identity and indicating the user's location in the app.
Access to important actions in a predictable way, such as search.
Support for navigation and view switching (with tabs or drop-down lists).
EDIT
An Action bar is traditionally a part of an Activity opaque window decor controlled by the framework but a Toolbar may be placed at any level of nesting within a view hierarchy. The toolbar provides more feature than ActionBar. A Toolbar may contain a combination of elements from start to end.
Important Note:
Toolbar’s are more flexible than ActionBar. We can easily modify its color, size and position. We can also add labels, logos, navigation icons and other views in it. In Material Design Android has updated the AppCompat support libraries so that we can use Toolbar’s in our devices running API Level 7 and up...
App bar is rather a component name from the design while Toolbar and ActionBar classes are about the implementation. So the question is - what is the difference between Toolbar and ActionBar.
In short, ActionBar is an initial realization of the app bar component and it's bound to Activity. Initially it wasn't even a part of the Activity layout but rather a decoration that's rendered by the system.
Later on Toolbar was introduced, unlike ActionBar, Toolbar isn't bound to Activity, you can place it wherever you want inside your layout and it has a clearer API. To make the adoption of the Toolbar easier, there's setSupportActionBar() method in AppCompatActivity class, so you can use a Toolbar via ActionBar API, and it's where a lot of confusion comes from.
So, which class should you use in a modern Android app? There's no doubts - Toolbar, or its more advanced version from MDC library - MaterialToolbar. But as I mentioned above, a Toolbar can be used as a standalone solution and can also be set as an Activity action bar using setSupportActionBar() method. Take a look at this question Is setSupportActionbar required anymore? and the answer to find more details.
I would say they all are the same container view on the upper end of the application's screen... Toolbar is the name of the Java class defining the features of this box, but on the Android Developers site they're calling it App Bar. As I know, it is an enhanced action bar which was mainly used before Material Design.
EDIT #1: By the way, I suggest to use the App Bar tutorial when creating an application because it is the recommended way to do it.

Toolbar as self-managed ActionBar vs Toolbar as framework managed ActionBar

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

Android ActionBar or Toolbar

I've been reading/youtubing a lot about how I should start my next app and found out that the activities suggested as start activities in Android Studio may contain deprecated elements such as the ActionBar, and that I should rather start from scratch without an activity, and use the newly lollipop-introduced "Toolbar".
My question is: As I need a top bar of a kind (ActionBar/Toolbar) and a navigation drawer in my app, should I invest the time it needs to build the Toolbar opposed to starting with a NavigationDrawer activity from the beggining?
Will using an NavigationDrawer activity (with an ActionBar) mean that I'm staring a new app with old-fashioned kind of top bar?
Very first thing ActionBar is not deprecated and not old style component. It still serves the purpose well. Toolbar only comes into existence when you need more than ActionBar, for instance, you want to increase the height of ActionBar and want to inflate some custom views then ActionBar might not be a right choice. If you just need an Activity with NavigationDrawer, I don't think so you need a ToolBar at all. Just create a new project from scratch and pick NavigationDrawer as a pre-configured option while creating the project and you are good to go. ActionBar is not going anywhere, at least for now. Hope it would help.

Categories

Resources