Android: Screen bottom coordinate is beyond visible area

437 views Asked by At

I'm trying to create the control, that allows moving view with the finger. To do this, I follow recommendations from this post.

But presented method needs some modification, to prevent my view from being moved beyond the screen. I found out how to get maxY and maxY coordinates - for my Samsung Galaxy A6 it's 1080x1920. But the problem is, that my maxY is deep beyond the visible bottom edge of the device.

So my control almost disappears, when reaches Y about 1650. What 300 more pixels go for. I can suppose, that this is NavigationBar height + my control view height, but this also doesn't place my control as expected.

I define max coordinates with this method.

private void setMaxCoordinates(int viewWidth, int viewHeight) {
    Display display = getWindowManager().getDefaultDisplay();
    Point displaySize = new Point();
    display.getSize(displaySize);
    maxX = displaySize.x - viewWidth;
    maxY = displaySize.y - viewHeight;
}

Please, help me to define the correct formula to detect bottom edge coordinate.

1

There are 1 answers

0
Dmytro On BEST ANSWER

I've found a solution. First of all I was wrong with getting bottom coordinates with the help of WindowManager. This just gives you height of your screen in pixels, that is not related to the container. So to detect your bottom coordinate this way, you have to consider:

  • StatusBar height.

  • NavigationBar height.

  • Height of all views that lokated higher, then your container.

  • Height of your own control view (ImageView in my case).

So formula will look like this.

maxY = windowHeight - (statusBarHeight + navBarheight + allUpperViewsHeight + yourViewHeight)

The correct way to define the bootom of your container, is to get the container's height and deduct your control height.

maxY = containerHeight - yourViewHeight;