beginner beginner - 3 months ago 15
Android Question

android: fragments are displayed over each other

I'm trying to add two fragments on one activity.
The problem is that the fragments are displayed over each other instead of under each other.

My code:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<FrameLayout
android:id="@+id/sample_content_fragment"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<FrameLayout
android:id="@+id/sample_content_fragment2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
/>
</RelativeLayout>


and

protected override void OnCreate(Bundle bundle)
{
base.OnCreate(bundle);

SetContentView(Resource.Layout.Main);

SlidingTabsFragment fragment = new SlidingTabsFragment();
MediaPlayerFragment mpFragment = new MediaPlayerFragment();

FragmentTransaction transaction = FragmentManager.BeginTransaction();

transaction.Replace(Resource.Id.sample_content_fragment, fragment);
transaction.Replace(Resource.Id.sample_content_fragment2, mpFragment);

transaction.Commit();
}


and

public override View OnCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
{
return inflater.Inflate(Resource.Layout.mediaplayer, container, false);
}


How can I fix this?

Answer

Try this!

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  android:orientation="vertical"
  android:id="@+id/layout_select_course"
  android:layout_width="match_parent"
  android:layout_height="match_parent">

   <RelativeLayout
       android:layout_width="match_parent"
       android:layout_height="0dp"
       android:layout_weight="0.5">
         <FrameLayout
            android:id="@+id/sample_content_fragment"
            android:layout_width="match_parent"
            android:layout_height="match_parent" />
   </RelativeLayout>

   <FrameLayout
      android:id="@+id/sample_content_fragment2"
      android:layout_width="match_parent"
      android:layout_height="match_parent"/>