Java Code Examples for android.view.View.getWindowVisibleDisplayFrame()

The following are Jave code examples for showing how to use getWindowVisibleDisplayFrame() of the android.view.View class. You can vote up the examples you like. Your votes will be used in our system to get more good examples.
Example 1
Project: boohee_v5.6   File: KeyboardUtil.java   Source Code and License Vote up 6 votes
public void onGlobalLayout() {
    int displayHeight;
    View userRootView = this.contentView.getChildAt(0);
    View contentParentView = (View) this.contentView.getParent();
    Rect r = new Rect();
    if (this.isTranslucentStatus) {
        contentParentView.getWindowVisibleDisplayFrame(r);
        displayHeight = (r.bottom - r.top) + this.statusBarHeight;
    } else {
        userRootView.getWindowVisibleDisplayFrame(r);
        displayHeight = r.bottom - r.top;
    }
    calculateKeyboardHeight(displayHeight);
    calculateKeyboardShowing(displayHeight);
    this.previousDisplayHeight = displayHeight;
}
 
Example 2
Project: boohee_v5.6   File: BitmapUtil.java   Source Code and License Vote up 6 votes
@SuppressLint({"NewApi"})
public static Bitmap takeScreenShot(Activity pActivity) {
    View view = pActivity.getWindow().getDecorView();
    view.setDrawingCacheEnabled(true);
    view.buildDrawingCache();
    Bitmap bitmap = view.getDrawingCache();
    Rect frame = new Rect();
    view.getWindowVisibleDisplayFrame(frame);
    int stautsHeight = frame.top;
    Point size = new Point();
    Display display = pActivity.getWindowManager().getDefaultDisplay();
    if (VERSION.SDK_INT < 13) {
        size.set(display.getWidth(), display.getHeight());
    } else {
        pActivity.getWindowManager().getDefaultDisplay().getSize(size);
    }
    return Bitmap.createBitmap(bitmap, 0, stautsHeight, size.x, size.y - stautsHeight);
}
 
Example 3
Project: CXJPadProject   File: SobotSelectPicDialog.java   Source Code and License Vote up 6 votes
private void setParams(Context context, WindowManager.LayoutParams lay) {
    DisplayMetrics dm = new DisplayMetrics();
    WindowManager wm = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE);
    wm.getDefaultDisplay().getMetrics(dm);
    Rect rect = new Rect();
    View view = getWindow().getDecorView();
    view.getWindowVisibleDisplayFrame(rect);
    lay.width = dm.widthPixels;
}
 
Example 4
Project: chips-input-layout   File: FilterableRecyclerView.java   Source Code and License Vote up 6 votes
/**
 * Uses alpha animation to fade in the current view if it's not visible.
 */
void fadeIn() {
    if (getVisibility() == VISIBLE) { return; }

    // Get visible window (keyboard shown)
    Rect r = new Rect();
    final View rootView = getRootView();
    rootView.getWindowVisibleDisplayFrame(r);

    int[] coord = new int[2];
    this.chipsInput.getLocationInWindow(coord);
    ViewGroup.MarginLayoutParams lp = (MarginLayoutParams)getLayoutParams();
    lp.topMargin = coord[1] + chipsInput.getHeight();

    // Height of the keyboard
    lp.bottomMargin = rootView.getHeight() - r.bottom;
    setLayoutParams(lp);

    AlphaAnimation anim = new AlphaAnimation(0.0f, 1.0f);
    anim.setDuration(200);
    startAnimation(anim);

    setVisibility(VISIBLE);
}
 
Example 5
Project: TextEmoji   File: SoftKeyboardUtils.java   Source Code and License Vote up 6 votes
/**
 * 判断软键盘是否弹出
 * * @param rootView
 *
 * @param listener 备注:在不用的时候记得移除OnGlobalLayoutListener
 */
public static ViewTreeObserver.OnGlobalLayoutListener doMonitorSoftKeyWord(final View rootView, final OnSoftKeyWordShowListener listener) {
    final ViewTreeObserver.OnGlobalLayoutListener layoutListener = new ViewTreeObserver.OnGlobalLayoutListener() {
        @Override
        public void onGlobalLayout() {
            final Rect rect = new Rect();
            rootView.getWindowVisibleDisplayFrame(rect);
            final int screenHeight = rootView.getRootView().getHeight();
            Log.e("TAG", rect.bottom + "#" + screenHeight);
            final int heightDifference = screenHeight - rect.bottom;
            boolean visible = heightDifference > screenHeight / 3;
            if (listener != null)
                listener.hasShow(visible);
        }
    };
    rootView.getViewTreeObserver().addOnGlobalLayoutListener(layoutListener);
    return layoutListener;
}
 
