Euler Geek
Euler Geek

Reputation: 131

Wrong Screen width and height

What is the diference betwen use the next code, to get screen width and height in android :

 public HorseView(Context context) {
        super(context);
    DisplayMetrics metrics = context.getResources().getDisplayMetrics();
    int height = metrics.heightPixels;
.............
}

And use the another this:

public void surfaceCreated(SurfaceHolder holder) {
    thread.setRunning(true);
    thread.start();
    int height = this.getHeight();
}

Anyone know why in firs case, height is set to 508; and in the second case is 533;

Thanks in advance.

Upvotes: 1

Views: 278

Answers (3)

Euler Geek
Euler Geek

Reputation: 131

Emulator or device have diferent density:

  • ldpi=.75,
  • mdpi=1,
  • hdpi=1.5,
  • xhdpi=2

The status bar icons have a height of 25dp (mdpi). For all densities the bar height: 25*(ldpi=.75, mdpi=1, hdpi=1.5, xhdpi=2) = (19px, 25px, 38px, 50px).

We can use 25dp as the base and multiply it by the density (rounded up) to get the status bar height on any device:

int statusBarHeight = (int) Math.ceil(25 * context.getResources().getDisplayMetrics().density);

Finally, the dimension that the application can use, if you have context, is:

int heightAreaWork = context.getResources().getDisplayMetrics().heightPixels - statusBarHeight;

Upvotes: 0

faylon
faylon

Reputation: 7450

  • 533 * 1.5 = 800.
  • 508 * 1.5 = 762.

The difference between the two value is because of the the height of notification bar, which is 38px height.

Upvotes: 3

sandy
sandy

Reputation: 3351

Try it.

Display display = getWindowManager().getDefaultDisplay();
     int height=display.getHeight();
              int width=display.getWidth();

Upvotes: 0

Related Questions