Main.java Source code

Java tutorial

Introduction

Here is the source code for Main.java

Source

//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];
        } 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;
    }
}