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 8644109
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T12:12:54+00:00 2026-06-12T12:12:54+00:00

I am trying to create an activity with 2 Fragment First fragment show a

  • 0

I am trying to create an activity with 2 Fragment

  • First fragment show a list of galleries using a LisFragment
  • Second fragment show the preview images of the gallery selected in the first fragment

When I declare the fragment in the view it works. But now I want to manage fragment dynamically :

GalleriesFragment gfs = new GalleriesFragment();
GalleryFragment gf = new GalleryFragment();
FragmentTransaction ft = getFragmentManager().beginTransaction();
ft.add(R.id.layout_m,gfs);
ft.add(R.id.layout_m,gf);
ft.commit();

My Layout:

<?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" 
    android:id="@+id/layout_m">
</LinearLayout>

When doing this only the first fragment added is shown, I tried to invert their order when adding fragment…

When I was working on the layout I could set my fragment width with:

android:layout_width="400dp"

Now I have 2 questions :

  1. How can I make my 2 fragments to display?
  2. How can I set the width (programmatically?) of the 1st fragment and the second to fill the remaining width?

Thanks for your help!

PS:

GalleriesFragment doesn’t have a layout and is not overriding onCreateView, but has an ArrayAdapter returning view like this for each rows:

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

    <ImageView
        android:id="@+id/idimage"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginBottom="10sp"
        android:layout_marginLeft="10sp"
        android:layout_marginTop="10sp"
        android:contentDescription="preview"
         >
    </ImageView>

    <LinearLayout
        android:id="@+id/LinearLayout01"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical" >

        <TextView
            android:id="@+id/texttitre"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:layout_marginLeft="5sp"
            android:layout_marginRight="7sp"
            android:layout_marginTop="10sp"
            android:background="@layout/roundcorner"
            android:gravity="center"
            android:text="Title here" >
        </TextView>

        <TextView
            android:id="@+id/itemdescription"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:layout_marginLeft="10sp"
            android:layout_marginRight="10sp"
            android:text="description" >
        </TextView>

    </LinearLayout>

</LinearLayout>

GalleryFragment :

<?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="vertical" >

    <Gallery
        android:id="@+id/gallery"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" />

</LinearLayout>

Update 2 :

I added an onCreateView in my GalleriesFragment class :

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {
        FrameLayout.LayoutParams lparams =  new FrameLayout.LayoutParams(400, LayoutParams.MATCH_PARENT);
        container.setLayoutParams(lparams);
        return super.onCreateView(inflater, container, savedInstanceState);
}

This fixes the first fragment’s width, but the second one is still not showing…

  • 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-12T12:12:55+00:00Added an answer on June 12, 2026 at 12:12 pm

    How can I make my 2 fragments to display ?

    Your current approach will make the first fragment supplied to the add method to take all the space of your container layout. One solution is to use a layout that reserves space for each of the two fragments that get added, something like this:

    <?xml version="1.0" encoding="utf-8"?>
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:id="@+id/layout_m"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="horizontal" >
    
        <FrameLayout
            android:id="@+id/frame1"
            android:layout_width="100dp"
            android:layout_height="match_parent" />
    
        <FrameLayout
            android:id="@+id/frame2"
            android:layout_width="match_parent"
            android:layout_height="match_parent" />
    
    </LinearLayout>
    

    Then in code:

    ft.add(R.id.frame1, gfs);
    ft.add(R.id.frame2, gf);
    ft.commit();
    

    How can I set the width (programmatically ?)of the 1st fragment and
    the second to fill the remaining width?

    Fragments are not just ordinary views but one way to do that is to use your single LinearLayout layout file and set the LayoutParams of the fragment’s view in onActivityCreated:

    getView().setLayoutParams(new LinearLayout.LayoutParams(100, LinearLayout.LayoutParams.WRAP_CONTENT));
    

    Of course this will make your fragments to be tied to the parent container, also I don’t know how well this will work in the overall fragment system.

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

Sidebar

Related Questions

I'm trying to create a list fragment activity like this : public class FluxListeFragment
I'm trying to create a custom activity with multiple connectors using WWF 4.0. Could
I'm trying to create an activity indicator in my app. I'm using a storyboard
I am trying to create a searchable activity however when I get to this
I'm trying create a bot which automatically likes Facebook posts. Using Mechanize I can
Ok so I am trying create a login script, here I am using PHP5
I'm trying to create a camera activity for taking photos to be OCR'd. Here's
I'm trying to create a setup activity for my Android application which my users
So what I am trying to create is an activity that displays an image
I am trying to create a PreferenceScreen but i want the Activity design like

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.