Start a Project

Android Dynamic height ViewPager

Android ViewPager allows the user to flip left and right through pages of contents. ViewPager found in the support library.

If you are using ViewPager for flipping static height layouts then you can simply add height flag as  hardcoded like:

If you want to use ViewPager for flipping the layouts having different heights then this blog will be helpful to you.

By adding WRAPCONTENT height flag for ViewPager will result in the ViewPager invisible this is because the wrap_content flag needs the height from the chield view which is not known in advance.

In this blog, we are using ViewPager for dynamic height views and the height of ViewPager will we updated dynamically at runtime based on the active layout height.

Creating ViewPager with dynamic height

First, create a custom view pager class by extending ViewPager class

Now use this custom ViewPager in your layout xml file

Till now we have created ViewPager with WRAPCONTENT height it will take the maximum height of your child’s view. Now we have to make the changes on the ViewPager adapter for changing the height of ViewPager as per our current active child view for this override the following method inside your ViewPager adapter:

 

Exit mobile version