tags:

views:

100

answers:

1

Is there an equivalent of the convertPointFromScreen method from the SwingUtilities class in the Anrdoid SDK ?

+1  A: 

I don't know of a built-in function to do it, but here's one using View.getLocationOnScreen().

protected static void convertPointFromScreen( int[] point, View v ) {
    final int v_location[] = new int[2];
    v.getLocationOnScreen(v_location);
    point[0] -= v_location[0];
    point[1] -= v_location[1];
}

Before you call this function you must make sure that the view has already been laid out on screen. The best way to do that is to post the invocation to the parent view's handler, eg:

    final View v = findViewById(R.id.my_view);
    ((View)v.getParent()).post( new Runnable() {
        public void run() {
            final int location[] = { 100, 400 };
            convertPointFromScreen(location, v );
        }
    });
Mike