Last active
June 8, 2017 13:28
-
-
Save elsennov/9408a19e9b2c783d99b4 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.novraditya.elsen.tablayoutexample; | |
import android.content.Context; | |
import android.graphics.Point; | |
import android.util.DisplayMetrics; | |
import android.view.Display; | |
import android.view.WindowManager; | |
/** | |
* Created by elsennovraditya on 3/20/16. | |
*/ | |
public class Utils { | |
private static final int WIDTH_INDEX = 0; | |
private static final int HEIGHT_INDEX = 1; | |
public static int[] getScreenSize(Context context) { | |
int[] widthHeight = new int[2]; | |
widthHeight[WIDTH_INDEX] = 0; | |
widthHeight[HEIGHT_INDEX] = 0; | |
WindowManager windowManager = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE); | |
Display display = windowManager.getDefaultDisplay(); | |
Point size = new Point(); | |
display.getSize(size); | |
widthHeight[WIDTH_INDEX] = size.x; | |
widthHeight[HEIGHT_INDEX] = size.y; | |
if (!isScreenSizeRetrieved(widthHeight)) { | |
DisplayMetrics metrics = new DisplayMetrics(); | |
display.getMetrics(metrics); | |
widthHeight[0] = metrics.widthPixels; | |
widthHeight[1] = metrics.heightPixels; | |
} | |
// Last defense. Use deprecated API that was introduced in lower than API 13 | |
if (!isScreenSizeRetrieved(widthHeight)) { | |
widthHeight[0] = display.getWidth(); // deprecated | |
widthHeight[1] = display.getHeight(); // deprecated | |
} | |
return widthHeight; | |
} | |
private static boolean isScreenSizeRetrieved(int[] widthHeight) { | |
return widthHeight[WIDTH_INDEX] != 0 && widthHeight[HEIGHT_INDEX] != 0; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment