Get location of view relative to screen. - Android User Interface

Android examples for User Interface:View

Description

Get location of view relative to screen.

Demo Code


//package com.java2s;

import android.graphics.Point;

import android.view.View;

public class Main {
    private static int[] mLocation = new int[2];

    /**// ww w  . j av  a2  s . co  m
     * Get location of view relative to screen.<br>
     * <br>
     * <b>Note</b>: Call this when content is completely inflated. So {@code onCreate} of an
     * activity won't work.
     * 
     * @param view
     *            any view which is attached to the content
     * 
     * @return location pixel
     */
    public static Point getScreenLocation(View view) {
        view.getLocationOnScreen(mLocation);
        return new Point(mLocation[0], mLocation[1]);
    }
}

Related Tutorials