Skip to content

Instantly share code, notes, and snippets.

@explodes
Created April 22, 2016 19:35
Show Gist options
  • Save explodes/0aff72d8b9bbddd28830f453bb33dc14 to your computer and use it in GitHub Desktop.
Save explodes/0aff72d8b9bbddd28830f453bb33dc14 to your computer and use it in GitHub Desktop.
Detecting the amount the status bar and navigation bars affect the screen area on Android
import android.content.Context;
import android.content.res.Resources;
import android.graphics.Point;
import android.os.Build;
import android.support.annotation.NonNull;
import android.view.Display;
import android.view.WindowManager;
import java.lang.reflect.InvocationTargetException;
public class ScreenUtils {
/**
* Whether or not a display is in landscape.
* <pre>
* Display display = activity.getWindowManager().getDefaultDisplay();
* boolean isLandscape = isLandscape(display);
* </pre>
*
* @return whether or not the screen is in landscape mode
*/
public boolean isLandscape(@NonNull Display display) {
Point point = new Point();
display.getSize(point);
return point.x > point.y;
}
/**
* Get the height of the status bar
*/
public static int getStatusBarHeight(@NonNull Context context) {
int result = 0;
Resources resources = context.getResources();
int resourceId = resources.getIdentifier("status_bar_height", "dimen", "android");
if (resourceId > 0) {
result = resources.getDimensionPixelSize(resourceId);
}
return result;
}
/**
* Get the width of a navigation bar on the right, or the height of a navigation bar on the bottom.
*/
public static Point getNavigationBarEffect(@NonNull Context context) {
Point appUsableSize = getAppUsableScreenSize(context);
Point realScreenSize = getRealScreenSize(context);
// navigation bar on the right
if (appUsableSize.x < realScreenSize.x) {
return new Point(realScreenSize.x - appUsableSize.x, 0);
}
// navigation bar at the bottom
if (appUsableSize.y < realScreenSize.y) {
return new Point(0, realScreenSize.y - appUsableSize.y);
}
// navigation bar is not present
return new Point();
}
private static Point getAppUsableScreenSize(@NonNull Context context) {
WindowManager windowManager = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE);
Display display = windowManager.getDefaultDisplay();
Point size = new Point();
display.getSize(size);
return size;
}
private static Point getRealScreenSize(@NonNull Context context) {
WindowManager windowManager = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE);
Display display = windowManager.getDefaultDisplay();
Point size = new Point();
if (Build.VERSION.SDK_INT >= 17) {
display.getRealSize(size);
} else if (Build.VERSION.SDK_INT >= 14) {
try {
size.x = (Integer) Display.class.getMethod("getRawWidth").invoke(display);
size.y = (Integer) Display.class.getMethod("getRawHeight").invoke(display);
} catch (IllegalAccessException e) {
} catch (InvocationTargetException e) {
} catch (NoSuchMethodException e) {
}
}
return size;
}
}
@Nik2505
Copy link

Nik2505 commented Mar 4, 2019

If Navigation bar is not visible for Android P (Navigation Gesture) then navigation height will be zero but I can't get zero. Can you help in this?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment