get View Location Relative To - Android User Interface

Android examples for User Interface:View Position

Description

get View Location Relative To

Demo Code


//package com.java2s;

import android.graphics.Rect;

import android.view.View;

public class Main {
    public static Rect getViewLocationRelativeTo(View view,
            View relativeToView) {
        int[] viewLocation = new int[2];
        view.getLocationInWindow(viewLocation);

        int[] relativeToViewLocation = new int[2];
        relativeToView.getLocationInWindow(relativeToViewLocation);

        int x = viewLocation[0] - relativeToViewLocation[0];
        int y = viewLocation[1] - relativeToViewLocation[1];
        int w = view.getWidth();
        int h = view.getHeight();
        return new Rect(x, y, x + w, y + h);
    }//from  w  w w .j a v a2 s.co  m
}

Related Tutorials