Add admob to a Drawer navigation layout

Asked

Viewed 182 times

1

I want to put the advertisement in the footer of Activity, but it is at the top by superimposing a list:

<FrameLayout
    android:id="@+id/container"
    android:layout_width="match_parent"
    android:layout_height="match_parent" />

<ListView
    android:id="@+id/list_slidermenu"
    android:layout_width="280dp"
    android:layout_height="match_parent"
    android:layout_gravity="start"
    android:choiceMode="singleChoice"
    android:listSelector="#4DA6FF"
    android:background="#FFF"/>

<LinearLayout
    android:id="@+id/listaPropaganda"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="vertical"/>

Java, on Activity’s oncreate:

AdView ads = new AdView(this);
    ads.setAdUnitId(ANUNCIO_ID);
    ads.setAdSize(com.google.android.gms.ads.AdSize.BANNER);
    LinearLayout layout = (LinearLayout) findViewById(R.id.listaPropaganda);
    layout.addView(ads);

    AdRequest request = new AdRequest.Builder().addTestDevice("1100").build();
    ads.loadAd(request);

I want to put in the footer and filling all the space. How to do this?


<!-- A DrawerLayout is intended to be used as the top-level content view using match_parent for both width and height to consume the full space available. -->

<FrameLayout
    android:id="@+id/container"
    android:layout_width="match_parent"
    android:layout_height="match_parent" />

<ListView
    android:id="@+id/list_slidermenu"
    android:layout_width="280dp"
    android:layout_height="match_parent"
    android:layout_gravity="start"
    android:choiceMode="singleChoice"
    android:listSelector="#4DA6FF"
    android:background="#FFF"/>

<LinearLayout
    android:id="@+id/listaPropaganda"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="vertical"/>

1 answer

2


Replace your layout with the one below. Note that I changed the FrameLayout external by a LinearLayout with android:orientation="vertical". Also note that the property has been added android:layout_weight="1" on your list. Her time has passed to 0dp for optimization purposes, since Weight will define that it should fill all the space left between the two views.

If you prefer, even you can take the listaPropaganda layout and add your banner directly on container.

<LinearLayout
    android:id="@+id/container"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" />

    <ListView
        android:id="@+id/list_slidermenu"
        android:layout_width="280dp"
        android:layout_height="0dp"
        android:layout_weight="1"
        android:layout_gravity="start"
        android:choiceMode="singleChoice"
        android:listSelector="#4DA6FF"
        android:background="#FFF"/>

    <LinearLayout
        android:id="@+id/listaPropaganda"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical"/>

</LinearLayout>
  • 1

    It didn’t work, it made the Drawer menu disappear, follows the original code, in the first disappeared Drawer:

  • Try to put the layout_gravity="start" in the LinearLayout of id "container", the MenuDrawer need to have a gravity.

  • Wakim’s tip was of fundamental importance, and I had to put my list outside of linearlayout, thanks for the help

Browser other questions tagged

You are not signed in. Login or sign up in order to post.