How to make ViewPager square sized to screen width?

android, android-viewpager

Solution

You can use `Display` to get screen size in pixels.

Display display = getWindowManager().getDefaultDisplay();
Point size = new Point();
display.getSize(size);
int width = size.x;
int height = width;

Then create `LayoutParams` and set them to your `ViewPager.`

LayoutParams lp = new LayoutParams(width,height);
mViewPager.setLayoutParams(lp);

You might also want to check which value is less, width or height.

Adapted from here

Problem

I have a viewpager that I want to take up the entire width of the phone screen, however much that is. I want the height of the ViewPager to then match the width so the whole thing is square, but cannot figure out how to do this. I have tried combinations of weight, fixed height, match_parent, wrap_content but nothing seems to achieve this

Original source

Related problems