In this post we will talking about how to integrate AdMob in Android application with banner using Eclipse. with our example we will add admob banner on the top of our application by using ads test unite id, we will complete run this example so now let's start with eclipse IDE and following step by step :
Before we create project in eclipse we need to sign in your Admob account and create unite ads as banner. (ca-app-pub-6593689845821813/9312087245)
Get Google-Play-Service library
make sure your Android SDK(System Development Kite) has been installed google play service in android manage. Open your eclipse tool and go to
1- Windows > Android SDK Manage
2- After completed install the project will download in to C:\Users\<User Name>\android-sdks\extras\google\google_play_services\libproject for Windows OS.
3- Import File>Import browse to google play-service in extra folder to your project work space follow with this :
Click Next it will popup like :
it will show google-play-service project folder in your current project work space. and next step we need to add google-play-service to our library follow this step:
4- Right click on your Project go to Property of your project
By Click Add button it will show
Then click OK>Apply>OK
Now we go to our project in activity main.xml I create ads and assign id call @+id/adsview1
<com.google.android.gms.ads.AdView
android:id="@+id/adsview1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_alignParentBottom="true"
ads:adSize="SMART_BANNER"
ads:adUnitId="@string/banner_ad_unit_id">
</com.google.android.gms.ads.AdView>
AdMob provides test ad unit id for application development mode. After development we need to use actual ad unit id for your application.
Now we add banner UnitId in
strings.xml
<string name="banner_ad_unit_id">ca-app-pub-3940256099942544/6300978111</string>
Android Manifest.xml add permissions meta data tag and Activity
Add Permissions
<uses-permission android:name="android.permission.INTERNET"/>
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>
Add meta data
<meta-data android:name="com.google.android.gms.version"
android:value="@integer/google_play_services_version" />
Add Activity Class
<activity android:name="com.google.android.gms.ads.AdActivity"
android:configChanges="keyboard|keyboardHidden|orientation|screenLayout|uiMode|screenSize|smallestScreenSize"
android:theme="@android:style/Theme.Translucent" />
Full Android Manufest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.grouprttcadmob"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="21" />
<uses-permission android:name="android.permission.INTERNET"/>
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name=".MainActivity"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<meta-data android:name="com.google.android.gms.version"
android:value="@integer/google_play_services_version" />
<activity android:name="com.google.android.gms.ads.AdActivity"
android:configChanges="keyboard|keyboardHidden|orientation|screenLayout|uiMode|screenSize|smallestScreenSize"
android:theme="@android:style/Theme.Translucent" />
</application>
</manifest>
Now we back to our activity_main.xml in this code it very important to add the code like below in our layout
xmlns:ads="http://schemas.android.com/apk/res-auto
So you can put ads on you activity
<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="@string/ads_banner_unitId" />
In MainActivity class
public class MainActivity extends Activity {
Utility utility;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
setBodyText();
utility = new Utility(this);
utility.setLayoutForSmartBanner();
//Display smart banner ads
AdView mAdView = (AdView) findViewById(R.id.adView);
AdRequest adRequest = new AdRequest.Builder().build();
mAdView.loadAd(adRequest);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.menu_main, menu);
return true;
}
}