Example 6
Project: dynamic-toasts   File: DynamicHint.java   Source Code and License Vote up 6 votes
/**
 * Show a toast above or below according to the anchor view position.
 *
 * @param anchor The anchor view to show the toast.
 * @param toast The toast to be displayed.
 */
public static void show(@NonNull View anchor, @NonNull Toast toast) {
    final int[] screenPos = new int[2];
    final Rect displayFrame = new Rect();
    anchor.getLocationOnScreen(screenPos);
    anchor.getWindowVisibleDisplayFrame(displayFrame);

    final int viewWidth = anchor.getWidth();
    final int viewHeight = anchor.getHeight();
    final int viewCenterX = screenPos[0] + viewWidth / 2;
    final int screenWidth = anchor.getResources().getDisplayMetrics().widthPixels;
    final int estimatedToastHeight = DynamicUnitUtils
            .convertDpToPixels(ADT_ESTIMATED_TOAST_HEIGHT_DIPS);

    if (screenPos[1] < estimatedToastHeight) {
        toast.setGravity(Gravity.TOP | Gravity.CENTER_HORIZONTAL,
                viewCenterX - screenWidth / 2,
                screenPos[1] - displayFrame.top - viewHeight);
    } else {
        toast.setGravity(Gravity.TOP | Gravity.CENTER_HORIZONTAL,
                viewCenterX - screenWidth / 2,
                screenPos[1] - displayFrame.top - estimatedToastHeight);
    }

    toast.show();
}
 
Example 7
Project: CXJPadProject   File: KeyboardUtil.java   Source Code and License Vote up 6 votes
@Override
public void onGlobalLayout() {
    final View userRootView = contentView.getChildAt(0);
    final View contentParentView = (View) contentView.getParent();

    // Step 1. calculate the current display frame's height.
    Rect r = new Rect();

    final int displayHeight;
    if (isTranslucentStatus) {
        contentParentView.getWindowVisibleDisplayFrame(r);
        displayHeight = (r.bottom - r.top) + statusBarHeight;
    } else {
        userRootView.getWindowVisibleDisplayFrame(r);
        displayHeight = (r.bottom - r.top);
    }

    calculateKeyboardHeight(displayHeight);
    calculateKeyboardShowing(displayHeight);

    previousDisplayHeight = displayHeight;
}
 
Example 8
Project: Impala   File: Utils.java   Source Code and License Vote up 5 votes
/**
 * @param rootView
 * @return
 */
public static boolean isKeyboardShown(View rootView) {
        /* 128dp = 32dp * 4, minimum button height 32dp and generic 4 rows soft keyboard */
    final int softKeyboardHeightThreshold = 128;

    Rect r = new Rect();
    rootView.getWindowVisibleDisplayFrame(r);
    DisplayMetrics dm = rootView.getResources().getDisplayMetrics();
        /* heightDiff = rootView height - status bar height (r.top) - visible frame height (r.bottom - r.top) */
    int heightDiff = rootView.getBottom() - r.bottom;
        /* Threshold size: dp to pixels, multiply with display density */
    return heightDiff > softKeyboardHeightThreshold * dm.density;
}
 
Example 9
Project: EasyEmoji   File: IMERootLayout.java   Source Code and License Vote up 5 votes
private boolean isKeyboardShown() {
    View mRootContentView = mWindow.getDecorView().findViewById(android.R.id.content);
    if(mRootContentView == null){
        return false;
    }
    final int softKeyboardHeight = 100;
    Rect r = new Rect();
    mRootContentView.getWindowVisibleDisplayFrame(r);
    int heightDiff = mRootContentView.getBottom() - r.bottom;
    DisplayMetrics mDisplayMetrics = getResources().getDisplayMetrics();
    boolean isOpen = heightDiff > softKeyboardHeight * mDisplayMetrics.density;
    return isOpen;
}
 
