get View Popup Position - Android User Interface

Android examples for User Interface:PopupMenu

Description

get View Popup Position

Demo Code


//package com.java2s;

import android.view.Gravity;

import android.view.View;

public class Main {

    public final static int[] getPopupPosition(View anchor, int gravity) {
        int[] position = new int[2];

        int windowWidth = anchor.getRootView().getMeasuredWidth();
        int windowHeight = anchor.getRootView().getMeasuredHeight();

        int anchorWidth = anchor.getMeasuredWidth();
        int anchorHeight = anchor.getMeasuredHeight();

        int[] location = new int[2];
        anchor.getLocationInWindow(location);

        if (Gravity.LEFT == (gravity & Gravity.LEFT)) {
            position[0] = location[0];//from w w w  .ja  v  a 2  s  .  co m
        } else if (Gravity.RIGHT == (gravity & Gravity.RIGHT)) {
            position[0] = windowWidth - location[0] - anchorWidth;
        }

        if (Gravity.TOP == (gravity & Gravity.TOP)) {
            position[1] = location[1] + anchorHeight;
        } else if (Gravity.BOTTOM == (gravity & Gravity.BOTTOM)) {
            position[1] = windowHeight - location[1];
        }

        return position;
    }
}

Related Tutorials