masmic
masmic

Reputation: 3564

set a fragment dynamically

I have a FragmentActivty where its layout is composed by a LinearLayout with some buttons at the half-left, and a empty container FrameLayout at the half-right where I want to insert other activities (Maybe they should be fragments instead of activities).

Depending the button I touch, it will call one activity/fragment and this will be displayed on the container framelayout.

For this I have:

Main.java

public class Main extends FragmentActivity {

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

main.xml

<RelativeLayout     

<!-- Buttons containing layout -->    
<LinearLayout 

    <Button
        ... />
    ...

</LinearLayout>

<!-- Blank space which will contain other activities -->
<FrameLayout
    android:id="@+id/activitycontent"
    ...>    
</FrameLayout>   

This is a example of a activity/frament to set dinamically on the container framelayout:

Content1.java

public class Content1 extends Fragment {

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    /**Inflate the layout for this fragment*/
    return inflater.inflate(R.layout.content1, container, false);
}

@Override
public void onActivityCreated(Bundle state) {
    super.onActivityCreated(state);

So, the question is: How can I define the framelayout as a container for content1 activity/frament, and how I set it? (Remembering that I call to the activty/fragment when I touch the button)

Upvotes: 1

Views: 1343

Answers (2)

Rohit5k2
Rohit5k2

Reputation: 18112

To load the fragment do

public void switchContent(Fragment fragment, boolean addToBackStack)
{
    FragmentManager fragmentManager = getSupportFragmentManager();
    if(fragmentManager == null)
    {
        Log.e("switchContent", "Fragment manager is null, exiting");
        return;
    }
    FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();

    if(fragmentTransaction == null)
    {
        Log.e("switchContent", "Fragment transaction is null, exiting");
        return;
    }

    fragmentTransaction.replace(R.id.activitycontent, fragment);
    if(addToBackStack)
    {
        fragmentTransaction.addToBackStack(null);
    }

    fragmentTransaction.commit();
}

on click of the button call this method as

switchContent(new Content1(), false); 

or

switchContent(new Content1(), true); // if want this fragment to be added in stack

Upvotes: 0

Raghunandan
Raghunandan

Reputation: 133560

On button click

Content1 fragment = new Content1();
FragmentManager fragmentManager = getSupportFragmentManager()
FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
fragmentTransaction.add(R.id.activitycontent, fragment);
fragmentTransaction.commit();

Programmatically add the fragment to an existing ViewGroup(container). At any time while your activity is running, you can add fragments to your activity layout. You simply need to specify a ViewGroup in which to place the fragment.

Upvotes: 3

Related Questions