Example 10
Project: EasyEmoji   File: KeyboardManagerImpl.java   Source Code and License Vote up 5 votes
@TargetApi(Build.VERSION_CODES.HONEYCOMB_MR2)
@Override
public void onGlobalLayout() {
    final View userRootView = contentView.getChildAt(0);
    final View actionBarOverlayLayout = (View) contentView.getParent();

    Rect r = new Rect();

    final int displayHeight;
    if (isTranslucentStatus) {
        actionBarOverlayLayout.getWindowVisibleDisplayFrame(r);

        final int overlayLayoutDisplayHeight = (r.bottom - r.top);

        if (!isOverlayLayoutDisplayHContainStatusBar) {
            isOverlayLayoutDisplayHContainStatusBar = overlayLayoutDisplayHeight == screenHeight;
        }

        if (!isOverlayLayoutDisplayHContainStatusBar) {
            displayHeight = overlayLayoutDisplayHeight + statusBarHeight;
        } else {
            displayHeight = overlayLayoutDisplayHeight;
        }

    } else {
        userRootView.getWindowVisibleDisplayFrame(r);
        displayHeight = (r.bottom - r.top);
    }

    calculateKeyboardHeight(displayHeight);
    calculateKeyboardShowing(displayHeight);

    previousDisplayHeight = displayHeight;
}
 
Example 11
Project: Android-UtilCode   File: KeyboardActivity.java   Source Code and License Vote up 5 votes
private boolean isKeyboardShown(View rootView) {
    final int softKeyboardHeight = 100;
    Rect frame = new Rect();
    rootView.getWindowVisibleDisplayFrame(frame);
    DisplayMetrics dm = rootView.getResources().getDisplayMetrics();
    int heightDiff = rootView.getBottom() - frame.bottom;
    LogUtils.d("" + rootView.getBottom() + ", " + frame.bottom + ", " + heightDiff);
    return heightDiff > softKeyboardHeight * dm.density;
}
 
Example 12
Project: RLibrary   File: RSoftInputLayout.java   Source Code and License Vote up 5 votes
public static int getSoftKeyboardHeight(View view) {
    int screenHeight = view.getResources().getDisplayMetrics().heightPixels;
    Rect rect = new Rect();
    view.getWindowVisibleDisplayFrame(rect);
    int visibleBottom = rect.bottom;
    return screenHeight - visibleBottom;
}
 
Example 13
Project: diycode   File: LoginActivity.java   Source Code and License Vote up 5 votes
private boolean isKeyboardShown(View rootView) {
    final int softKeyboardHeight = 100;
    Rect r = new Rect();
    rootView.getWindowVisibleDisplayFrame(r);
    DisplayMetrics dm = rootView.getResources().getDisplayMetrics();
    int heightDiff = rootView.getBottom() - r.bottom;
    return heightDiff > softKeyboardHeight * dm.density;
}
 
Example 14
Project: ucar-weex-core   File: SoftKeyboardDetector.java   Source Code and License Vote up 5 votes
public static boolean isKeyboardVisible(Activity activity) {
    Rect windowFrame = new Rect();
    View root = getActivityRoot(activity);

    if (root != null) {
        root.getWindowVisibleDisplayFrame(windowFrame);
        int heightDiff = root.getRootView().getHeight() - windowFrame.height();
        return heightDiff > WXViewUtils.dip2px(KEYBOARD_VISIBLE_THRESHOLD_DIP);
    }
    return false;
}
 
Example 15
Project: QMUI_Android   File: QMUIKeyboardHelper.java   Source Code and License Vote up 5 votes
/**
 * Determine if keyboard is visible
 *
 * @param activity Activity
 * @return Whether keyboard is visible or not
 */
public static boolean isKeyboardVisible(Activity activity) {
    Rect r = new Rect();

    View activityRoot = QMUIViewHelper.getActivityRoot(activity);
    int visibleThreshold =
            Math.round(QMUIDisplayHelper.dp2px(activity, KEYBOARD_VISIBLE_THRESHOLD_DP));

    activityRoot.getWindowVisibleDisplayFrame(r);

    int heightDiff = activityRoot.getRootView().getHeight() - r.height();

    return heightDiff > visibleThreshold;
}
 
