开发者

adMob banner covers text view when soft keyboard pops up

开发者 https://www.devze.com 2023-03-10 10:37 出处:网络
I have been having troubles keeping an adMob banner from rolling up along the soft keyboard and covering my text views....

I have been having troubles keeping an adMob banner from rolling up along the soft keyboard and covering my text views....

This is the link to what I am trying to achieve in Android: http://bit.ly/myfoodcalc

I know my layout is a little bit complex, too many nested views (maybe), etc.

I have tryed changing things on the Manifest without success.

For you to understand it better here is son pseudo XML (and the full XML later on the post)...

 <ScrollView>
      <Linear layout>
            <ImageView>
            <TableView>
                  <Several rows with a Edit Text widget>
            </TableView>
            </ImageView>
      </LinearLayout>
  </ScrollView>

  <FrameLayout>
       <ImageView>
  </FrameLayout>

An here is my complete XML

<RelativeLayout     xmlns:android="http://schemas.android.com/apk/res/android" 
                    xmlns:ads="http://schemas.android.com/apk/lib/com.google.ads" 
                    android:layout_width="fill_parent" 
                    android:layout_height="fill_parent" 
                    android:id="@+id/relativeLayout"
                    android:background="@drawable/backgroundwithtext">

    <ScrollView android:id="@+id/scrollView1" 
                android:layout_width="fill_parent" 
                android:layout_height="fill_parent">

        <LinearLayout   android:id="@+id/linearLayout1" 
                        android:layout_width="fill_parent" 
                        android:layout_height="fill_parent" 
                        android:orientation="vertical" 
                        android:gravity="center_horizontal">

            <ImageView  android:id="@+id/imageView1" 
                        android:layout_width="wrap_content" 
                        android:src="@drawable/icon" 
                        android:layout_height="wrap_content"></ImageView>

            <TableLayout    android:id="@+id/tableLayout1" 
                            android:layout_height="wrap_content" 
                            android:layout_width="wrap_content" 
                            android:background="@drawable/backgroundsmallobj">

                <TableRow   android:id="@+id/tableRow1" 
                            android:layout_width="wrap_content" 
                            android:layout_height="wrap_content">

                    <TextView   android:text="TITLE" 
                                android:layout_width="wrap_content" 
                                android:layout_height="wrap_content" 
                                android:id="@+id/textView1">
                    </TextView>

                </TableRow>

                <TableRow   android:id="@+id/tableRow2" 
                            android:layout_width="wrap_content" 
                            android:layout_height="wrap_content">

                    <EditText   android:text="EditText" 
                                android:maxLines="1" 
                                android:id="@+id/editText2" 
                                android:layout_width="wrap_content"     
                                android:layout_height="wrap_content" 
                                android:imeOptions="actionNext">
                    </EditText>

                </TableRow>

                <TableRow   android:id="@+id/tableRow3" 
                            android:layout_width="wrap_content" 
                            android:layout_height="wrap_content">

                    <EditText   android:text="EditText" 
                                android:maxLines="1" 
                                android:id="@+id/editText3" 
                                android:layout_width="wrap_content" 
                                android:layout_height="wrap_content"
                                android:imeOptions="actionNext">
                    </EditText>

                </TableRow>

                <TableRow android:id="@+id/tableRow4" android:layout_width="wrap_content" android:layout_height="wrap_content">

                    <EditText   android:text="EditText" 
                                android:maxLines="1" 
                                android:id="@+id/editText4" 
                                android:layout_width="wrap_content" 
                                andro开发者_C百科id:layout_height="wrap_content" 
                                android:imeOptions="actionNext">
                    </EditText>

                </TableRow>

                <TableRow   android:id="@+id/tableRow5" 
                            android:layout_width="wrap_content" 
                            android:layout_height="wrap_content">

                    <EditText   android:text="EditText"
                                android:maxLines="1" 
                                android:id="@+id/editText5" 
                                android:layout_width="wrap_content" 
                                android:layout_height="wrap_content" 
                                android:imeOptions="actionNext">
                    </EditText>

                </TableRow>

                <TableRow android:id="@+id/tableRow6" android:layout_width="wrap_content" android:layout_height="wrap_content">
                    <EditText android:text="EditText" android:maxLines="1" android:id="@+id/editText6" android:layout_width="wrap_content" android:layout_height="wrap_content" android:imeOptions="actionDone"></EditText>
                </TableRow>

                <TableRow android:id="@+id/tableRow7" android:layout_width="wrap_content" android:layout_height="wrap_content">
                    <Button android:id="@+id/button1" android:layout_height="wrap_content" android:text="Button" android:layout_width="wrap_content"></Button>
                </TableRow>


            </TableLayout>

        </LinearLayout>

    </ScrollView>

    <FrameLayout    android:id="@+id/frameLayout1" 
                    android:layout_height="wrap_content" 
                    android:layout_alignParentBottom="true" 
                    android:layout_width="fill_parent">

        <com.google.ads.AdView android:id="@+id/adView"
                     android:layout_width="fill_parent"
                     android:layout_height="wrap_content"
                     ads:adUnitId="############"
                     ads:adSize="BANNER"
                     ads:loadAdOnCreate="true" />

    </FrameLayout>

</RelativeLayout>


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.


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" />


I had a similar issue in my Ionic app. I wanted the Ad to completely be hidden when the soft keyboard shows up, but be visible when the soft keyboard is dismissed. I solved it by adding the following in the <platform name="android"> tag of config.xml:

<platform name="android">
        <edit-config file="app/src/main/AndroidManifest.xml" mode="merge" target="/manifest/application/activity">
            <activity android:windowSoftInputMode="adjustPan|adjustResize" />
        </edit-config>
</platform>
0

精彩评论

暂无评论...
验证码 换一张
取 消