Main.java Source code

Java tutorial

Introduction

Here is the source code for Main.java

Source

//package com.java2s;
//License from project: Apache License 

import android.content.Context;

import android.os.Build;

import android.util.DisplayMetrics;
import android.util.Pair;
import android.util.TypedValue;

import android.view.ViewConfiguration;
import android.view.WindowManager;

public class Main {
    public static Pair<Integer, Integer> getWindowDimensionsWithoutMargin(Context context,
            WindowManager windowManager, int dpMarginX, int dpMarginY) {

        dpMarginX *= 2;
        dpMarginY *= 2;

        DisplayMetrics displayMetrics = new DisplayMetrics();
        windowManager.getDefaultDisplay().getMetrics(displayMetrics);
        int screenWidth = displayMetrics.widthPixels;
        int screenHeight = displayMetrics.heightPixels;

        int marginX = dpToPx(context, dpMarginX);
        if (hasSoftNavigation(context)) {
            dpMarginY += 30;
        }
        int marginY = dpToPx(context, dpMarginY);

        Pair<Integer, Integer> dimensions = new Pair<Integer, Integer>(screenWidth - marginX,
                screenHeight - marginY);

        return dimensions;
    }

    private static int dpToPx(Context context, int dp) {
        return (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, dp,
                context.getResources().getDisplayMetrics());
    }

    private static boolean hasSoftNavigation(Context context) {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.ICE_CREAM_SANDWICH) {
            return !ViewConfiguration.get(context).hasPermanentMenuKey();
        }
        return false;
    }
}