Java tutorial
//package com.java2s; //License from project: Apache License import android.annotation.TargetApi; import android.content.Context; import android.graphics.Point; import android.os.Build; import android.support.annotation.NonNull; import android.view.Display; import android.view.WindowManager; public class Main { public static int getDisplayWidthPixel(@NonNull Context context) { return getDisplayWidthPixel( ((WindowManager) context.getSystemService(Context.WINDOW_SERVICE)).getDefaultDisplay()); } public static int getDisplayWidthPixel(@NonNull Display display) { int width; if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB_MR2) { width = getDisplayWidthPixel13AndLater(display); } else { width = getDisplayWidthPixel12AndBefore(display); } return width; } @TargetApi(Build.VERSION_CODES.HONEYCOMB_MR2) public static int getDisplayWidthPixel13AndLater(@NonNull Display display) { Point size = new Point(); display.getSize(size); return size.x; } public static int getDisplayWidthPixel12AndBefore(@NonNull Display display) { //noinspection deprecation return display.getWidth(); } }