In my app, I am using a android.support.v7.widget.Toolbar and I have various fragments with different items it the toolbar.
What is the correct way to implement that?
Should I use a different android.support.v7.widget.Toolbar for each fragment with it's child views, or should I use one main toolbar and hide/make visible elements? The latter seems messy and ungly.
ToolBar is basically just a simple ViewGroup so you can treat it this way. You can create a fragment holder in the ToolBar and then just replace it with any Fragment you want.
You can also put different views in it a hide/unhide them according to your logic. Either way is find, depends on your app needs.
Related
I am stuck with a problem where I have designed a tab layout that I have used as a library in the current project which has a Recycler View. I want to show the tab layout on top and the recycler view under the tab layout.
For reference in Whatsapp, there is tab layout under which there is a recycler view that shows all the messages.
Problem is that I am confused about how to combine both activities together so the user can use the tab layout.
If anyone knows any solution please help!!
Use single-activity pattern. This pattern means that you need to use Fragments in a single activity (MainActivity, etc). Additional tip, you can use ViewPager2 with TabLayout for your Fragments.
Activities can't be composed into one, cause it designed so.
This is the toolbar I want to have at the bottom of most of my activities. Basically, it is a total of four button images that are in a linear layout. This toolbar is used to move between the four root activities. How do I have this exact set of views on all of my activities so that I do not have to copy paste and mess with id's, etc.?
Android already has a tablayout you should use
The best way is to use tab activity or fragment.
You have a main activity in which your desired toolbar is at the bottom of it.
Then by each click on toolbar items, only the container view changes.
First at all, i have a main activity with a toolbar. This toolbar is used for all my fragments.
For each fragment i have some difference menu items. And the GUI for it quite complex.
I know there is the way that create menu for each fragment then use setHasOptionsMenu(true). But, when use the menu like that, it too hard to custom all the menu item as i want.
So, i created a toolbar with many layouts. Set it "gone" by default. When adding a fragment i going to set the corresponding layout visible and hide all other.
It solved my problem but i'm not satisfied with this solution. And i wonder if you have any better solution for it. Please share!
I've answered to something similar to what you need... take a look over here Using different layouts under a Toolbar
I am using Android-ObservableScrollView library. Everything works great, but I have activity for holding fragments, so all views are encapsulated in the fragment. In activity there is only FrameLayout for holding fragments.
So I need to use Toolbar in my application, I have several ideas how to implement this.
Use Toolbar in activity, in this case my layout will have FrameLayout and Toolbar. In this way I have communicate with activity whenever I need to do something with toolbar, I can also obtain it by using getSupportedActionBar() from fragment.
Use Toolbar inside fragment (in its layout) setting in each fragment view creation. And each time I change fragment I have to add new Toolbar to the activity. In some fragment I am going to have different toolbars but not in all. Is it good approach to store Toolbar inside fragment.
The problem that I can see in using second approach, if there will be more than one fragment on the screen there will be also several toolbars.
Please suggest what will be the right way in this case.
Thank you.
You should use first method.
While using first method will have less problem then second one because in second method you have to define toolbar for many times which is not good programming.
I am thinking of an app in which I will be programatically able to display some fragments according to metadata I have stored somewhere. Up to know, I have been able to find out, that each fragments lies in corresponding FrameLayout, or especially, when I create activity with one FrameLayout, I am able to store there only one Fragment at a time, no matter what kind is it. Problem is, however, with situation, when my metadata declares I have to put 3 fragments into my activity, while there is only one FrameLayout.
I see two possible solutions:
1) making several FrameLayout and in final stage, some of them will be used or not
2) somehow join multiple fragments to fit into one available FrameLayout
I don't like solutuion 1) and I don't know how to achieve 2). How to you see it? Is it possible to dynamically add multiple Fragments into activity with one Frame Layout?
In your situation where you require more number of Fragments to be added to your screen avoid using the FrameLayout, you will not be able to achive it. There are several factors that define how you layout multiple Fragments
Are all the fragments of the same size?
Should the fragments be place in a particular order?
First Create an empty parent layout (Layout class is left to your choice except FrameLayout). Next define your child view for your parent layout using a seperate xml file this file must contain your fragment place holder. Using the inflator inflate your child view and assign a Fragment to the inflated layout and eventually add it to your parent layout, through this way you can active what you are looking for. Just remember as you inflate your layout do mention its layout size so that you achive the kindly of layout you want.