1

I have an issue about a Fragment this is hidden by ViewPager. I know the fragment is there behind since i experimented setting the ViewPager height to a fixed amount of pixels. Then it shows up i its container right below the viewpager. But What I want is the viewpager to cover all the screen and the when a user clicks on a button in the toolbar the fragment should appear ontop of all other views - that is the viewpager.

So my question - how could I make the Fragment to be shown on top of all other views?

 <?xml version="1.0" encoding="utf-8"?>

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="@+id/linlayout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
>

<include layout="@layout/toolbar"/>

<android.support.design.widget.TabLayout
    android:id="@+id/tabs"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:background="#4f83cc"
    style="@style/MyCustomTabLayout"
    app:tabMode="scrollable"
    >
</android.support.design.widget.TabLayout>



<android.support.v4.view.ViewPager

    android:id="@+id/viewPager"
    android:layout_below="@+id/tabs"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    />

<FrameLayout
    android:id="@+id/container"
    android:layout_width="200dp"
    android:layout_height="200dp"
    android:elevation="10dp"
    android:translationZ="5dp"
    >

</FrameLayout>


</LinearLayout>
java
  • 1,165
  • 1
  • 25
  • 50
  • fragment and view pager never go along well with each other but for a view to be fetched to the front of all views bringChildToFront method can be used check here https://stackoverflow.com/questions/3740100/setting-z-order-of-view-with-bringchildtofront – krishank Tripathi Apr 07 '18 at 10:13
  • Is your code within an **Activity** or **Fragment**? Because you could easily just add the fragment to your **Activity** using the **SupportFragmentManager** and a container view and it would be above everything. – Joey Dalu Apr 07 '18 at 10:21
  • @krishankTripathi tried brinChildToFront(view) without success – java Apr 07 '18 at 10:33
  • @JoeyDalu - I instanciate the fragment in my Activity. Hmmm ... could I possibly inflate a new xml that holds the container (framelayout) ? – java Apr 07 '18 at 10:35
  • @java so this xml is your activity code yes? – Joey Dalu Apr 07 '18 at 10:36
  • @JoeyDalu yes!!! – java Apr 07 '18 at 10:37

1 Answers1

1

You should use a FrameLayout. Since you're using a LinearLayout, your fragment ends up being laid out below your ViewPager, that's what they're for. A FrameLayout would always lay out it child views by stacking them ontop of each other.

Change your XML to this instead:

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

<!-- Your first child-->
<LinearLayout
    android:id="@+id/linlayout"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="vertical"
    >

    <include layout="@layout/toolbar"/>

    <android.support.design.widget.TabLayout
        android:id="@+id/tabs"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="#4f83cc"
        style="@style/MyCustomTabLayout"
        app:tabMode="scrollable"
        >
    </android.support.design.widget.TabLayout>



    <android.support.v4.view.ViewPager

        android:id="@+id/viewPager"
        android:layout_below="@+id/tabs"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        />


</LinearLayout>

<!--Your Fragment Container (Second Child) is now above the views above it-->

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

</FrameLayout>

</FrameLayout>
Joey Dalu
  • 1,113
  • 9
  • 13
  • thanks for your code, I tried it and what happends is that my viewPager is completely freezed, the app does not respond at all - nor it crasches – java Apr 07 '18 at 11:07
  • please check again, I just edited it. Removing **android:clickable="true"** from the Fragment container should solve it – Joey Dalu Apr 07 '18 at 11:12