Search code examples
androidandroid-listviewadmobsticky-footer

Set at the bottom of the screen an admob banner after a listview


I have an xml layout which is displaying a listview. I also want to display an admob banner just below this listview and I want to display it always in the bottom regardless of user scrolling the listview. I have this xml layout:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:orientation="vertical">
<LinearLayout android:layout_width="fill_parent"
              android:id="@+id/home_layout"
              android:orientation="vertical"
              android:layout_height="wrap_content">
    <!-- Put all your application views here, such as buttons, textviews, edittexts and so on -->
    <ListView
    android:id="@+id/lvImages"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"     
    android:dividerHeight="1dp" />
</LinearLayout>
<LinearLayout android:layout_width="match_parent"
            android:orientation="horizontal"
              android:id="@+id/footerLayout"
              android:layout_height="wrap_content"
              android:gravity="bottom"
              android:layout_centerHorizontal="true"
              android:layout_alignParentBottom="true"
              android:layout_alignBottom="@+id/home_layout">

</LinearLayout>

And I have in onCreate method the creation of the Admob banner:

protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_listado_imagenes_subidas_main);

    // Crear adView.
    adView = new AdView(this);
    adView.setAdUnitId("XXXXXXXXXXXXXXXXXXX");
    adView.setAdSize(AdSize.BANNER);

    LinearLayout layout = (LinearLayout)findViewById(R.id.footerLayout);
    layout.addView(adView);
    AdRequest adRequest = new AdRequest.Builder().build();
    adView.loadAd(adRequest);

    // load list application
    listImagenes = (ListView)findViewById(R.id.lvImages);

    // create new adapter
    ImagenesAdapter adapter = new ImagenesAdapter(this, ListadoImagenes());
    // set adapter to list view
    listImagenes.setAdapter(adapter);
}

How can I display a sticky footer area below the listview?

Thank you in advance.


Solution

  • You need to modify your layout a little bit.

    <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
                    android:layout_width="match_parent"
                    android:layout_height="match_parent"
                    android:orientation="vertical">
        <LinearLayout
                android:id="@+id/home_layout"
                android:layout_width="fill_parent"
                android:layout_height="match_parent"
                android:orientation="vertical"
                android:layout_above="@+id/footerLayout">
            <!-- Put all your application views here, such as buttons, textviews, edittexts and so on -->
            <ListView
                    android:id="@+id/lvImages"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:dividerHeight="1dp"/>
        </LinearLayout>
        <LinearLayout
                android:id="@+id/footerLayout"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:orientation="horizontal"
                android:gravity="bottom"
                android:layout_centerHorizontal="true"
                android:layout_alignParentBottom="true"
                >
        </LinearLayout>
    </RelativeLayout>