8

I were using AdMob from Google Play services version 13. I realize, when I place the advertisement within ScrollView, AdMob will try to perform undesired auto scrolling, after it fetches the advertisement successfully from server.

package com.example.admob_bug;

import com.google.android.gms.ads.AdRequest;
import com.google.android.gms.ads.AdSize;
import com.google.android.gms.ads.AdView;

import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.widget.LinearLayout;

public class MainActivity extends Activity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        // Create the adView.
        adView = new AdView(this);
        adView.setAdUnitId("a151b03485063e0");
        adView.setAdSize(AdSize.BANNER);

        // Lookup your LinearLayout assuming it's been given
        // the attribute android:id="@+id/mainLayout".
        LinearLayout layout = (LinearLayout)findViewById(R.id.advertisement);

        // Add the adView to it.
        layout.addView(adView);

        // Initiate a generic request.
        AdRequest adRequest = new AdRequest.Builder().build();

        // Load the adView with the ad request.
        adView.loadAd(adRequest);        
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }

    @Override
    public void onPause() {
        adView.pause();
        super.onPause();
    }

    @Override
    public void onResume() {
        super.onResume();
        adView.resume();
    }

    @Override
    public void onDestroy() {
        adView.destroy();
        super.onDestroy();
    }

    private AdView adView;    
}

<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/scrollView1"
    android:layout_width="match_parent"
    android:layout_height="match_parent" >

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical" >

        ...
        ...

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Large Text 7"
            android:layout_margin="30dp"
            android:textAppearance="?android:attr/textAppearanceLarge" />

        <LinearLayout
            android:id="@+id/advertisement"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="vertical" >

        </LinearLayout>

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Large Text 8"
            android:layout_margin="30dp"
            android:textAppearance="?android:attr/textAppearanceLarge" />        

    </LinearLayout>

</ScrollView>

The complete source code can be downloaded from https://www.dropbox.com/s/e53zjqsc5cnilz2/admob_bug.zip

You will realize this problem, after you wait for around 10 seconds (Depending on network quality), after advertisement is loaded.

Is there any workaround to prevent auto scrolling?

This problem wasn't there, before I switch from old AdMob 6.4.1 JAR, to AdMob of Google Play Service.

I'm testing using device Nexus S, Android 4.1.2.

Rich
  • 36,270
  • 31
  • 115
  • 154
Cheok Yan Cheng
  • 47,586
  • 132
  • 466
  • 875
  • I don't see any ScrollView in your XML or your code. – William Nov 15 '13 at 21:59
  • Firstly, there is no need for the LinearLayout immediately inside the ScrollView, you can remove it. Just to be clear is your AdView consume space before the ad is loaded? – William Nov 21 '13 at 10:16
  • It doesn't consume any space, before ad is loaded. – Cheok Yan Cheng Nov 26 '13 at 03:59
  • 2
    You need LinearLayout inside ScrollView. Please refer `A ScrollView is a FrameLayout, meaning you should place one child in it containing the entire contents to scroll; this child may itself be a layout manager with a complex hierarchy of objects. A child that is often used is a LinearLayout in a vertical orientation, presenting a vertical array of top-level items that the user can scroll through.` – Cheok Yan Cheng Nov 26 '13 at 04:01
  • Ack, my bad. What is strange is that your AdView is not consuming any space before the ad is loaded. It would normally do so. Why don't you move your AdView outside the ScrollView. – William Nov 26 '13 at 11:18
  • Just FYI, I post a similar post to https://groups.google.com/forum/#!searchin/google-admob-ads-sdk/cheok/google-admob-ads-sdk/jpq_1cykTjA/1iRNn0f0ekEJ too – Cheok Yan Cheng Feb 08 '14 at 08:24

2 Answers2

12

update your activity_main.xml

 <LinearLayout
        android:id="@+id/advertisement"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:descendantFocusability="blocksDescendants"
        android:orientation="vertical" >
    </LinearLayout>

it is working fine. testing with nexus 4 and samsung galaxy s.also emulator api 18.

Imtiyaz Khalani
  • 2,037
  • 18
  • 32
  • 4
    Alternatively, you can add `android:descendantFocusability="blocksDescendants"` to your `PublisherAdView`, or do it in code `adView.setDescendantFocusability(ViewGroup.FOCUS_BLOCK_DESCENDANTS)`, so you don't need to mess up with your root view. – Wenhui May 01 '14 at 17:16
  • Talk about devaluing your own platform. Seems like the ad division and the android division aren't talking to each other, either way what in the actual hell are they thinking releasing this kind of bug? – sleep Aug 15 '15 at 13:47
0

I didn't test this with your code, I am just assuming: I think the problem is that you didn't reserve space within your layout for the banner and once the ad is loaded it is squeezing into the rest.

Maybe try adding some min height to your layout to reserve space

    <LinearLayout
        android:id="@+id/advertisement"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:minHeight="50dp"
        android:orientation="vertical" >

    </LinearLayout>

Or maybe even better define your admob ad view directly inside the xml and then set loadAdOncreate to true and replace your linearLayout for the ad with something like this:

<com.google.ads.AdView android:id="@+id/adView"
    xmlns:ads="http://schemas.android.com/apk/lib/com.google.ads"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    ads:refreshInterval="60"
    ads:adUnitId="xxxxxxxxxxxxxx"
    ads:adSize="SMART_BANNER"
    ads:testDevices="TEST_EMULATOR"
    ads:loadAdOnCreate="true"/>
donfuxx
  • 11,277
  • 6
  • 44
  • 76
  • In fact, it is difficult for me to pre-reserve space for AdMob banner, as the number of UI components added to layout, are being dynamically generated. So, there might chance where we shouldn't suppose to show up the AdMob. – Cheok Yan Cheng Feb 13 '14 at 01:19
  • Just an idea: Maybe don't add your ad in the onCreate method already. Try adding & loading it later once you dynamically generated the rest of UI components and once you have checked that the ad will be actually visible without scrolling? – donfuxx Feb 13 '14 at 20:09
  • Sorry. Your proposed solution doesn't work for me. It still having "auto scrolling" bug effect. – Cheok Yan Cheng Feb 14 '14 at 12:40