Android getHeightInPixels() returns wrong value

Android getHeightInPixels() returns wrong value

I’ve created a function that finds the height of an ad so it doesn’t cover game content.

I used jnihelper to call java functions.

public static int getAdViewHeightJni()
   return AdSize.BANNER.getHeightInPixels (appActivity)

It returns 131 based on my phone and emulator.
However, this return value slightly exceeds the size of the ad like below.

The blue part is the height of the value returned by getAdViewHeightJni. The height of the red part is the correct height I want.

How can I get the correct height?

I hope it’s a way to find out when it’s first run. Getting the size of an ad when it is shown is cumbersome because it need to change the position of the game content to don’t overlap the ad when it is shown.

There was an element that I did not consider.
designResolutionHeight / screen height multiplied by the returned pixel value to get the correct value.

auto glview = Director::getInstance()->getOpenGLView();
float rate = glview->getDesignResolutionSize().height / glview->getFrameSize().height;

adHeight = AdmobHelper::getHeight() * rate;

Would Director::getContentScaleFactor() provide the correct value that you need?

For instance:

adHeight = AdmobHelper::getHeight() * Director::getInstance()->getContentScaleFactor();

Thank you for your reply, R101!
I fixed the problem in my second post but could not set it as a solution because the post approval was not completed.

Thank you!

This topic was automatically closed 24 hours after the last reply. New replies are no longer allowed.