3

I'm a fairly new Android app programmer trying to implement ads in an app. I'm including a Banner ad in a fragment like so

<ScrollView
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:ads="http://schemas.android.com/apk/res-auto"
    android:id="@+id/meScrollView"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:layout_gravity="center_horizontal"

    <RelativeLayout
        xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:tools="http://schemas.android.com/tools"
        android:paddingLeft="@dimen/activity_horizontal_margin"
        android:paddingRight="@dimen/activity_horizontal_margin"
        android:paddingTop="@dimen/activity_vertical_margin"
        android:paddingBottom="@dimen/activity_vertical_margin"
        tools:context=".MainActivity$PlaceholderFragment"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@+id/mePageRelLayout">

        <com.google.android.gms.ads.AdView
            android:id="@+id/adView"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            ads:adSize="BANNER"
            ads:adUnitId="@string/banner_ad_unit_id"
            android:layout_gravity="center"
            android:layout_centerHorizontal="true"
            android:layout_below="@+id/chart">
        </com.google.android.gms.ads.AdView>

        lots and lots of 
        <TextView
            .../>

        <TextView
            .../>

        <ImageView
            android:id="@+id/chart"
            .../>
    </RelativeLayout>
</ScrollView>

And Java like so:

        AdView mAdView = (AdView) rootView.findViewById(R.id.adView2);
        AdRequest adRequest = new AdRequest.Builder().build();
        mAdView.loadAd(adRequest);

Whenever I go into that fragment, the view starts out fine. I see the TextView and ImageView that I want at the top. Then, the view jumps to the AdView, which I've placed all the way at the bottom of the ScrollView. I'd like it to not jump to the AdView. This happens every few seconds, whenever I scroll away. Why does this happen, and what can I do to fix it?

I've tried placing it other places within the RelativeLayout but it still jumps to wherever it is. I've taken it out of the RelativeLayout and just put it in the ScrollView but then I get an error saying that there isn't any space for the ad (Ad is 320x50, Available space is 0x600, or something like that). I've tried making the Java code the first thing that runs in the fragment, but then it immediately jumps to the AdView and still jumps every few seconds afterwards. My best guess is I have to override some class in AdView?

squeegene
  • 467
  • 1
  • 5
  • 18
  • I'm not going to get into this past this comment, but if the ScrollView is supposed to be the entire screen, you would need to insert it into a view in the ScrollView with other views above and/or below it. If the ScrollView should partially take up the screen then you need to setHeight or setWeight or Above/Below,Top/Bottom or whatever for the views in the parent Layout. – CmosBattery Jan 12 '16 at 00:56
  • So it's in a RelativeLayout inside the ScrollView right now but once I take it out of the RelativeLayout it says that the ScrollView can only host one direct child. I kind of want it to be between certain Views in the RelativeLayout so that you can scroll past the ad if you want. Thank you for helping, however! – squeegene Jan 12 '16 at 03:52
  • Your question is unclear, please elaborate and provide more XML data. – Keshav Jan 12 '16 at 05:54
  • Sorry, I'll try to clarify as best as I can – squeegene Jan 12 '16 at 08:54
  • Found another fix here: http://stackoverflow.com/questions/9842494/how-to-prevent-a-scrollview-from-scrolling-to-a-webview-after-data-is-loaded – squeegene Jan 13 '16 at 02:14

2 Answers2

1

I think you need to add other content to the ScrollView. If there already is other content, try instantiating the ad programatically before everything else in the ScrollView. If you want the ad to be separate from the ScrollView, put it outside but in the same fragment.

AcFreeman
  • 316
  • 1
  • 2
  • 8
  • Thank you for your suggestions! I've tried both; right now it's in a RelativeLayout with other things in the ScrollView, so that you can scroll past it. For some reason it still jumps straight to the ad after a while, but I have a feeling it has to do with ScrollView. I've noticed that every time it jumps, the logcat will print out >Ad is not visible. Not refreshing ad. >Scheduling ad refresh 60000 milliseconds from now. – squeegene Jan 12 '16 at 04:03
1

It is likely that as the adView receives content is it causing itself to briefly receive focus or otherwise cause the containing ScrollView to reposition to show the AdView.

I would strongly recommend moving the AdView outside of the ScrollView as this will

  1. Avoid the problem.
  2. Ensure the AdView is always displayed thereby increasing your revenue.
William
  • 20,150
  • 8
  • 49
  • 91