Example 16
Project: jackknife   File: ImageUtils.java   Source Code and License Vote up 5 votes
public static Bitmap takeScreenShot(Activity activity){
    View view = activity.getWindow().getDecorView();
    view.setDrawingCacheEnabled(true);
    view.buildDrawingCache();
    Bitmap bitmap = view.getDrawingCache();
    Rect rect = new Rect();
    view.getWindowVisibleDisplayFrame(rect);
    int statusBarHeight = rect.top;
    Bitmap outputBitmap = Bitmap.createBitmap(bitmap,0,statusBarHeight,
            SystemUtils.getScreenWH()[0], SystemUtils.getScreenWH()[1] - statusBarHeight);
    view.destroyDrawingCache();
    return outputBitmap;
}
 
Example 17
Project: okwallet   File: CheatSheet.java   Source Code and License Vote up 5 votes
/**
 * Internal helper method to show the cheat sheet toast.
 */
private static boolean showCheatSheet(View view, CharSequence text) {
    if (TextUtils.isEmpty(text)) {
        return false;
    }

    final int[] screenPos = new int[2]; // origin is device display
    final Rect displayFrame = new Rect(); // includes decorations (e.g. status bar)
    view.getLocationOnScreen(screenPos);
    view.getWindowVisibleDisplayFrame(displayFrame);

    final Context context = view.getContext();
    final int viewWidth = view.getWidth();
    final int viewHeight = view.getHeight();
    final int viewCenterX = screenPos[0] + viewWidth / 2;
    final int screenWidth = context.getResources().getDisplayMetrics().widthPixels;
    final int estimatedToastHeight = (int) (ESTIMATED_TOAST_HEIGHT_DIPS
            * context.getResources().getDisplayMetrics().density);

    Toast cheatSheet = Toast.makeText(context, text, Toast.LENGTH_SHORT);
    boolean showBelow = screenPos[1] < estimatedToastHeight;
    if (showBelow) {
        // Show below
        // Offsets are after decorations (e.g. status bar) are factored in
        cheatSheet.setGravity(Gravity.TOP | Gravity.CENTER_HORIZONTAL, viewCenterX - screenWidth / 2,
                screenPos[1] - displayFrame.top + viewHeight);
    } else {
        // Show above
        // Offsets are after decorations (e.g. status bar) are factored in
        // NOTE: We can't use Gravity.BOTTOM because when the keyboard is up
        // its height isn't factored in.
        cheatSheet.setGravity(Gravity.TOP | Gravity.CENTER_HORIZONTAL, viewCenterX - screenWidth / 2,
                screenPos[1] - displayFrame.top - estimatedToastHeight);
    }

    cheatSheet.show();
    return true;
}
 
Example 18
Project: qmui   File: QMUIKeyboardHelper.java   Source Code and License Vote up 4 votes
/**
 * Set keyboard visibility change event listener.
 *
 * @param activity Activity
 * @param listener KeyboardVisibilityEventListener
 */
@SuppressWarnings("deprecation")
public static void setVisibilityEventListener(final Activity activity,
                                              final KeyboardVisibilityEventListener listener) {

    if (activity == null) {
        throw new NullPointerException("Parameter:activity must not be null");
    }

    if (listener == null) {
        throw new NullPointerException("Parameter:listener must not be null");
    }

    final View activityRoot = QMUIViewHelper.getActivityRoot(activity);

    final ViewTreeObserver.OnGlobalLayoutListener layoutListener =
            new ViewTreeObserver.OnGlobalLayoutListener() {

                private final Rect r = new Rect();

                private final int visibleThreshold = Math.round(
                        QMUIDisplayHelper.dp2px(activity, KEYBOARD_VISIBLE_THRESHOLD_DP));

                private boolean wasOpened = false;

                @Override
                public void onGlobalLayout() {
                    activityRoot.getWindowVisibleDisplayFrame(r);

                    int heightDiff = activityRoot.getRootView().getHeight() - r.height();

                    boolean isOpen = heightDiff > visibleThreshold;

                    if (isOpen == wasOpened) {
                        // keyboard state has not changed
                        return;
                    }

                    wasOpened = isOpen;

                    listener.onVisibilityChanged(isOpen);
                }
            };
    activityRoot.getViewTreeObserver().addOnGlobalLayoutListener(layoutListener);
    activity.getApplication()
            .registerActivityLifecycleCallbacks(new QMUIActivityLifecycleCallbacks(activity) {
                @Override
                protected void onTargetActivityDestroyed() {
                    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN) {
                        activityRoot.getViewTreeObserver()
                                .removeOnGlobalLayoutListener(layoutListener);
                    } else {
                        activityRoot.getViewTreeObserver()
                                .removeGlobalOnLayoutListener(layoutListener);
                    }
                }
            });
}
 
