Why Google usually put Fragments inside a FrameLayout. I see it in the examples and in the source code of the Google I/O App. I tried to translate the properties of the FrameLayout to the properties of the Fragment and all stayed equal as expected, so why do they use a FrameLayout container?
Probably because that way you can easily make it into a tabbed navigation, since TabHost extends FrameLayout, you can assign the FrameLayout resource to it.
Related
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
After having been introduced to Android development and creating a few basic applications, I have begun splitting activities into fragments for reusability. However one thing that I'm still slightly confused about is how the layout of the main activity(which holds the fragments), is affected by the layouts defined for fragment activities and vice versa.
While I believe that the layouts would affect each other based on definitions of height and width for each fragment, the number of fragments in an activity, etc. However Im not sure if there are other rules I am unaware of and I want to know if the layouts specified for the fragments directly affect the way the way the layout of the main activity displays.
For example, the main activity has a RelativeLayout and contains two fragments which have LinearLayout defined in their own separate layout xml files. Do the LinearLayouts affect the way in which the RelativeLayout would normally display and vice versa?
Fragments are basically similar to other view like RelativeLayout or LinearLayout in some ways. For Example they can also have width and height; Fragments can expand on the basis of size of its children. Rest is up on you, how you design layouts (fragments). Fragment is also similar to Activity in a sense that it has its own xml layout and corresponding java class. Primarily, we use fragments in two ways:
i) One or more fragments capturing separate parts (space) of activity at a time.
ii) A particular fragment in activity which is replaced (add,remove,replace) by many other fragments programmatically
You can use fragments statically in the layout files of your activities and dynamically by adding them in containers which you also define in the layout files of your activities.
The fragment view, will be inflated and be displayed in its container. It all depends on how you define your container. If you make it so it match the width of its parent, the layout of the fragment can have a width as much as the parent of the container. It is like putting views into that container.
I am learning Android Apps development and in this guide, the API makes it clear a Fragment can be managed in at runtime, so far you have the fragment's id or tag.
But this tutorial in the other hand says you can only deal with fragments at runtime IF you don't declare them in the XML.
I find this very confusing...Which one is right?
If you declare a fragment in a XML layout with the <fragment> tag, you cannot replace it dinamically at runtime.
If you declare in xml layout a container(FrameLayout) for different fragments. Using FragmentTansaction you will be able to add a fragment to that container and then replace it or remove it at runtime. As you will see in the Building a Flexible UI lesson:
In order to replace one fragment with another, the activity's layout
includes an empty FrameLayout that acts as the fragment container.
Is it possible to have two fragments - one on the left which controls the one on the right, and dock and undock the left fragment such that on docking the one on the left, only the fragment on the right occupies the screen ? If so how?
You can create a horizontal linear layout as your main layout for your activity and within that layout add two linear layouts that will be the place holders for your two fragments ie leftLinLayout and rightLinLayout. When the activity loads add the two fragments dynamically to the two layouts using a FragmentTransaction.
Within the fragments it is possible to get a reference to other fragments since you have method getActivity() then you call the fragment manager and find the fragment you want to manipulate or remove. However this is not desirable. The better solution would be to build a callback interface that the host activity has to implement so that it becomes a listener to your fragment events and then you let the activity add/remove the desired fragments. A good example of this implementation is the newsreader app in the android developer reference http://developer.android.com/training/multiscreen/index.html .
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.