How to get the coordinates of the center of the screen in android?

I am trying to get the x and y coordinates of the center of the screen. Here is what I tried:

Display display = getWindowManager().getDefaultDisplay(); final Point size = new Point(); display.getSize(size); height = size.y; float centerY=height/2;//i expect this is the y coordinate of center 

But this does not work when I say:

 SomeImageView.setY(centerY); 

I do not see him in the center of the screen. Can anyone help me with this?

thanks

+4
source share
1 answer

I think you get the center coordinates correctly, but setY sets the position of the bottom edge of your ImageView . Above my head you can try something like

 SomeImageView.setY(height - SomeImageView.getDrawable().getIntrinsicHeight()); 
+4
source

Source: https://habr.com/ru/post/1497707/


All Articles