Dominik Ku Dominik Ku - 3 months ago 15
Java Question

Use a static background image for several fragments

i have a problem. When i swipe from one fragment to another the background swipe with the text, instead of beeing static. How can the backround be static in all the fragments. What is my mistake?

activity_main.xml
:

<android.support.v4.view.ViewPager
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/pager"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@drawable/hintergrund" />


one fragment of four (all have the same code):

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

<TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignParentTop="true"
android:layout_alignParentRight="true"
android:layout_alignParentLeft="true"
android:gravity="center"
android:text="Test"
android:textAppearance="?android:attr/textAppearanceLarge"
android:textColor="#ffdb00"
android:textStyle="italic" />

<TextView
android:id="@+id/textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignParentLeft="true"
android:layout_marginBottom="141dp"
android:layout_marginLeft="18dp"
android:text="Kollegah; King; Omega"
android:textAppearance="?android:attr/textAppearanceMedium"
android:textColor="#ffdb00" />

</RelativeLayout>


New
activity_main.xml
:

<?xml version="1.0" encoding="utf-8"?>
<FrameLayout
android:layout_width="match_parent"
android:layout_height="match_parent" >

<ImageView
android:id="@+id/background"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@drawable/hintergrund" />

<ViewPager
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/pager"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@drawable/hintergrund" />

</FrameLayout>

Answer

You can wrap your viewpager in a FrameLayout with an ImageView.

<FrameLayout 
      xmlns:android="http://schemas.android.com/apk/res/android"
      android:layout_width="match_parent"
      android:layout_height="match_parent" >

      <ImageView 
            android:id="@+id/background"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:background="@drawable/hintergrund" />

     <android.support.v4.view.ViewPager
            android:id="@+id/pager"
            android:layout_width="match_parent"
            android:layout_height="match_parent">

     </android.support.v4.view.ViewPager>

</FrameLayout>

This way the ViewPager will show on top of your ImageView which will be static.