Pages

19 Jan 2014

WRAP_CONTENT doesn't work with ViewPager?!

Lately, I have been working on an Android project, and when I was making a tutorial activity for it, I got into a problem with ViewPager.

I wanted to use ViewPager and ViewPagerIndicator to show content of the tutorial activity, the ViewPager had a rounded corner background, and each page of it contains an ImageView which is a part of the tutorial. And the problem is although I set layout_width for the ViewPager as wrap_content but it always acts like match_parent?!

After googling for a while, I saw some guys got into the same problem, for example: http://stackoverflow.com/questions/8394681/android-i-am-unable-to-have-viewpager-wrap-content. They proposed some solutions but none of them matches my need, but this anwser gave me an idea.

Because the rounded corner background and all image on pages of the ViewPager have the same size, and as the answer says, it's possible to set a fixed size for the ViewPager, so the solution for my case is just set size of the ViewPager as size of the rounded corner background.

Check out following gist to get a clearer idea about it.