Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

AdMob from Google Play Services will perform undesired auto scrolling

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.

like image 755
Cheok Yan Cheng Avatar asked Nov 14 '13 06:11

Cheok Yan Cheng


1 Answers

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.

like image 56
Imtiyaz Khalani Avatar answered Sep 20 '22 18:09

Imtiyaz Khalani