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

  • SEARCH
  • Home
  • 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 8253743
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 8, 20262026-06-08T00:58:43+00:00 2026-06-08T00:58:43+00:00

I’m starting using Fragments, and I’ve done like the API guide but … of

  • 0

I’m starting using Fragments, and I’ve done like the API guide but … of course it’d be too easy 😉
When I launch the app, it crashes. After some research I’ve found this post Android fragment is not working
and the response of Stephen Wylie seems to correct the things for Ali, but .. I don’t get it !
Where should I put the FrameLayout ? The “where_i_want_my_fragment” id… it’s whatever I want, right ?
and finally where should I put the Java code ? in my activity (which is displaying 2 fragments by the way) .

Thanks !

Nico

EDIT : Let’s just say what I want for design you would understand better I think.
I want a list fragment on left side which display a list of strings, and to the right side I want a fragment displaying info regarding the selected string in the list. And I wanna be able to swip with fingers movements the right side of my app (I dont know if it s better to swipe fragment or whatever.. It’s the same layout but filled with differents datas)

Ok I just post my code because I really don’t see why it doesn’t do anything.
This is my activity_main.xml

<LinearLayout 
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:baselineAligned="false"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="horizontal" >

<FrameLayout  xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/list_frag"
    android:name="main.courante.c.DateListFragment"
    android:layout_width="fill_parent"
    android:layout_height="match_parent" >
</FrameLayout>

<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/fiche_frag"
    android:name="main.courante.c.fiche_frag"
    android:layout_weight="2"
    android:layout_width="0dp"
    android:layout_height="match_parent" >
</FrameLayout>

</LinearLayout>

Here is my main activity :
public class MainActivity extends Activity {

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    DateListFragment fragment = new DateListFragment();
    getFragmentManager().beginTransaction().add(R.id.list_frag, fragment).commit();
    fiche_freg frag2 = new fiche_frag();
    getFragmentManager().beginTransaction().add(R.id.fiche_frag,frag2).commit();

}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    getMenuInflater().inflate(R.menu.activity_main, menu);
    return true;
}

Here is DateListFragment (no onCreateView because it ‘s automatically generated)

public class DateListFragment extends ListFragment {
private int mposition = 1;
private String[] mListItem = new String[] {
        "Lundi 9 Juilllet",
        "Mardi 10 Juillet",
        "Mercredi maintenant"
};
@Override
public void onCreate(Bundle savedInstanceState) {
    // TODO Auto-generated method stub
    super.onCreate(savedInstanceState);

    this.setListAdapter(new ArrayAdapter<String>
                     (this.getActivity(),R.layout.frag_list_view ,mListItem));
    this.getListView().setChoiceMode(ListView.CHOICE_MODE_SINGLE);
}

and here is fiche_frag :
public class fiche_frag extends Fragment {

@Override
public void onCreate(Bundle savedInstanceState) {
    // TODO Auto-generated method stub
    super.onCreate(savedInstanceState);
}

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

R.layout.checks_matin works fine alone.

I thank you already and again for your help. I’m a beginner in android environnement and I find it difficult to englobe every notions for the UI at once… !!

  • 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-08T00:58:46+00:00Added an answer on June 8, 2026 at 12:58 am

    You understand the basics. The FrameLayout goes wherever you want your fragment(s) to go. I’ve done it where my whole screen was one single FrameLayout before and I swapped up to five different fragments in and out of it.

    If you have two Fragments that you want to display simultaneously, you could make your main layout with two FrameLayouts. However, this means you are locked into having both there all the time (or an empty space if you remove one.

    If you want two fragments that don’t have to be on the screen at the same time you use a single FrameLayout and write code to swap the fragments as required.

    Code to instantiate fragments should always be in the controlling activity (if they are dynamic).

    Without code and a more specific problem, the above is the best answer I can give you.

    EDIT

    An example main layout to put two fragments side by side:

    <?xml version="1.0" encoding="utf-8"?>
    ...
        <LinearLayout
            android:id="@+id/frames"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_above="@id/hline1"
            android:layout_below="@id/horizontalline"
            android:orientation="horizontal" >
            <FrameLayout
                android:id="@+id/leftpane"
                android:layout_width="0px"
                android:layout_height="match_parent"
                android:layout_weight=".4" />
            <FrameLayout
                android:id="@+id/rightpane"
                android:layout_width="0px"
                android:layout_height="match_parent"
                android:layout_weight="1" >
            </FrameLayout>
        </LinearLayout>
    ...
    

    To add your fragment to one of the framelayouts:

    FragmentClass fragment = new FragmentClass();
    getFragmentManager().beginTransaction().add(R.id.leftpane, fragment).commit();
    

    If you want to swap fragments in one of the framelayouts (say the left pane), you can do it like this:

    FragmentClass fragment = new FragmentClass();
    getFragmentManager().beginTransaction().replace(R.id.leftpane, fragment).commit();
    

    I suggested instantiating from the XML because it sounded like you were going to have two fragments and not make any changes. If you are going to swap them in and out, then it would be appropriate to add a tag to each one so you can find them again if you want to display them again.

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

Sidebar

Related Questions

I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
We're building an app, our first using Rails 3, and we're having to build
I am using Paperclip to handle profile photo uploads in my app. They upload
I'm making a simple page using Google Maps API 3. My first. One marker
link Im having trouble converting the html entites into html characters, (&# 8217;) i
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I want to count how many characters a certain string has in PHP, but
I would like to count the length of a string with PHP. The string
For some reason, after submitting a string like this Jack’s Spindle from a text
I've got a string that has curly quotes in it. I'd like to replace

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.