Drive mobile ad revenue by integrating banner into your app and get paid for your hard work. You will need a reference to Google Play Service SDK in your project. I’ve applied this tutorial to my app FlipNote. Check it out if you want to see how it looks on a real device.
Create Layout
The important part here is android:layout_alignParentBottom=”true” for the AdView. Replace “YOUR_ADMOB_ID” with your actual id.
File: activity_main.xml
<RelativeLayout xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" xmlns:android="http://schemas.android.com/apk/res/android"> <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_alignParentBottom="true" ads:adSize="SMART_BANNER" ads:adUnitId="YOUR_ADMOB_ID"/> <ListView android:id="@+id/list" android:layout_width="match_parent" android:layout_height="match_parent" android:divider="@null" android:dividerHeight="0dp"> </ListView> </RelativeLayout>
Open your MainActivity.java and add this code.
File: MainActivity.java
package com.ahotbrew.admob; import com.google.android.gms.ads.AdRequest; import com.google.android.gms.ads.AdView; import android.app.Activity; import android.os.Bundle; public class MainActivity extends Activity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); AdView adView = (AdView)this.findViewById(R.id.adView); AdRequest adRequest = new AdRequest.Builder().build(); adView.loadAd(adRequest); } }
Sit back, take a sip from your hot brew and run your project.
Don’t forget to join or newsletter and get free android tutorials to enjoy with your hot brew.