Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

AdMob AdView and ViewPager?

I have Android application with ViewPager implemented like this :

http://www.youtube.com/watch?v=C6_1KznO95A

I don't know how to implement AdView below the ViewPager. I have tried to put AdView below the ViewPager programmatically and in xml. There is no errors but ads are not showing.

My xml:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">

<android.support.v4.view.ViewPager
    android:id="@+id/pager"
    android:layout_width="match_parent"
    android:layout_alignParentBottom="top"
    android:layout_height="0dp"
    android:layout_weight="1"/>

<com.google.ads.AdView
    android:id="@+id/ad"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentLeft="true"
    android:layout_alignParentRight="true"
    android:layout_alignParentBottom="true"
    android:layout_below="@+id/pager"
    ads:adSize="BANNER"
    ads:adUnitId="@string/admob_publisher_id"
    ads:loadAdOnCreate="true" >
</com.google.ads.AdView>

like image 367
GavriloPrincip Avatar asked Jan 10 '14 17:01

GavriloPrincip


People also ask

How to integrate ads in android app?

To configure your app to carry ads, you must integrate an Google Play Instant-compatible ad-network SDK into your app. The Google Mobile Ads Lite SDK is one such SDK. For more information about integrating an ad-network SDK, see the guides related to Google Mobile Ads Lite SDK and Google AdMob.

What is android banner?

Banner ads occupy a spot within an app's layout, either at the top or bottom of the device screen. They stay on screen while users are interacting with the app, and can refresh automatically after a certain period of time.

What is ViewPager?

ViewPager is a layout manager that allows the user to flip left and right through pages of data. It is mostly found in apps like Youtube, Snapchat where the user shifts right – left to switch to a screen. Instead of using activities fragments are used.


1 Answers

OK, you have a couple of problems.

  1. orientation is only relevant for a LinearLayout.
  2. layout_alignParentBottom needs a boolean value not "top"
  3. layout_weight is only relevant for a LinearLayout nor RelativeLayout

You have 2 options. Use a RelativeLayout but define the AdView first with the ViewPager being defined as above it OR use a LinearLayout and have the ViewPager fill the unused space using layout_weight.

As RelativeLayout

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
>

<com.google.ads.AdView
    android:id="@+id/ad"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_alignParentBottom="true"
    ads:adSize="BANNER"
    ads:adUnitId="@string/admob_publisher_id"
    ads:loadAdOnCreate="true" />

<android.support.v4.view.ViewPager
    android:id="@+id/pager"
    android:layout_width="match_parent"
    android:layout_height="0dp"
    android:layout_alignParentTop="true"
    android:layout_above="@+id/ad"
    />

</RelativeLayout>

OR as LinearLayout

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">

<android.support.v4.view.ViewPager
    android:id="@+id/pager"
    android:layout_width="match_parent"
    android:layout_height="0dp"
    android:layout_weight="1"/>

<com.google.ads.AdView
    android:id="@+id/ad"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    ads:adSize="BANNER"
    ads:adUnitId="@string/admob_publisher_id"
    ads:loadAdOnCreate="true" />

</LinearLayout>

Personally I'd go with the LinearLayout it feels simpler.

like image 86
William Avatar answered Oct 08 '22 09:10

William