Question

Is it possible in a Service (i.e. not in an Activity) to get the screen's working area, i.e. its height minus the status bar height at the top and the soft key space ('back', 'home' etc) at the bottom?

Was it helpful?

Solution

private DisplayMetrics displayMetrics = new DisplayMetrics();
WindowManager windowManager = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE);
windowManager.getDefaultDisplay().getMetrics(displayMetrics);

int height = displayMetrics.heightPixels;
int width = displayMetrics.widthPixels;

I've tried this from the service and it works for me. On 2.3 devices (tried on Galaxy Y and Nexus One) it gives the complete screen size. But on 4.x devices (tried on Nexus 4), It gives the screen size minus the status bar. If you have found a more reliable method do post.

OTHER TIPS

As well as Srikant's answer, here's an alternative way of getting the display dimensions:

DisplayMetrics displayMetrics = context.getResources().getDisplayMetrics();
int height = displayMetrics.heightPixels;
int width = displayMetrics.widthPixels;

This gives the same height, width as Srikant's answer.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top