How to place your ads at the bottom of your screen in Android

So you’ve setup some ads using AdMob or AdWhirl for your android application, and you’re having a hard time getting the ad to appear at the bottom of your screen? Well fear not, for we have a solution.

What you need to do, is to have a RelativeLayout, and within that, have one LinearLayout for your own views, and another LinearLayout which just contains the ads, and anchor that to the bottom of the screen.

Have a look at my example here :

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
                xmlns:androidsam="http://schemas.android.com/apk/res/com.jameselsey"
                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 -->

    </LinearLayout>
    <LinearLayout android:layout_width="fill_parent"
                  android:id="@+id/ad_layout"
                  android:layout_height="wrap_content"
                  android:gravity="bottom"
                  android:layout_alignParentBottom="true"
                  android:layout_alignBottom="@+id/home_layout">
        <com.admob.android.ads.AdView
                android:id="@+id/ad"
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"
                android:gravity="bottom"
                androidsam:backgroundColor="#000000"
                androidsam:primaryTextColor="#FFFFFF"
                androidsam:secondaryTextColor="#CCCCCC"/>
    </LinearLayout>
</RelativeLayout>

Hope this helps!

9 thoughts on “How to place your ads at the bottom of your screen in Android”

  1. Hi James, was wondering if you have any experience of A sync tasks? The app I am developing freezes because there is too much going on sometimes. Have looked online at many examples, but all seem very complex. Your code is usually easier to follow!

    1. Hi Philip, thanks for the comment.

      Your app freezes because too much is going on? One of the golden rules I’ve been told from Google is to move any resource hungry/time consuming actions OFF of the main UI thread. For example, if your application goes off to download some data from the net (i.e., a REST call), don’t do that on the main thread otherwise your activity will hang around twiddling its thumbs until that REST call is complete. ASync tasks, or services are ideal for sorting this out. You can then use the runOnUIThread() method to get data back onto the screen.

      I have some sample code on my box at home that you press a button on the screen, it fires off a service which does something for 10 seconds on a background thread, and updates a progress bar on the screen, that might help you. I actually have it on my “to do” list this weekend to cover some Async / service related stuff, so if you can let me know your exact requirements I’ll see if I can’t tweak a tutorial for you :)

      Have fun!

  2. Sounds cool!, Basically it one of my activities I send some data over the net, and I get some data back, while this happens the UI seems to hang!

  3. hey –
    tried using your code and i get the ad at the bottom of the screen, but it overlaps my main content instead of resizing it. any way to get around this and get the main content to squeeze up instead of being overlapped? (such as what happens if you place the ad at the top of the screen in a linearlayout)
    thanks!

    1. Hi Emma,

      Whats your layout look like? Odd that its overlapping, it should resize the display so that everything fits in OK. Might be worth checking the FILL_PARENT options in the nested layouts.

      Thanks

  4. Hi! great tutorial James!
    this has partially answer my question….
    i actually want to put my admob at the bottom(just like you have shown above) but, what i want is, when the ads are not available the content above it will fill the rest of the layout. but if there’s any ads, then it will push all the contents up…
    in my case the content is list, so when the ads pops, it will resize the listview to give some space for the ads….

    do you have any idea how to accomplish this? Thanks!

  5. Hi! i got it works!
    just the same problem like Emma had, i add android:layout_above=”@+id/ad_layout to home_layout
    and FOILA! it works! thanks for the tutorial…

    i also saw your post at http://android.jmsliu.com/209/add-google-admob-in-android-application.html

    it’s nice! thank you! btw do you have any alternative for another ads provider james? coz i think admob is little bit screw with me…. i mean it’s often replies “no ads are available this time” (i’m wondering if i could be rich with that….) :(

Leave a Reply to Philip Cancel reply

Your email address will not be published. Required fields are marked *