is Visible On Screen - Android User Interface

Android examples for User Interface:Screen Position

Description

is Visible On Screen

Demo Code


//package com.java2s;
import android.app.Activity;

import android.graphics.Rect;
import android.support.v4.app.Fragment;

import android.view.View;

public class Main {
    public static boolean isVisibleOnScreen(Fragment fragment) {
        return isVisibleOnScreen(fragment.getActivity(), fragment.getView());
    }//w w  w. j a  v a 2s.c o m

    public static boolean isVisibleOnScreen(Activity activity, View view) {
        if (activity == null || view == null) {
            return false; // ASK better throw exception?
        }
        Rect screenRect = new Rect();
        activity.getWindow().getDecorView()
                .getGlobalVisibleRect(screenRect);
        int[] location = new int[2];
        view.getLocationOnScreen(location);
        boolean inHorizontalBounds = screenRect.left <= location[0]
                && screenRect.right >= location[0] + view.getWidth();
        boolean inVerticalBounds = screenRect.top <= location[1]
                && screenRect.bottom >= location[1] + view.getHeight();
        return view.isShown() && inHorizontalBounds && inVerticalBounds;
    }
}

Related Tutorials