Dynamic adding of multiple fragments - android

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.

Related

Can I use the same fragment multiple times in a single activity?

I have an activity needs to put some custom card fragments in it. These fragments basically look the same, I just need to change some parameter values of each. Now I'm planing to use the same fragment as a 'template' in the page multiple times. But in the fragment's layout xml file there are some fixed item IDs, so if I want to change one of the parameters of the fragment's UI display, I need to use findViewByID() in the fragment's class to set different values of same item ID. Can I do like that?

How are fragment activity layouts affected by main activity layout?

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.

Assistance needed Understanding Android fragment addition during onCreate

I just need help cleaning up some information that doesn't make sense to me, with use cases if possible.
WHAT I UNDERSTAND:
With fragments in android, I understand that if you plan on replacing them you need to have a container view, preferably a FrameLayout, and add the initial fragment to the container during the activities onCreate method. But there is one thing that continues to not make sense to me.
WHAT I DON'T UNDERSTAND AND NEED HELP WITH:
What rules are there regarding where/how the container view is set up, if there are any. Android Developers site makes it look like the container view needs to be it's own XML layout file, but it doesn't say that and I have seen examples on here with FrameLayouts nested inside of your typical layout files, but they are all specific uses and I need to understand the rules of setting a container up.
There are no rules. You just need any ViewGroup -- position and size it however you want. When you add the Fragment into it, it will behave just as if you'd created the Fragment's View manually, and called yourViewGroup.addView(fragmentView).
FrameLayout is typically used just because it makes a good container with no real behavior (you just give it a size and position and let the fragment fill that container).
There's absolutely no need to make the container view its own layout file. In fact, if you want the Fragment to take over the content view of the entire Activity, you could even just add the Fragment to the Activity using the ID android.R.id.content.

Stubs, Fragments or Compound Controls?

I'm new to Android and a confused about the options to group Views together.
Let's say I want to create a UI where I have 2 sections of controls (one with Buttons and one with text + Spinners) that are above each other in portrait mode and next to each other in landscape mode, and the same goes for the stuff within those sections. Obviously I would like to dynamically change this when the user changes from one mode to another.
So, do I use Fragments within Fragments or use them only the outer section and then a Compound Control for the inner elements? Or are Fragments even necessary and I should better stick to something else? What is the best practice here?
Thank you in advance!
In your example you would create two layout files. One that contained your buttons and the other that contained your spinners.
You would create a 3rd layout file for the portrait orientation and use the include tag to include your other layouts within the 3rd layout. Similarly for your landscape layout, you would include your inner UI layouts in that.
You could then use an Activity or a Fragment which would use only the main layouts and assuming they were placed into the correct layout folders, the fragment/activity would load the correct one based on your orientation.
Fragments within Fragments should be avoided unless you have a specific need for it. It works but in practice managing the lifecycle becomes tiresome.

Using same views twice in two Fragments

I have a quiz app with two fragments, for the two quiz modes I have, within a ViewPager. For each fragment, in addition to other things, I dynamically create a list by inflating linearlayouts from XML within a while loop iterating through all the categories. This layout which contains this list is identical in both the modes, it is the rest of the fragment that is different, hence requiring two separate pages.
The problem therefore is that I do this costly process of inflating layouts twice, once within each fragment. I would like to do this only once and use it twice.
Two solutions I've considered:
Using an adapter in the parent activity, accessing this through the
fragments and subsequently using a ExpandableListView.
Problems:
I use checkboxes to select categories. The two fragments deal with clicks differently. With my limited knowledge I have no clue how achieve this. I understand that I can inflate a custom layout but then how do I set a listener in the fragment?
Some of my categories are grouped, others are standalone items. How do I remove the arrow in these items?
.
Using an arraylist in the parent activity. Accessing this in each fragment and adding the layouts to the parent this way.
Problems:
Cannot do this as I'm adding the same view twice so I get an error as the specified child already has a parent. (IllegalStateException)
However this does mean I can easily access the CheckBoxes and iterate through settings oncheckedchangedlisteners
Thank you so much in advance :) If you require any more information or anything else just ask!
I dynamically create a list by inflating linearlayouts from XML
within a while loop iterating through all the categories.
This suggests a ListView.
Using an arraylist in the parent activity. Accessing this in each
fragment and adding the layouts to the parent this way.
This solution will not work from the start, because you have the two fragments in a ViewPager which imposes a different behavior. If the fragments weren't in the ViewPager you could build a mechanism for storing those views in a list followed by dynamically detaching//attaching them from the fragments views. But storing the views in a list(and having them in memory all the time) plus the headache of managing their status isn't worth it.
Using an adapter in the parent activity, accessing this through the
fragments and subsequently using a ExpandableListView.
Why use only one adapter and not two, one for each fragment(so they are independent)? I would also implement the adapter in the fragment itself.
I use checkboxes to select categories. The two fragments deal with
clicks differently. With my limited knowledge I have no clue how
achieve this. I understand that I can inflate a custom layout but then
how do I set a listener in the fragment?
Not quite sure, but if you're talking about accessing the CheckBoxe from a "row" layout and having different behaviors on user actions based on the two fragments, you could very easy implement a base adapter class. That base adapter class will handle the common logic, but you'll also have an abstract method called on a CheckBox action(in the OnCheckedChangeListener). The two subclases(for the two fragments) of that base adapter will each implement that abstract method with their own logic independent from each other.
Some of my categories are grouped, others are standalone items. How do
I remove the arrow in these items?
Like above, this can be done by implementing a smart adapter.

Categories

Resources