Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9174277
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T16:43:32+00:00 2026-06-17T16:43:32+00:00

I am trying to show multiple fragments on the one screen by creating them

  • 0

I am trying to show multiple fragments on the one screen by creating them programmatically. I am able to do this no problem by including each fragment into the activities layout file. However when I try and do it programmatically Im confused.This is what I have so far for two fragments on a screen.

Main Class:

public class MainActivity extends FragmentActivity {

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

        FragmentOne one = new FragmentOne();
        FragmentTwo two = new FragmentTwo();
        FragmentThree three = new FragmentThree();

        FragmentTransaction ft = getSupportFragmentManager().beginTransaction();
        ft.add(R.id.imOne, one, "fragmentone");
        ft.add(R.id.imTwo, two, "fragmenttwo");
        ft.add(R.id.imThree, three, "fragmenthree");
        ft.commit();

    }
}

Fragment One:

public class FragmentOne extends Fragment {

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        View view = inflater.inflate(R.layout.frag_one, container, false);
        return view;
    }

}

Fragment Two:

public class FragmentTwo extends Fragment{

    public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        View view = inflater.inflate(R.layout.frag_two, container, false);
        return view;
    }

}

The layout files for my two fragment classes just contain a simple TextView.
And then the activity_main.xml layout file for my main class:

    <?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="horizontal" >

    <FrameLayout
        android:id="@+id/imOne"
        android:name="com.david.twofragexample.FragmentOne"
        android:layout_width="0dp"
        android:layout_height="match_parent"
        android:layout_weight="2" />

    <FrameLayout
        android:id="@+id/imTwo"
        android:name="com.david.twofragexample.FragmentTwo"
        android:layout_width="0dp"
        android:layout_height="match_parent"
        android:layout_weight="2" />

    <FrameLayout
        android:id="@+id/imThree"
        android:name="com.david.twofragexample.FragmentThree"
        android:layout_width="0dp"
        android:layout_height="match_parent"
        android:layout_weight="2" />

</LinearLayout>

When I try and do it programatically I get confused as to what should be in my activity_main.xml file. So from the Android Developer Guide(http://developer.android.com/guide/components/fragments.html) it says to use

FragmentManager fragmentManager = getFragmentManager()
FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
FragmentOne fragment = new FragmentOne();
fragmentTransaction.add(R.id.fragment_container, fragment);
fragmentTransaction.commit();

So when I add this code to my onCreate method what changes do I have to make to my activity_main.xml file to do this programmatically?I don’t see where R.id.fragment_container comes from. How can I determine where on the screen my new fragment will go?Thanks

EDIT: I am developing this on a tablet and not a phone.This has now been solved and the above code will add three fragments programmatically.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-17T16:43:32+00:00Added an answer on June 17, 2026 at 4:43 pm
    <?xml version="1.0" encoding="utf-8"?>
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="horizontal" >
    
        <FrameLayout
            android:id="@+id/list"
            android:name="com.david.twofragexample.FragmentOne"
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_weight="2" />
    
        <FrameLayout
            android:id="@+id/viewer"
            android:name="com.david.twofragexample.FragmentTwo"
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_weight="2" />
    
    </LinearLayout>
    

    In your Activity:

    FragmentOne one = new FragmentOne();
    FragmentTwo two = new FragmentTwo();
    
    fm = getSupportFragmentManager();
    fm.beginTransaction().replace(R.id.list, one, "fragmentone").commit();
    fm.beginTransaction().replace(R.id.viewer, two, "fragmenttwo").commit();
    

    I suggest you study this:

    http://developer.android.com/reference/android/app/Fragment.html

    and fully understand the fragment lifecycle, etc. The way I show is quick and dirty, but not neccesarilly best.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am trying to show/hide multiple divs with the one button, but I am
I am trying to show multiple points on maps. I tried DisplayMap *ann =
I am trying to create an android app which will show multiple web urls
HI, Im trying to show a ProgressDialog while the activity is loading. my problem
I am trying to get show hide to work on multiple objects but I
I am trying to get multiple columns and show duplicate ItemCodes form a table.
I'm trying to use the Fancy Zomm jQuery plugin to show multiple images in
I hope someone can assist please. I have a Fragment hosting multiple list fragments
I am trying to insert multiple images in one editText. I have used following
I want to show multiple image on a canvas. I need to position them

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.