12

I'm building an android application with the following layout.application layout

It's already known that layout_height="wrap_content" doesn't work for the ViewPager.

My question is how to change it dynamically. The content of the ViewPager is inflated from two xml layouts, vp_statistics.xml and vp_campaigns.xml.

here's the xml for the ViewPager

<LinearLayout
            android:id="@+id/topcontent_wrapper"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginTop="10dp"
            android:orientation="vertical" >
</LinearLayout>

<LinearLayout
            android:id="@+id/viewpager_wrapper"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginTop="10dp"
            android:orientation="vertical" >

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

Here's the code for my PagerAdapter

class MyPagerAdapter extends PagerAdapter{

@Override
public int getCount() {
    // TODO Auto-generated method stub
    return 2;
}

public Object instantiateItem(View collection, int pos){
    LayoutInflater inflater =     (LayoutInflater)collection.getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);

    int resId = 0;
    switch(pos){
    case 0:
        resId = R.layout.vp_statistics;
        break;
    case 1:
        resId = R.layout.vp_campaigns;
        break;
    }

    View view = inflater.inflate(resId, null);


    ((ViewPager)collection).addView(view, 0);

    return view;
}

@Override
public void destroyItem(ViewGroup container, int position, Object object) {
    // TODO Auto-generated method stub
    ((ViewPager)container).removeView((View)object);
}

@Override
public boolean isViewFromObject(View arg0, Object arg1) {
    // TODO Auto-generated method stub
    return arg0 == ((View)arg1);
}

@Override
public Parcelable saveState() {
    // TODO Auto-generated method stub
    return null;
}

}

and inside my onCreate() mthod of the Activity class:

MyPagerAdapter adapter = new MyPagerAdapter();
ViewPager viewPager = (ViewPager)findViewById(R.id.viewpager);
viewPager.setAdapter(adapter);
viewPager.setCurrentItem(0);

Is there a way to get the height of the vp_statistics.xml and vp_campaigns.xml layouts and assign it dynamically to ViewPager (@id/viewpager) every time the instantiateItem() object gets called?

duggu
  • 37,851
  • 12
  • 116
  • 113
Andrei Stalbe
  • 1,511
  • 6
  • 26
  • 44

5 Answers5

6
  1. Add a weightSum=2 attribute to the parent LinearLayout, and set orientation=vertical.
  2. Set the top child LinearLayout's layout_weight=1. Also, set layout_height=0dip
  3. Wrap the ViewPager into another LinearLayout with layout_weight=1 and layout_height=0dip
prabhakaran
  • 668
  • 2
  • 10
  • 33
josephus
  • 8,284
  • 1
  • 37
  • 57
2

I answered a similar question here.

The solution extends ViewPager to override onMeasure() to make wrap_content just work for the height.

Community
  • 1
  • 1
cybergen
  • 3,108
  • 1
  • 22
  • 30
0

use 0dp instead of wrap_content, and for dynamically change that

pager = new ViewPager(this);
        pager.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT));
SomeKittens
  • 38,868
  • 19
  • 114
  • 143
virusss8
  • 235
  • 1
  • 4
  • 19
  • 9
    Your code doesn't work, it actually crashes the application. The right code would be `pager.setLayoutParams(new LinearLayout.LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT));` But even with the changed code, it doesn't change the height of the ViewPager. – Andrei Stalbe Jun 06 '12 at 10:00
-1

In order to assign different screen real estate to each linear layout try using the layout_weight parameter. You can reassign this in-code to achieve this 'dynamic' weighting change. Overview here with plenty of Google-able examples of how to achieve the weight layouts in code...

BrantApps
  • 6,362
  • 2
  • 27
  • 60
  • What I'm trying to do in my application is not to assign the top layout and the bottom ViewPager a weight in order to give them half of the screen for each, I'm trying to assign different values to the height of the ViewPager each time the ViewPager page changes, because the layouts that represent the ViewPager pages are different in height. And, for example if the first page has the height of 200dp and I assign the viewpager layout_height="200dp", when changing to the second page (which is 400dp) the bottom 200dp are hidden! – Andrei Stalbe Jun 06 '12 at 10:08
  • Hey Andrei, It sounds like you need to measure the view after it has been drawn and update your view pager width and height after the draw has completed. I embarked on a similar task recently whilst embedding Google Charts into WebViews. Check out [this answer](http://stackoverflow.com/a/4406090/1348379) on the topic. It will help you out. – BrantApps Jun 06 '12 at 14:13
-2

Use RelativeLayout:

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

    <LinearLayout 
        android:id="@+id/layout"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal" >

    </LinearLayout>

    <android.support.v4.view.ViewPager
        android:id="@+id/pager"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:layout_below="@+id/layout" >

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

</RelativeLayout>
franta kocourek
  • 1,289
  • 13
  • 19