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.
Related
The structure of the app is one Activity and multiple Fragments(+ 20).
The Activity has a Toolbar with a title and back button that i used in multiple Fragments.
Then there is two more types of Toolbars that i use inside multiple Fragments, one contains an EditText and the other one is an Expandable Toolbar.
An example would be:
Fragment A: Uses Toolbar from Activity. Show that one, hide if another is showing.
Fragment B: Has a Toolbar of it own, now i need to hide the Activity toolbar.
What would be the correct approach to show/ hide the correct toolbar?
Things i tried:
Using onDestinationChangedListener checking the id of the fragment and show/ hide the correct toolbar. The problem here is that it cause the screen to flicker and i have to check for every id.
toolbar.isVisible = destination.id in listOf(id1, id2...)
Hide or show the correct Toolbar from the Fragment. But then i need to set the toolbar on every Fragment.
(requireActivity() as MainActivity).showToolbar(true)
Removing the Activity Toolbar and creating a new one inside every Fragment. But i don't know if the best way and if it is expensive to constantly inflate the Toolbar.
I need to use part of my activity layout in fragmnent, but I dont know how.
If I use overlaying FrameLayout, I will not have access to the elements behind it. And duplicate part of activity_main_layout in several fragments also is not good idea.
A fragment can't host an activity. Instead you can use nested fragments for this purpose. Fragment Inside Fragment
It looks like i need to use fragments with transparent container in the middle
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
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.
I have spent a lot of time searching about the use of the new ToolBar.
I'm trying to develop an app with 1 activity and 3 fragments. The activity has a layout with the toolbar and a container where the fragments will be fit.
The question is: is possible to define the toolbar once in the activity and share it with the 3 fragments or each fragment have to define its own toolbar inside the respective layout?
I'm using appcompat and the activity extends ActionBarActivity.
I have medium skill in Android and appreciate your attention.
Fragment is like a pluggable sub-Activity, and each instance of Fragment must be managed by an Activity, though, each Fragment has its own life-cycle callbacks, but its life-cycle is directly affected and determined by the life-cycle of its host Activity. You can plug as many Fragments into an Activity as you like by using a horizontal-swiping ViewGroup such as the ViewPager , example Facebook app, it has 4 Fragments and they are wrapped in a ViewPager ViewGroup. In one word, each Fragment that you added to a common Activity will have the same UI like ActionBar or Toolbar or whatever. Hope this helps