Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Fragment unintentionally behind other views

I have a container build like this:

RelativeLayout container
--LinearLayout
  --TextView
--LinearLayout
  --Button
  --Button

And i am trying to add a Fragment "ListFragment" into the container showing ABOVE the two LinearLayouts with this:

ListFragment listfragment = new ListFragment();
getFragmentManager().beginTransaction().replace(R.id.contianer,listfragment).commit();

The issue is: It doesnt. It only is displayed above the second LinearLayout, and the first LinearLayout is still visible ABOVE the Fragment.

Here are both xmls:

Container xml:

<RelativeLayout
        android:id="@+id/contianer"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_weight="1"
        android:layout_marginLeft="10dp"
        android:layout_marginRight="10dp"
        android:layout_alignParentEnd="true"
        android:layout_below="@+id/container">

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:paddingBottom="20dp"
            android:paddingLeft="20dp"
            android:paddingRight="20dp"
            android:layout_marginLeft="0dp"
            android:id="@+id/linearLayout"
            android:layout_alignParentBottom="true"
            android:paddingTop="30dp">

            <Button
                android:id="@+id/back"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_weight="1"
                android:text="Button"
                android:textSize="30dp" />

            <Button
                android:id="@+id/nextButton"
                android:layout_width="match_parent"
                android:layout_height="80dp"
                android:layout_weight="0.5"
                android:text="Button"
                android:textSize="30dp"
                android:background="#009688"
                android:textColor="#ffffff" />
        </LinearLayout>

        <LinearLayout
            android:orientation="vertical"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:background="@android:color/white"
            android:elevation="4dp"
            android:id="@+id/vanisherView"
            android:layout_centerHorizontal="true"
            android:layout_margin="20dp">

            <TextView
                android:id="@+id/erklareung"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:textColor="@android:color/black"
                android:background="@android:color/white"
                android:padding="40dp"
                android:textAppearance="?android:attr/textAppearanceMedium"
                android:textSize="35dp"
                android:layout_alignParentTop="true"
                android:layout_alignParentEnd="true" />

        </LinearLayout>


    </RelativeLayout>

The container is wrapped in another Layout, but i think, that doesnt really matter.

Here is the Fragment xml:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@android:color/black"
    android:id ="@+id/viewer_layout">

    <com.woxthebox.draglistview.DragListView
        xmlns:android="http://schemas.android.com/apk/res/android"
        android:id="@+id/drag_list_view"
        android:layout_width="match_parent"
        android:background="@android:color/black"
        android:layout_alignParentBottom="true"
        android:layout_alignParentLeft="true"
        android:layout_alignParentRight="true"
        android:layout_alignParentTop="true"
        android:layout_height="match_parent"/>

</RelativeLayout>

Ive painted the Fragment Black, to see where it is shown, and where not

Thanks for answers and cheers

like image 370
PlatinTato Avatar asked Nov 13 '15 13:11

PlatinTato


1 Answers

Don't try to add a Fragment to a container view that already contains other views.

Instead, place a FrameLayout wherever you want the Fragment to be placed and then add your Fragment to that instead of the parent container.

You can set the required attributes on the FrameLayout to ensure it is placed where you want it within your RelativeLayout.

Example:

<RelativeLayout android:id="@+id/container>"

    <FrameLayout android:id="@+id/fragment_container"/>

    <LinearLayout>
        <Button/>
        <Button/>
    </LinearLayout>

    <LinearLayout>
        <TextView/>
    </LinearLayout>

</RelativeLayout>
like image 63
Kuffs Avatar answered Nov 05 '22 21:11

Kuffs