0

ViewPager does not wrap its height to its content. For my layout I need to set the height dynamically because it is nested in other layouts. Therefore I created a ScrollView as content of the ViewPager and check its height in onCreate method of the Fragment, but the ScrollView has always the same size. How can I wrap height of the ViewPager to fit to its content?

        final ScrollView sv = (ScrollView) v.findViewById(R.id.sv);

        ViewTreeObserver vto = sv.getViewTreeObserver(); 
        vto.addOnGlobalLayoutListener(new OnGlobalLayoutListener() { 
            @Override 
            public void onGlobalLayout() {

                int height = sv.getMeasuredHeight();
                //always same height independent of content height
                //need to resize ViewPager



                ViewGroup.LayoutParams params = (ViewGroup.LayoutParams) mPager.getLayoutParams();
                params.height = height;
                mPager.setLayoutParams(params);
            } 
        });
user1324936
  • 2,187
  • 4
  • 36
  • 49
  • This is a duplicate of http://stackoverflow.com/questions/8394681/android-i-am-unable-to-have-viewpager-wrap-content which have a more appropriate answer. – 3c71 Jul 30 '14 at 17:50

1 Answers1

-2

Tried a lot of things, finally got it. -initially set the ViewPager to a big height to fit all possible content and not to make its height reduced -had to nest 2 LinearLayout to get the correct height. hth

user1324936
  • 2,187
  • 4
  • 36
  • 49
  • 12
    could you post some sample code of your layout with your fix? – MOST2K2 Oct 12 '12 at 13:51
  • ViewGroup.LayoutParams params = (ViewGroup.LayoutParams) mPager.getLayoutParams(); params.height = height; mPager.setLayoutParams(param.heigt); – user1324936 Apr 04 '14 at 20:08