Search code examples
javaandroidandroid-layoutadmobbanner

Admob banner wrong position


I have a android game written with JAVA, for some unknown reason, Admob banner is displayed in wrong position on small percentage of our players devices, I have already tested it on 30+ devices, but could not reproduce it, some players sent us proofs though.

By design, our banner should ALWAYS be displayed in TOP RIGHT corner of the screen, but sometimes its getting displayed in TOP LEFT, any idea will be greatly appreciated.

That's how I initialise it (I am doing everything programatically, no xml's

    @Override
    protected void onSetContentView()
    {
        final RelativeLayout relativeLayout = new RelativeLayout(this);

        final FrameLayout.LayoutParams relativeLayoutLayoutParams = new FrameLayout.LayoutParams(RelativeLayout.LayoutParams.MATCH_PARENT, RelativeLayout.LayoutParams.MATCH_PARENT);

        this.mRenderSurfaceView = new RenderSurfaceView(this);
        this.mRenderSurfaceView.setRenderer(this.mEngine, this);

        final android.widget.RelativeLayout.LayoutParams surfaceViewLayoutParams = new RelativeLayout.LayoutParams(BaseGameActivity.createSurfaceViewLayoutParams());
        surfaceViewLayoutParams.addRule(RelativeLayout.CENTER_HORIZONTAL);

        relativeLayout.addView(this.mRenderSurfaceView, surfaceViewLayoutParams);

        FrameLayout frameLayout = new FrameLayout(this);

        FrameLayout.LayoutParams fparams=new FrameLayout.LayoutParams( FrameLayout.LayoutParams.MATCH_PARENT, FrameLayout.LayoutParams.MATCH_PARENT);

        adView = new AdView(this);

        final FrameLayout.LayoutParams adViewLayoutParams = new FrameLayout.LayoutParams(
                FrameLayout.LayoutParams.WRAP_CONTENT,
                FrameLayout.LayoutParams.WRAP_CONTENT,
                Gravity.END | Gravity.TOP);

        adView = new AdView(this);
        adView.setAdSize(com.google.android.gms.ads.AdSize.BANNER);
        adView.setAdUnitId("....");
        adView.setEnabled(false);
        adView.refreshDrawableState();
        adView.setVisibility(AdView.GONE);
        AdRequest adRequest = new AdRequest.Builder().build();
        adView.loadAd(adRequest);
        adView.refreshDrawableState();

        frameLayout.addView(adView,adViewLayoutParams);

        relativeLayout.addView(frameLayout,fparams);

        this.setContentView(relativeLayout, relativeLayoutLayoutParams);
    }

Show/hide methods:

public void hideBanner()
    {
        if (adDisplayed)
        {
            this.runOnUiThread(new Runnable()
            {
                @Override
                public void run() {
                    adView.setEnabled(false);
                    adView.setVisibility(View.INVISIBLE);
                    adDisplayed = false;
                }
            });
        }
    }

    public void showBanner()
    {
        if (!ResourcesManager.getInstance().isNoAdverts())
        {
            if (!adDisplayed)
            {
                this.runOnUiThread(new Runnable()
                {
                    @Override
                    public void run()
                    {
                        AdRequest adRequest = new AdRequest.Builder().build();
                        adView.loadAd(adRequest);

                        adView.setAdListener(new com.google.android.gms.ads.AdListener()
                        {
                            @Override
                            public void onAdLoaded()
                            {
                                adView.setVisibility(View.GONE);
                                adView.setEnabled(true);
                                adView.setVisibility(AdView.VISIBLE);
                            }
                        });

                        adDisplayed = true;
                    }
                });
            }
        }
    }

Here are my project settings:

    compileSdkVersion 28
    minSdkVersion 16
    targetSdkVersion 28

This issue was reported from players with SDK: 23


Solution

  • Given that the problem was reported by a user in Israel, I suspect that the user has their device's language set to a right-to-left language, like Hebrew.

    Since you are using Gravity.END, this will place the element on the left side of the device in this scenario (since, after all, that is the "end" for this user). If you want the element to always be on the right, regardless of the normal layout direction for your user's chosen language, build your LayoutParams with Gravity.RIGHT instead.

    Alternatively, you could disable right-to-left support for your application. Check the <application> tag in your AndroidManifest.xml and delete this attribute:

    android:supportsRtl="true"