Returns true if the given x,y coordinates falls within the view bounds. - Android User Interface

Android examples for User Interface:View

Description

Returns true if the given x,y coordinates falls within the view bounds.

Demo Code


//package com.java2s;
import android.view.View;

public class Main {
    /**/*from   w  ww .java  2  s  . c  om*/
     * Returns true if the given x,y coordinates falls within the view bounds.
     * @param view
     * @param x
     * @param y
     * @return
     */
    public static boolean isInBounds(View view, int x, int y) {

        int[] l = new int[2];
        view.getLocationOnScreen(l);
        int vx = l[0];
        int vy = l[1];
        int vw = view.getWidth();
        int vh = view.getHeight();

        return !(x < vx || x > vx + vw || y < vy || y > vy + vh);
    }
}

Related Tutorials