androidandroid-layoutandroid-linearlayoutadbannerview

AdView Banner inside Tabbed Activity


I'm trying to show banner ad inside Linear Layout activity that has tabs(which are fragments).

But whatever I try, the ad banner doesn't show up. Here is my code:

XML:

activity_hometopnav.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:id="@+id/activity_hometabs"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    tools:context="com.vossenthemes.swappuser.activities.homepages.HomeTopNavActivity">

    <android.support.design.widget.AppBarLayout
        android:id="@+id/appbarlayout"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:theme="@style/AppTheme.AppBarOverlay"
        app:elevation="0dp">

        <android.support.v7.widget.Toolbar
            android:id="@+id/toolbar"
            android:layout_width="match_parent"
            android:layout_height="?attr/actionBarSize"
            android:background="?attr/colorPrimary"
            app:popupTheme="@style/AppTheme.PopupOverlay" />

    </android.support.design.widget.AppBarLayout>

    <android.support.design.widget.TabLayout
        android:id="@+id/top_navigation"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        app:tabMode="fixed"
        android:background="?attr/colorPrimary"
        android:minHeight="?attr/actionBarSize"
        app:tabSelectedTextColor="@android:color/white"
        app:tabIndicatorColor="@color/colorAccentLight"
        app:tabGravity="fill"
        app:tabTextColor="@color/colorAccentLight"
        />

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

    <com.google.android.gms.ads.AdView
        xmlns:ads="http://schemas.android.com/apk/res-auto"
        android:id="@+id/adView2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerHorizontal="true"
        android:layout_alignParentBottom="true"
        android:layout_gravity="bottom|center"
        ads:adSize="BANNER"
        ads:adUnitId="@string/admob_banner_id">
    </com.google.android.gms.ads.AdView>

</LinearLayout>

fragment_home.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    xmlns:ads="http://schemas.android.com/apk/res-auto">


    <android.support.v4.widget.SwipeRefreshLayout
        xmlns:android="http://schemas.android.com/apk/res/android"
        android:id="@+id/swipeRefreshLayout"
        android:layout_width="match_parent"
        android:layout_height="match_parent">

        <WebView
            android:id="@+id/webView"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            />

    </android.support.v4.widget.SwipeRefreshLayout>

    <FrameLayout
        android:id="@+id/frameLayout"
        android:background="@android:color/transparent"
        android:layout_width="match_parent"
        android:layout_height="3dp">

        <ProgressBar
            style="?android:attr/progressBarStyleHorizontal"
            android:background="@android:color/transparent"
            android:progressDrawable="@drawable/progress_color"
            android:id="@+id/progressBar"
            android:layout_width="match_parent"
            android:layout_height="8dp"
            android:layout_marginTop="-3dp"
            android:layout_gravity="top"
            android:max="100"
            android:progress="20"/>

    </FrameLayout>

JAVA:

HomeTopNavActivity.java

 MobileAds.initialize(this, getString(R.string.admob_banner_id));
 bannerAd = findViewById(R.id.adView2);
 bannerAd.loadAd(adRequest);

I'm trying to show banner ad inside Linear Layout activity that has tabs(which are fragments).

But whatever I try, the ad banner doesn't show up.


Solution

  • Your Viewpager's height is match_parent. So there is no room for adview. You can use android:layout_weight for this problem. For example:

      <android.support.v4.view.ViewPager
            android:id="@+id/pager"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_weight="1"/>
    
      <com.google.android.gms.ads.AdView
            xmlns:ads="http://schemas.android.com/apk/res-auto"
            android:id="@+id/adView2"
            android:layout_width="wrap_content"
            android:layout_height="64dp"       <--- You can change height
            android:layout_centerHorizontal="true"
            android:layout_alignParentBottom="true"
            android:layout_gravity="bottom|center"
            ads:adSize="BANNER"
            ads:adUnitId="@string/admob_banner_id">
      </com.google.android.gms.ads.AdView>