I’m trying to get screen dimensions inside page adapter. I could get it inside MainActivity 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 instantiateItem?
This is my code:
public class MyPagerAdapter extends PagerAdapter {
Display display = getWindowManager().getDefaultDisplay(); // The method getWindowManager() is undefined for the type MyPagerAdapter
display.getSize(size);
...
}
From any context reference (such as in your
intantiateItem), you can get a reference to theDisplayMetricsclass by doingcontext.getResources().getDisplayMetrics()Which gives you a reference to the
DisplayMetricsclass DisplayMetricsParticularly of use to you will be the
widthPixelsandheightPixelsattributes of this, which return the raw pixel height and width for the device.