I'm trying to add a collapsing toolbar in a fragment and a normal toolbar in another. How can I accomplish that when using Navigation Components?
I've already read some articles saying that each toolbar should be part of the fragment layout, but it seems wrong. And I couldn't find anything about it on documentation.
Also, I saw something about NavigationUI.setupWithNavController(Toolbar, NavController). But I can't figure it out how to properly use it.
Related
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
I've been interested in material design and wanted to build a layout for the main page of my app so that it looks like the default template for Scrolling Activity but also has a Navigation Drawer like the default template for Navigation Drawer Activity for easy navigation.
As an example, Google's Play Music app already does this sort of thing.
Have a look:
Scrolling list with a banner and a FAB at the top,
Navigation Drawer, accessed by swiping from over to the left
I've searched everywhere and could find a direct way for implementing such a layout. So I tried doing it on my own and with some changes here and there in the default templates of Scrolling Activity and the Navigation Drawer Activity I was able to achieve something along those lines but not exactly what I was looking for.
I was hopping if someone could present a better way of doing it.
You can still use the DrawerLayout supplied in the Android support library with a FrameLayout and ListView to achieve the Navigation drawer. In the FrameLayout, you will display a Fragment which takes a ScrollView to display scrollable content. The Fragment can also have other widgets that let you customize your layout.
I've got exactly what I wanted, you simply need to include Scrolling Activity's layout inside the layout of Navigation Drawer Activity and it works nicely.
You can use the default templates for these activities given in the Android Studio to save yourself some time or start from scratch.
Let me know if anyone needs some code and I will be more than happy to post it :)
I want to implement a toolbar for navigation which appears at the bottom of almost all activities in my app. It will have a fixed amout of elements (four).
Screenshot:
(certain activities will still have their own toolbar at the top):
In order to make it somewhat reusable, I've created a separate layout file for the toolbar which allows me to include it on multiple activities with:
<include android:id="#+id/toolbar_main" layout="#layout/toolbar_main" />
Every item of the toolbar leads to another activity, which means it acts as a navigation throughout the whole application.
However, as I want to use it on multiple activities, I'm not sure on what would be the correct place for the Java-Code behind the onClick-Events of the menu items. I've seen approaches using a base-class which can be extended by all activities using this navigation toolbar. I've also thought about not using a toolbar at all and creating a fragment for the navigation. I haven't used fragments yet, however, they seem to have their own code-behind class in addition to their design .xml, which would be suitable for the click-Events. Any suggestions? I might add, that I want to center and stretch the navigation bar later on, which seems to be not so easy using a Toolbar with associated menu.
Make A Base activity, which will be extending by all your other activities. Write all your logic related to that toolbar on Base Activity. Thats all.
You could use the TabLayout with ViewPager. Place them in main Activity, and use Fragment for every root screen.
My question is how do I do make the ActionBar transparent (and also the status bar)?
I'm having trouble trying to make use of the AppBarLayout and Toolbars on Android to achieve that.
I need that because I want to create an application that has one main Activity that holds a DrawerLayout and many screens which are mostly fragments. Then, there is some screens where the ActionBar and StatusBar becomes transparent (while still having the hamburger button for the DrawerLayout).
I've tried changing the AppBarLayout and the Toolbar, but it looks weird... It looks like there are some weird margins on the ActionBar.
appBarLayout.setBackgroundColor(ContextCompat.getColor(this, android.R.color.transparent));
toolbar.setBackgroundColor(ContextCompat.getColor(this, android.R.color.transparent));
And here is the result:
And here is it before changing the color/changing it back:
Any ideas on how to achieve the transparency on the ActionBar when some fragments are attached to the main content of the main Activity?
If you want to take a look at the code, here is the repository: Sample Repo.
Okay...
I made a mistake here...
First, all you need to do is remove the paddings on the content_main.xml and set the elevation of the appBarLayout to 0 and the code will work. (You'd need to set it back to its original value if you need the shadows).
I updated the repo to fix that and it should be working now.
But, There is another way to solve this problem using a fragment with its own AppBarLayout/Toolbar and hiding and showing the MainActivity AppBarLayout when you're showing the Fragment inner one.
I'll create a branch in the same repository to show what I mean.
Please, answer/comment with better solutions/ideas if have them.
Repo here.
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