0

I have this code in my main activity for the fragments.

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:id="@+id/mainView"
    android:orientation="vertical"
    android:layout_marginBottom="10dp">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical"
        android:id="@+id/mainView2"/>


</LinearLayout>

And i use this code in my main activity to inflate the fragments

FragmentManager fm = getSupportFragmentManager();
        fm.beginTransaction().add(R.id.mainView,new MainViewFragment())
                .add(R.id.mainView2,new SecondProductLayout())
                .commit();

But i'm still getting the 1st fragments view, what am i doing wrong and how can i fix this problem?

Kristjan
  • 33
  • 4

3 Answers3

0

split the activity screen two parts inflate your fragment in your activity

vinay
  • 149
  • 7
0

You got to split your layout into two parts using weight and inflate two fragments as usual we do.

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
             xmlns:tools="http://schemas.android.com/tools"
             android:id="@+id/frameContainer"
             android:layout_width="match_parent"
             android:layout_height="match_parent"
              android:orientation="vertical"
             tools:context="com.android.buffer.exampleapplication.MainActivity">

    <FrameLayout
        android:id="@+id/flFragment1"
        android:layout_weight="1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"/>

      <FrameLayout
          android:id="@+id/flFragment2"
        android:layout_weight="1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"/>

</LinearLayout>

java code to inflate the fragment.

getFragmentManager().beginTransaction().replace(R.id.flFragment1, FragmentA.getInstance(msg)).commit();
            getFragmentManager().beginTransaction().replace(R.id.flFragment2, FragmentA.getInstance(msg)).commit();
vikas kumar
  • 10,447
  • 2
  • 46
  • 52