Determines if given point is inside view - Android User Interface

Android examples for User Interface:View Contains

Description

Determines if given point is inside view

Demo Code


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

public class Main {
    /**//from   w  ww .jav a 2 s .co  m
     * Determines if given point is inside view
     * @param p - coordinates of point 
     * @param view - view object to compare
     * @return true if the point is within view bounds, false otherwise
     */
    public static boolean isPointInsideView(Point p, View view) {
        if (view == null)
            return false;

        int[] location = new int[2];

        view.getLocationOnScreen(location);

        if (p.x < location[0] || p.y < location[1])
            return false;

        if (location[0] + view.getWidth() < p.x
                || location[1] + view.getHeight() < p.y)
            return false;

        return true;
    }
}

Related Tutorials