Example 19
Project: QMUI_Android   File: QMUIKeyboardHelper.java   Source Code and License Vote up 4 votes
/**
 * Set keyboard visibility change event listener.
 *
 * @param activity Activity
 * @param listener KeyboardVisibilityEventListener
 */
@SuppressWarnings("deprecation")
public static void setVisibilityEventListener(final Activity activity,
                                              final KeyboardVisibilityEventListener listener) {

    if (activity == null) {
        throw new NullPointerException("Parameter:activity must not be null");
    }

    if (listener == null) {
        throw new NullPointerException("Parameter:listener must not be null");
    }

    final View activityRoot = QMUIViewHelper.getActivityRoot(activity);

    final ViewTreeObserver.OnGlobalLayoutListener layoutListener =
            new ViewTreeObserver.OnGlobalLayoutListener() {

                private final Rect r = new Rect();

                private final int visibleThreshold = Math.round(
                        QMUIDisplayHelper.dp2px(activity, KEYBOARD_VISIBLE_THRESHOLD_DP));

                private boolean wasOpened = false;

                @Override
                public void onGlobalLayout() {
                    activityRoot.getWindowVisibleDisplayFrame(r);

                    int heightDiff = activityRoot.getRootView().getHeight() - r.height();

                    boolean isOpen = heightDiff > visibleThreshold;

                    if (isOpen == wasOpened) {
                        // keyboard state has not changed
                        return;
                    }

                    wasOpened = isOpen;

                    listener.onVisibilityChanged(isOpen);
                }
            };
    activityRoot.getViewTreeObserver().addOnGlobalLayoutListener(layoutListener);
    activity.getApplication()
            .registerActivityLifecycleCallbacks(new QMUIActivityLifecycleCallbacks(activity) {
                @Override
                protected void onTargetActivityDestroyed() {
                    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN) {
                        activityRoot.getViewTreeObserver()
                                .removeOnGlobalLayoutListener(layoutListener);
                    } else {
                        activityRoot.getViewTreeObserver()
                                .removeGlobalOnLayoutListener(layoutListener);
                    }
                }
            });
}
 
Example 20
Project: GeekZone   File: CheatSheetUtils.java   Source Code and License Vote up 4 votes
/**
 * Internal helper method to show the cheat sheet toast.
 */
private static boolean show(View view, CharSequence text) {
    if (TextUtils.isEmpty(text)) {
        return false;
    }

    final int[] screenPos = new int[2]; // origin is device display
    final Rect displayFrame = new Rect(); // includes decorations (e.g. status bar)
    view.getLocationOnScreen(screenPos);
    view.getWindowVisibleDisplayFrame(displayFrame);

    final Context context = view.getContext();
    final int viewWidth = view.getWidth();
    final int viewHeight = view.getHeight();
    final int viewCenterX = screenPos[0] + viewWidth / 2;
    final int screenWidth = context.getResources().getDisplayMetrics().widthPixels;
    final int estimatedToastHeight = (int) (ESTIMATED_TOAST_HEIGHT_DIPS
            * context.getResources().getDisplayMetrics().density);

    Toast cheatSheet = Toast.makeText(context, text, Toast.LENGTH_SHORT);
    boolean showBelow = screenPos[1] < estimatedToastHeight;
    if (showBelow) {
        // Show below
        // Offsets are after decorations (e.g. status bar) are factored in
        cheatSheet.setGravity(Gravity.TOP | Gravity.CENTER_HORIZONTAL,
                viewCenterX - screenWidth / 2,
                screenPos[1] - displayFrame.top + viewHeight);
    } else {
        // Show above
        // Offsets are after decorations (e.g. status bar) are factored in
        // NOTE: We can't use Gravity.BOTTOM because when the keyboard is up
        // its height isn't factored in.
        cheatSheet.setGravity(Gravity.TOP | Gravity.CENTER_HORIZONTAL,
                viewCenterX - screenWidth / 2,
                screenPos[1] - displayFrame.top - estimatedToastHeight);
    }

    cheatSheet.show();
    return true;
}