If I put this in some activity class it works perfectly but, when I put it in my App class the method getWindowManager()
can not be found. Is there some way to get the WindowManager in app class?
My app class is defined like this:
public class myApp extends Application {
and in on create method I have this:
DisplayMetrics dm = new DisplayMetrics();
getWindowManager().getDefaultDisplay().getMetrics(dm);
int width = dm.widthPixels;
Here, Context.getResource()
DisplayMetrics dm = getResources().getDisplayMetrics();
int densityDpi = dm.densityDpi;
You can also try this:
WindowManager wm = (WindowManager) getSystemService(WINDOW_SERVICE);
final DisplayMetrics displayMetrics = new DisplayMetrics();
wm.getDefaultDisplay().getMetrics(displayMetrics);
int height = displayMetrics.heightPixels;
int width = displayMetrics.widthPixels;
Try this:
Display display = getWindowManager().getDefaultDisplay();
Log.e("", "" + display.getHeight() + " " + display.getWidth());
hope this will work for you
DisplayMetrics displayMetrics = context.getResources().getDisplayMetrics();
int pxWidth = displayMetrics.widthPixels;
int pxHeight = displayMetrics.heightPixels;
With Context -
context.resources.displayMetrics.widthPixels
Without Context use -
Resources.getSystem().displayMetrics.widthPixels // but this doesn't work well on foldables
Note : getWindowManager().getDefaultDisplay().getMetrics(dm);
returns void
in the new API.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With