tcollart
tcollart

Reputation: 1062

How to display Admob on the top of my Layout using XML on Android?

I'm kind of struggling with the new AdMob. I want my ad to display on the top as on the image below:

a

I'm doing what the google documentation says but the ad always appears on the bottom (after all my other views)...

Notes:

I tried many different techniques and did a lot of researches but all the questions and answers I find (on SO and other websites) concern the "old" AdMob display (pre winter 2013).

Here is on of my XML file, the interesting part is certainly on the top, after the com.google.android.gms.ads.AdView it's only a "grid" made in LinearLayout followed by one bigger button:

<?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:ads="http://schemas.android.com/apk/res-auto"
    android:id="@+id/mainLayout"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@drawable/gradient"
    android:orientation="vertical"
    tools:context=".MainActivity" >

 <com.google.android.gms.ads.AdView
        android:id="@+id/adView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        ads:adSize="BANNER"
        ads:adUnitId="MY HIDDEN AD UNIT ID" />


 <!-- The part below is a grid made of 4 Linear Layouts displaying buttons -->

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:gravity="center"
    android:orientation="horizontal" >

    <Button
        android:id="@+id/Button1"
        android:layout_width="60dp"
        android:layout_height="60dp"
        android:layout_margin="10dp"
        android:alpha="0.5"
        android:background="#424242"
        android:clickable="false"
        android:scaleType="fitCenter"
        android:text="1"
        android:textColor="#fff"
        android:textSize="24sp"
        android:textStyle="bold" />

    <Button
        android:id="@+id/Button2"
        android:layout_width="60dp"
        android:layout_height="60dp"
        android:layout_margin="10dp"
        android:alpha="0.5"
        android:background="#424242"
        android:clickable="false"
        android:enabled="false"
        android:scaleType="fitCenter"
        android:text="2"
        android:textColor="#fff"
        android:textSize="24sp"
        android:textStyle="bold" />

    <Button
        android:id="@+id/Button3"
        android:layout_width="60dp"
        android:layout_height="60dp"
        android:layout_margin="10dp"
        android:alpha="0.5"
        android:background="#424242"
        android:clickable="false"
        android:enabled="false"
        android:scaleType="fitCenter"
        android:text="3"
        android:textColor="#fff"
        android:textSize="24sp"
        android:textStyle="bold" />

    <Button
        android:id="@+id/Button4"
        android:layout_width="60dp"
        android:layout_height="60dp"
        android:layout_margin="10dp"
        android:alpha="0.5"
        android:background="#424242"
        android:clickable="false"
        android:enabled="false"
        android:scaleType="fitCenter"
        android:text="4"
        android:textColor="#fff"
        android:textSize="24sp"
        android:textStyle="bold" />
</LinearLayout>

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:gravity="center"
    android:orientation="horizontal" >

    <Button
        android:id="@+id/Button5"
        android:layout_width="60dp"
        android:layout_height="60dp"
        android:layout_margin="10dp"
        android:alpha="0.5"
        android:background="#424242"
        android:clickable="false"
        android:enabled="false"
        android:scaleType="fitCenter"
        android:text="5"
        android:textColor="#fff"
        android:textSize="24sp"
        android:textStyle="bold" />

    <Button
        android:id="@+id/Button6"
        android:layout_width="60dp"
        android:layout_height="60dp"
        android:layout_margin="10dp"
        android:alpha="0.5"
        android:background="#424242"
        android:clickable="false"
        android:enabled="false"
        android:scaleType="fitCenter"
        android:text="6"
        android:textColor="#fff"
        android:textSize="24sp"
        android:textStyle="bold" />

    <Button
        android:id="@+id/Button7"
        android:layout_width="60dp"
        android:layout_height="60dp"
        android:layout_margin="10dp"
        android:alpha="0.5"
        android:background="#424242"
        android:clickable="false"
        android:enabled="false"
        android:scaleType="fitCenter"
        android:text="7"
        android:textColor="#fff"
        android:textSize="24sp"
        android:textStyle="bold" />

    <Button
        android:id="@+id/Button8"
        android:layout_width="60dp"
        android:layout_height="60dp"
        android:layout_margin="10dp"
        android:alpha="0.5"
        android:background="#424242"
        android:clickable="false"
        android:enabled="false"
        android:scaleType="fitCenter"
        android:text="8"
        android:textColor="#fff"
        android:textSize="24sp"
        android:textStyle="bold" />
