How to Show Admob Ads in Android Apps
For monetizing android apps, you can show mobile adds in your application. Though, there are a number of ways you can add advertisement in your apps, this article will focus on adding ads in the XML layouts. To show ads in XML layout, you need to follow the following steps:
- Sign up for Admob or any other mobile ad program. If you sign up for admob they will give you the code for each ad you create. You just have to add this ad code in your xml file.
- Download Google Play Mobile SDK and add it to your project.
If you already have an admob account, create an ad and copy the ad code, which you will have to add in your XML file. Remember that there are serveral ad programs such as "DoubleClick For Publishers", Ad Exchange, search ads for mobile. So, if you want to show ads in your apps, admob is not the only solution. But at the beginning, you can start with admob because they have one of the largest ad inventories.
Before installing the SDK make sure you meet the following requirements.
In your AndroidManifest.xml file, you need to set the android:minSdkVersion level to a minimum of 9.
android:minSdkVersion to at least 9 in your AndroidManifest.xml
In the project properties file set the target level to android-13.If it is already set to 13, you do not have to do modify anything. The project.propertifes file is located in the ‘res’ folder of your android project.
How to install Google Mobile Ads SDK?
- To install Google Mobile ads SDK, you need to download Google Play services in your Eclipse. If you already have it in your eclipse you just have to add it in your android project. If you do not have Google paly service, follow the steps given below:
- In your eclipse, click on “window” and then click on “Android SDK Manager”
- Next click on ‘Extra’ and select “Google Play Services”. Click on the “install package” to start installing the Google Play Services.
Note: Remember that Google play services contains the Goolge_play_service_lib that you will need to configure in your project, which you will come to know shortly.
Once installation completes, go to the following folders:
- Go to adt-bundle-windows-x86-20140321\sdk\extras\google and copy the “google play services” folder that you have downloaded earlier using the SDK manager.
- Now, in your eclipse, paste the “google play services” folder in the ‘libs’ folder of your android project.
- Now, click on ‘File” and then ‘import’. Select ‘Android’ and then select
“existing android code into workspace”. Click ‘Next’ and browse to find the location of the libs folder in your project workspace where pasted the “google play services”. Just select the ‘libs’ folder, not need to select other files inside it. Click ‘OK’ .Next, select the folder that has Google_play_services_lib and deselect all other folders and finish the installation. Now, your project has the necessary mobile SDK that helps to show ads in your apps.
- The next step after completing the installation is to add the Google play service to your project. Right click on your project, select ‘Android’ and click ‘ADD’ under the library and then select “google_play_services_lib” and click ‘OK’ and ‘Apply.’
You need to add a few lines of code in the Maniest.xml file:
- Add meta data
- Add Ad-activity
- Add permission
<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"/>
Add Network permission: add the following codes just before tag in the AndroidManifest.XML file.
<uses-permission android:name="android.permission.INTERNET"/> <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>
You can have a look at Google’s android developer’s site for more details about the above mentioned codes: https://developers.google.com/mobile-ads-sdk/docs/
Codes for JAVA class file.
The following JAVA codes need to be added in the MainActivity.Java or any other java file that will load the xml file. Normally, you need to add this code right after the “ setContentView(R.layout.main);
AdView adView = (AdView)this.findViewById(R.id.adView); AdRequest adRequest = new AdRequest.Builder().build(); adView.loadAd(adRequest);
Codes for XML file: suppose you want to show the banner ads in your apps. You have to add the following code in the xml layout file that will be loaded when
<com.google.android.gms.ads.AdView android:id="@+id/adView" android:layout_width="wrap_content" android:layout_height="wrap_content" ads:adUnitId="MY_AD_UNIT_ID" ads:adSize="BANNER"/>