There is no method to automatically change your Android app’s container size for showing Google AdMob ads. While it does re-adjust itself whenever the orientation changes, it does not resize itself to the correct size.

Try changing your device’s orientation from portrait to landscape mode and you will see that the height is the same. The result is that the height of the ad container shown is too big just like when its orientation is in portrait mode.

The workaround to make the ad container resize correctly according to orientation change like the image below is to remove the ad container in your layout view and add it back.


Say this is your code for adding the ad container view to your layout view.

Then place the method in the onConfigurationChanged().

This one is tricky because I totally was lost until I luckily came across a forum post that explained how Android works behind the scenes during orientation change.

So my Android widget has two layouts, one visible and one hidden. The visible layout is default while the hidden layout will only be shown if data is present.

When orientation change happens (portrait, landscape), the problem is that the hidden layout which I programmaticaly set to visible is not shown.

Use Jona from Stack Overflow mentioned this important note that solved my problem.

“When the screen is rotated and the widget needs to be restored the Android system will restore it using the data from the last RemoteViews update. I was updating parts of the widget separately so when it needed to restore only one part got updated. The fix is always update everything on your RemoteViews all at once.”

So that is the solution. Whenever you instantiate and inflate RemoteViews, be sure that you also set the View states on everything.

Related Posts Plugin for WordPress, Blogger...