</LinearLayout>

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:gravity="center"
    android:orientation="horizontal" >

    <Button
        android:id="@+id/Button9"
        android:layout_width="60dp"
        android:layout_height="60dp"
        android:layout_margin="10dp"
        android:alpha="0.5"
        android:background="#424242"
        android:clickable="false"
        android:enabled="false"
        android:scaleType="fitCenter"
        android:text="9"
        android:textColor="#fff"
        android:textSize="24sp"
        android:textStyle="bold" />

    <Button
        android:id="@+id/Button10"
        android:layout_width="60dp"
        android:layout_height="60dp"
        android:layout_margin="10dp"
        android:alpha="0.5"
        android:background="#424242"
        android:clickable="false"
        android:enabled="false"
        android:scaleType="fitCenter"
        android:text="10"
        android:textColor="#fff"
        android:textSize="24sp"
        android:textStyle="bold" />

    <Button
        android:id="@+id/Button11"
        android:layout_width="60dp"
        android:layout_height="60dp"
        android:layout_margin="10dp"
        android:alpha="0.5"
        android:background="#424242"
        android:clickable="false"
        android:enabled="false"
        android:scaleType="fitCenter"
        android:text="11"
        android:textColor="#fff"
        android:textSize="24sp"
        android:textStyle="bold" />

    <Button
        android:id="@+id/Button12"
        android:layout_width="60dp"
        android:layout_height="60dp"
        android:layout_margin="10dp"
        android:alpha="0.5"
        android:background="#424242"
        android:clickable="false"
        android:enabled="false"
        android:scaleType="fitCenter"
        android:text="12"
        android:textColor="#fff"
        android:textSize="24sp"
        android:textStyle="bold" />
</LinearLayout>

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:gravity="center"
    android:orientation="horizontal" >

    <Button
        android:id="@+id/Button13"
        android:layout_width="60dp"
        android:layout_height="60dp"
        android:layout_margin="10dp"
        android:alpha="0.5"
        android:background="#424242"
        android:clickable="false"
        android:enabled="false"
        android:scaleType="fitCenter"
        android:text="13"
        android:textColor="#fff"
        android:textSize="24sp"
        android:textStyle="bold" />

    <Button
        android:id="@+id/Button14"
        android:layout_width="60dp"
        android:layout_height="60dp"
        android:layout_margin="10dp"
        android:alpha="0.5"
        android:background="#424242"
        android:clickable="false"
        android:enabled="false"
        android:scaleType="fitCenter"
        android:text="14"
        android:textColor="#fff"
        android:textSize="24sp"
        android:textStyle="bold" />

    <Button
        android:id="@+id/Button15"
        android:layout_width="60dp"
        android:layout_height="60dp"
        android:layout_margin="10dp"
        android:alpha="0.5"
        android:background="#424242"
        android:clickable="false"
        android:enabled="false"
        android:scaleType="fitCenter"
        android:text="15"
        android:textColor="#fff"
        android:textSize="24sp"
        android:textStyle="bold" />

    <Button
        android:id="@+id/Button16"
        android:layout_width="60dp"
        android:layout_height="60dp"
        android:layout_margin="10dp"
        android:alpha="0.5"
        android:background="#424242"
        android:clickable="false"
        android:enabled="false"
        android:scaleType="fitCenter"
        android:text="16"
        android:textColor="#fff"
        android:textSize="24sp"
        android:textStyle="bold" />
</LinearLayout>




    <Button
        android:id="@+id/ButtonErase"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_margin="10dp"
        android:alpha="0.5"
        android:background="#ff0000"
        android:scaleType="fitCenter"
        android:text="@string/erase"
        android:textColor="#fff"
        android:textSize="18sp"
        android:textStyle="bold" />

</LinearLayout>

I could change my LinearLayout to a Relative one, if you think it's a solution, but I find the LinearLayout easier to play with.

My question is, of course: What should I change to display my Ad at the top of the screen instead of at the bottom of the screen ? What didn't I get here ? All documentation you have is welcomed, I just can't find enough resources/SO posts to answer my question as it's a "new version" of AdMob (as I said earlier).

Upvotes: 0

Views: 230

Answers (1)

Yoann Hercouet
Yoann Hercouet

Reputation: 17996

If you are ready to switch to RelativeLayout (that should not be long), here is the beginning of what should be your new XML:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
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/adview"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_alignParentTop="true"
    ads:adSize="SMART_BANNER"
    ads:adUnitId="XXXXX" />

<ImageView
    android:id="@+id/search_background"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_below="@+id/adview" />

The most important is to add android:layout_alignParentTop="true" for the AdView element. Then just be sure that all the other elements are below this one (using layout_below).

Upvotes: 1

Related Questions