user2101081
user2101081

Reputation: 451

WindowManager X Y not working

I am using Android window-manager to display an overlay image onto the users phone, but it is not setting the ImageView in the correct location, it always defaults to the center of the screen. I know the location is correct cause I have used accessibility services to touch the location and with show touches turned on in my phone it is touching the correct location. So why will the imageview not go to this location?

I get the location of the where I want to place the image from and imageview in my app:

Point centerPoint = getCenterPointOfView(myImageView);
int xImageLocation = centerPoint.x;
int yImageLocation = centerPoint.y;

And then I open the new overlay with this:

//Add the view to the window.
    final WindowManager.LayoutParams params = new WindowManager.LayoutParams(
            WindowManager.LayoutParams.WRAP_CONTENT,
            WindowManager.LayoutParams.WRAP_CONTENT,
            LAYOUT_FLAG,
            WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE,
            PixelFormat.TRANSLUCENT);

params.x = xImageLocation;
params.y = yImageLocation;

mWindowManager = (WindowManager) getSystemService(WINDOW_SERVICE);
    if (mWindowManager != null) {
        mWindowManager.addView(mFloatingView, params);
    }else {
        return;
    }

Upvotes: 1

Views: 2210

Answers (1)

Pawel
Pawel

Reputation: 17268

You need to set gravity of WindowManager.LayoutParams to Gravity.TOP | Gravity.LEFT.

As you've noticed by default its set to Gravity.CENTER, so with x=0 and y=0 your mFloatingView should be exactly in the middle of the screen.

Upvotes: 3

Related Questions