1

I am putting a ViewPager inside a Fragment.

When I set a fixed height to the ViewPager like layout_height=300dp , the ViewPager with its contents are visible.

But when I change the height of the Viewpager to either fill_parent,match_parent or wrap_content the Viewpager does not get dispalyed.

I am posting my layout below.Please help.

<LinearLayout
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:orientation="horizontal"

    android:weightSum="3" >

    <Button
        android:id="@+id/btn_reviews"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:text="@string/reviews" />

    <Button
        android:id="@+id/btn_description"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:text="@string/Description" />

    <Button
        android:id="@+id/btn_specs"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:text="@string/Specs" />
</LinearLayout>

<android.support.v4.view.ViewPager
    android:id="@+id/pagers"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:background="#000000" >
</android.support.v4.view.ViewPager>

</LinearLayout>   
Emil
  • 2,786
  • 20
  • 24
Tiny
  • 109
  • 12

3 Answers3

0

Try below code ...also make sure when you are giving weight make its child

 for orientation="horizontal" (parent), (child) android:layout_width="0dp"

 for orientation="vertical" (parent),(child) android:layout_height="0dp"

Code XML:

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

<LinearLayout
    android:layout_width="fill_parent"
    android:layout_height="0dp"
    android:orientation="horizontal"
    android:layout_weight=".2"  //change .2 value accordingly
    android:weightSum="1" >

<Button..

<Button..

<Button..

</LinearLayout>

<android.support.v4.view.ViewPager
    android:id="@+id/pagers"
    android:layout_width="fill_parent"
    android:layout_height="0dp"
    android:background="#000000"
   android:layout_weight=".8"    //change .8 value accordingly
   >
</android.support.v4.view.ViewPager>
Santosh Kathait
  • 1,444
  • 1
  • 11
  • 22
0
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">


    <LinearLayout
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal"
        android:weightSum="3">

        <Button
            android:id="@+id/btn_reviews"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:text="b0" />

        <Button
            android:id="@+id/btn_description"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:text="b1" />

        <Button
            android:id="@+id/btn_specs"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:text="b2" />
    </LinearLayout>

    <android.support.v4.view.ViewPager
        android:id="@+id/pagers"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:background="#000000"></android.support.v4.view.ViewPager>
</LinearLayout>
Anitha Manikandan
  • 1,160
  • 7
  • 19
0

When you use weight attribute for parent layout you have to set the child views height or weight to 0dip. If the orientation of your layout is Vertical you have to set layout_height of child views to 0dip so that the android:layout_weight="1" can work. Also, If the orientation of your layout is Horizontal you have to set layout_widht of child views to 0dip. Also, do not you fill_parent on the child because it covers all the page, use instead wrap_content all play with the weights depending on what you want to do. Suppose you want the layout to be vertical and your child lineatlayout to be horizontal try this:

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

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:weightSum="3"
    android:orientation="horizontal">

<Button
    android:id="@+id/btn_reviews"
    android:layout_width="0dip"
    android:layout_height="wrap_content"
    android:layout_weight="1"
    android:text="test" />

<Button
    android:id="@+id/btn_description"
    android:layout_width="0dip"
    android:layout_height="wrap_content"
    android:layout_weight="1"
    android:text="testDescription" />

<Button
    android:id="@+id/btn_specs"
    android:layout_width="0dip"
    android:layout_height="wrap_content"
    android:layout_weight="1"
    android:text="testSpecs" />

</LinearLayout>

<android.support.v4.view.ViewPager
   android:id="@+id/pagers"
   android:layout_width="match_parent"
   android:layout_height="wrap_content"
   android:background="#000000"
   android:layout_weight="3"/>

  </LinearLayout>
Sifis
  • 653
  • 5
  • 18