3

I want to display a simple banner ad in RecyclerView which loads a list of images. just a simple bottom banner ad.

This is my RecyclerViewXML

<?xml version="1.0" encoding="utf-8"?>
<android.support.v7.widget.RecyclerView
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/list"
android:name="com.example.fragmentactivity"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#FAC873"
app:layoutManager="StaggeredGridLayoutManager"
tools:context="com.example.fragmentactivity"
tools:listitem="@layout/fragment_image" />

if I try to put to the recyclerView inside RelativeLayout or any other so i could include banner in the bottom, but then it wont display any images.

How can I display banner in that view?

I tried almost all the available solution to dynamically add the banner but nothing worked

Atiq
  • 14,435
  • 6
  • 54
  • 69

2 Answers2

0

You should add the banner to the bottom and in the front of the RecyclerView. For do that this will help you:

Usage: Replace the RelativeLayout for your AdMobView

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

        <android.support.v7.widget.RecyclerView
            android:id="@+id/list"
            android:name="com.example.fragmentactivity"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:background="#FAC873"
            app:layoutManager="StaggeredGridLayoutManager"
            tools:context="com.example.fragmentactivity" />

        <RelativeLayout
            android:layout_width="match_parent"
            android:layout_height="52dp"
            android:background="#fff"
            android:layout_alignParentBottom="true" />
    </RelativeLayout>

Explanation: . The RelativeLayout is bellow RecyclerView and it is android:layout_alignParentBottom="true" overlaying the RecyclerView.

diogojme
  • 2,309
  • 1
  • 19
  • 25
  • still no luck, whenever i put `recyclerview` inside any layout then it shows no images :( lets assume this is fragmentB, is there anyway i could access banner from fragmentA and show it here too without modifying xml? – Atiq Jan 21 '16 at 03:52
  • Yes it's possible. But I think this is not the problem. Can you edit and post the code you are using to load the banner from Activity class? I think the banner id or the call its not correctly. – diogojme Jan 21 '16 at 14:40
0

I have it working perfectly well by first adding the adview at the bottom and then adding the recycler view above it (leaving a margin at the bottom for the adview that was placed earlier and then filling the parent with the recycler view). My layout is as follows:

Note: In my case it is MoPubView Replace it with your Adview from Admob.

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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:layout_width="match_parent"
    android:layout_height="match_parent" android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="5dp"
    android:paddingBottom="5dp"
    android:background="@color/color_green"
    app:layout_behavior="@string/appbar_scrolling_view_behavior"
    tools:showIn="@layout/app_bar_category_list"
    tools:context=".CategoryListActivity">

    <!-- I'm using Mopub, replace the MoPubView with AdView in your case -->
    <com.mopub.mobileads.MoPubView
        android:id="@+id/adview"
        android:layout_width="fill_parent"
        android:layout_height="50dp"
        android:layout_centerHorizontal="true"
        android:layout_alignParentBottom="true" />

    <android.support.v7.widget.RecyclerView
        android:id="@+id/my_recycler_view"
        android:layout_width="match_parent"
        android:layout_height="fill_parent"
        android:scrollbars="vertical"
        android:layout_marginBottom="60dp" />

</RelativeLayout>
ArnonZ
  • 3,822
  • 4
  • 32
  • 42
Viral Patel
  • 32,418
  • 18
  • 82
  • 110