yrazlik
yrazlik

Reputation: 10777

How to get the center coordinates 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 it at the center of the screen. Can anyone help me with this?

Thanks

Upvotes: 4

Views: 12731

Answers (1)

ssantos
ssantos

Reputation: 16526

I guess you're correctly getting the center coordinates, but setY is setting the position of the bottom edge of your ImageView. Off the top of my head, you may try something like

SomeImageView.setY(height - SomeImageView.getDrawable().getIntrinsicHeight());

Upvotes: 4

Related Questions