I have one class which does not extends any Activity. i want to find out that user uses LDPI,MDPI,HDPI or XHDPI screen using code. any idea? I have used DisplayMetrics metrics = new DisplayMetrics(); but it needs an Activity to extend. i want that without extending activity.
getWindowManager().getDefaultDisplay().getMetrics(metrics);
I have one class which does not extends any Activity. i want to find
Share
The problem with window manager is that it needs the activity context, read the name again
window managerit manages stuff on the scree, ergo, the activity.There’re some different tricks you can try:
You will need the context for this solution, but it doesn’t have to be the activity context, it can be the application context. Actually, to be honest you’ll need the resources, which can be accessed through the application context.
on your res folder create a value.xml for the all the parameters you want to find out:
now on each one of those xml you put the following code:
only changing the
trueorfalseas appropriate.now it’s just as simple as calling: