adMob banner covers text view when soft keyboard pops up

巧了我就是萌 提交于 2019-12-05 10:24:50

You can change how the keyboard behaves when it appears over the Ad. Go into your AndroidManifest.xml and add this attribute in the activity tag with the AdMob banner.

android:windowSoftInputMode="adjustPan"

This will prevent the ads from jumping above the keyboard and hiding the input. Instead, they will appear behind the keyboard (hidden). I don't know if this is against AdMob policy, I am just providing a solution.

bat-el.g

I tried setting android:windowSoftInputMode="adjustPan". it hides the adMob banner but also the EdiText. So the solution I found is to hide the adMob banner when the keyboard opens. I learned how to detect if the keyboard opens here. and here is my solution:

final View activityRootView = findViewById(R.id.sample_main_layout);
            activityRootView.getViewTreeObserver().addOnGlobalLayoutListener(new OnGlobalLayoutListener() {
                @Override
                public void onGlobalLayout() {
                    int heightDiff = activityRootView.getRootView().getHeight() - activityRootView.getHeight();
                    if (heightDiff > Support.dpToPx(MainActivity.this, 200)) { // if more than 200 dp, it's probably a keyboard...
                        mAdView.setVisibility(View.GONE);
                    }
                    else{
                        mAdView.setVisibility(View.VISIBLE);
                    }
                 }
            });

add this in your Manifest in activity android:windowSoftInputMode="stateVisible|adjustPan"

This may not be the ideal solution for you but it is what I found to be best for my users. When the keyboard was shown it was covering buttons and text in my WebView making for a bad user experience. To fix this I set my AdView height to the height of the banner ad, in my case 50dp and set the AdView layout to below to the WebView. Finally I set the WebView height to wrap_content. When the keyboard is visible the ad is temporary removed because it runs out of space, when the keyboard is hidden the ad is shown again. Hope this helps.

<WebView
    android:id="@+id/webUI"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_alignParentLeft="true"
    android:layout_alignParentTop="true" />

<com.google.ads.AdView android:id="@+id/adView"
    android:layout_width="wrap_content"
    android:layout_height="50dp"
    android:layout_below="@+id/webUI" />
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!