Waypoint
Waypoint

Reputation: 17743

Dynamic adding of multiple fragments

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?

Upvotes: 1

Views: 1714

Answers (1)

NikhilKesari
NikhilKesari

Reputation: 36

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

  1. Are all the fragments of the same size?
  2. 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.

Upvotes: 1

Related Questions