Find the topmost child under the given point within the parent view's coordinate system. - Android User Interface

Android examples for User Interface:View Parent

Description

Find the topmost child under the given point within the parent view's coordinate system.

Demo Code


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

public class Main {
    /**/*  www  .  j ava2  s  .  c  o m*/
     * Find the topmost child under the given point within the parent view's
     * coordinate system.
     * 
     * @param x
     *            X position to test in the parent's coordinate system
     * @param y
     *            Y position to test in the parent's coordinate system
     * @return The topmost child view under (x, y) or null if none found.
     */
    public static View findTopChildUnder(ViewGroup mParentView, int x, int y) {
        final int childCount = mParentView.getChildCount();
        for (int i = childCount - 1; i >= 0; i--) {
            final View child = mParentView.getChildAt(i);
            if (x >= child.getLeft() && x < child.getRight()
                    && y >= child.getTop() && y < child.getBottom()) {
                return child;
            }
        }
        return null;
    }
}

Related Tutorials