lisovaccaro lisovaccaro - 2 months ago 16
Android Question

Get screen dimensions inside page adapter?

I'm trying to get screen dimensions inside page adapter. I could get it inside

and pass it to the adapter but it would be better to get it there. How can I do it, either directly inside the adapter or inside

This is my code:

public class MyPagerAdapter extends PagerAdapter {
Display display = getWindowManager().getDefaultDisplay(); // The method getWindowManager() is undefined for the type MyPagerAdapter


From any context reference (such as in your intantiateItem), you can get a reference to the DisplayMetrics class by doing


Which gives you a reference to the DisplayMetrics class DisplayMetrics

Particularly of use to you will be the widthPixels and heightPixels attributes of this, which return the raw pixel height and width for the device.