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.
Related
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.
I am building an Android project with lots of fragments that each have different action bars.
The recommended approach I've seen is to use a main layout that includes the toolbar, and then each fragment interacts with the action bar of the main layout.
To me, it seems to make more sense to just include a different toolbar separately in each fragment layout, but this goes against the approach that I see recommended everywhere. In fact, I find it quite messy to change the action bar attributes each time the fragment is started (the fragments are kept in memory). Are there any downsides to including a toolbar in each fragment individually?
In a project I'm working on I have just one main action bar for every fragment, and every time the user switches to another fragment I change the properties of it basing on the fragment id, to me this is the cleanest and most simple solution.
Is it possible to share navigation graph between two (or more) containers?
What I would like to achieve is to have two containers (NavHosts):
one smaller that is above bottom navigation and below toolbar
second is full screen.
I would like to have one navigation graph, because fragments from one container can trigger actions from other one. Otherwise it (having more navigation graphs that interacts with each other) will become untidy and problems created by library will surpass advantages.
Seems like you cannot, however you can have nested navigation: i.e. wrap your toolbar navigation inside of fullscreen (of vice versa) and playing with
navController.popBackStack(<id of nested item>)
Yes, it is possible. Take a look at this sample. This is an implementation of Android Navigation component with bottom navigation view with separate history for each tab. You can use the concept and change it the way that fits your app
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 set a navigation bar ( with two buttons and a textview ) common for all activities of my application. I know it can be done by including a common layout file into each activity's layout file. But i have to use slide transition animation when a activity changes to another. Now if i follow the common layout file including process, then the navigation bar will also slide in or out with the activity's view where i want my navigation bar to be fixed when activities view slides in or out. Besides, the two buttons of the navigation bar should be set as i can hide them by using code to some activities. So now i need help badly on the problem, please give me a solution/suggestion/clue on the problem.Thanks in advance .....
Perhaps Fragments may be what you are looking for in this scenario.
Or thinking about it further maybe ViewFlipper with multiple layouts could also help in this case.