Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ViewPager in android is taking full screen?

In my xml file I have a Linear layout that has a ViewPager for showing images and another Linear layout that contains previous and Next buttons for selecting images.My xml looks like this:

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

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

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:gravity="center"
            android:orientation="horizontal" >

            <Button
                android:id="@+id/goto_first"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="first" >
            </Button>

            <Button
                android:id="@+id/goto_last"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="last" >
            </Button>
        </LinearLayout>

    </LinearLayout>

My problem is that the ViewPager is taking full screen and the Linearlayout with previous next buttons is not showing up because there is no space left to draw this LinearLayout.

I am using Fragments to populate ViewPager with views.The xml file for the fragment view that goes into ViewPager is:

<ImageView
    android:id="@+id/ItemImage"
    android:layout_width="320dp"
    android:layout_height="180dp" />

<TextView
    android:id="@+id/ItemText"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_gravity="left|bottom"
    android:layout_marginBottom="5dp"
    android:textColor="#ffffffff"
    android:textStyle="bold" />
</FrameLayout>

The output i am getting after rendering is in this manner:
1.image ( covering 45% screen height)
2.blank space (covering 45% screen height)
3.Textview (covering rest of the 10% screen height)

The output i want is :
1.image ( covering 45% screen height)
2.Textview (covering 10% screen height)
3.LinearLayout for Buttons (covering rest of the 45% screen height)

like image 749
Ansh Avatar asked Dec 20 '12 07:12

Ansh


4 Answers

Just use a RelativeLayout and use the attributes like `

android:layout_below

until you achieve the position you desire. Relative Layout is your friend in these cases.

like image 86
Otieno Rowland Avatar answered Nov 04 '22 08:11

Otieno Rowland


Use RelativeLayout. Add your buttons to the bottom with alignParentBottom="true". Then add the ViewPager with layout_above="@id/buttons"

like image 44
Shawn Lauzon Avatar answered Nov 04 '22 06:11

Shawn Lauzon


Just give those to viewpager:

 android:layout_height="0dp"
 android:layout_weight="1"
like image 39
zhumingvictor Avatar answered Nov 04 '22 07:11

zhumingvictor


The problem with viewpagers are they don't know the size since each fragment could possibly be a different size. You have to manually set the viewpager's, not wrap_content. I recommend setting a weight to all the items in the outer linear layout.

like image 44
jbenowitz Avatar answered Nov 04 '22 08:11

jbenowitz