Android Dynamic height ViewPager

Updated 30 November 2020

Save

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:

 

author
. . .

Leave a Comment

Your email address will not be published. Required fields are marked*


Be the first to comment.

Start a Project


    Message Sent!

    If you have more details or questions, you can reply to the received confirmation email.

    Back to Home