Tuesday, August 4, 2015

Add Google AdMob Ads in Android Application & Earn Money

Want to Earn Money from Android Application? Yes, than you are on right place to get accurate method to earn money via android application. AdMob is the best mobile advertising site/company to fill your pocket in dollars. Initially, i don't have any idea to earn money via android apps but now i know how can i earn money from my android applications that's why I am sharing this method and hope everyone like it.
In this Tutorial, we will learn how to show Google AdMob Ads on Android Application step by step. We will need the following two things:-

1. Ad Unit Id from Admob site.
2. Google Play Services installed in Android SDK.

How to get Ad Unit Id from Admob site:-

1. Go to Admob site.

2. Create New Account (if you are already an admob user, then log in). 

3. Select "Monetize new app" option.
Admob monetize new app

4. Search your app on Google play or fill details manually of your app to connect.

Form to add app on Admob site

5. After filling all information, you will get instruction to set up admob unit id.

7. Now you have added your app to admob site. Go to Monetize tab and check your added app in "all app" list and than select your added app and create new ad-unit. Create ad-unit for banner or Interstitial Ads as you want in your application and get Ad Unit id which will look like this: ca-app-pub-xxxx71439079xxxx/270233xxxx.

8. Now use Ad Unit Id in your project.

How to get Google Play Services Folder:-

1. Open android SDK -> Click on Windows -> android SDK manager -> extras -> download Google play services.

Add Google AdMob Ads in Android Application
Install Google Play Service Packages

After installing Google Play services, import it in your workspace as an android project. Now we have Admob Ad Unit Id and Google play services both. Create a new project to test Ads on android application and use Google play services as a library in your android project.

How to Add Google AdSense code in Android Application:-

1. Now select project -> right click on project -> properties -> android -> add library -> select Google play services folder -> Apply.

How to add Google AdMob Ads in Android Application
Add Google Play Services Package As a Library

2. Open your android XML file and add the following line in layout:

xmlns:ads=http://schemas.android.com/apk/res-auto

3. Add AdView in Layout:

<com.google.android.gms.ads.AdView
 android:id="@+id/adView"
 android:layout_width="match_parent"
 android:layout_height="wrap_content"
 ads:adSize="BANNER"
 ads:adUnitId="YOUR_AD_UNIT_ID"
/> 
4. The Android XML file will look like this:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
 xmlns:android="http://schemas.android.com/apk/res/android"
 xmlns:ads="http://schemas.android.com/apk/res-auto"
 android:id="@+id/mainLayout"
 android:layout_width="match_parent"
 android:layout_height="match_parent"
>
<com.google.android.gms.ads.AdView
 android:id="@+id/adView"
 android:layout_width="match_parent"
 android:layout_height="wrap_content"
 android:layout_alignParentBottom="true"
 android:layout_alignParentLeft="true"
 ads:adSize="BANNER"
 ads:adUnitId="Your_AD_UNIT_ID"
/>
</RelativeLayout>

5. Now open your Java file and initialize AdView Object in your Activity.

AdView ad=(AdView)findViewById(R.id.adView);

6. Load Ad from Google:

ad.loadAd(new AdRequest.Builder().build());

Now change AndroidManifest.xml file according to the code given in the end of the tutorial & run your project. Now install the application on your phone & check ads!

For Interstitial Ads:-

This Ad will be displayed on your complete phone screen.

1. Use the following code in your activity:

 InterstitialAd end_ad=new InterstitialAd(this);
    end_ad.setAdUnitId(getResources().getString(Your_AD_UNIT_ID));
    end_ad.loadAd(new AdRequest.Builder().build());

2. Show Ad if loaded:

if(end_ad.isLoaded())
          {
             end_ad.show();
          }

Now change AndroidManifest.xml file according to the code given in the end of the tutorial & run your project. Now install the application on your phone & check ads!

Now change AndroidManifest.xml file:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.Main_Activity"
android:versionCode="1"
android:versionName="1.0" >

<uses-sdk
android:minSdkVersion="9"
android:targetSdkVersion="18" />

<!-- Used to request banner and interstitial ads. -->
<uses-permission android:name="android.permission.INTERNET" />

<!-- Used to avoid sending an ad request if there is no connectivity. -->
<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">

<meta-data
 android:name="com.google.android.gms.version"
 android:value="@integer/google_play_services_version" />

<activity
 android:name="com.example.Main_Activity"
 android:label="@string/app_name" >
<intent-filter>
 <action android:name="android.intent.action.MAIN" />
 <category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>

<!-- Activity required to show ad overlays. -->
<activity
 android:name="com.google.android.gms.ads.AdActivity"
 android:configChanges="keyboard|keyboardHidden|orientation|screenLayout|uiMode|screenSize|smallestScreenSize"/>
</application>
</manifest>

Both above examples are used in the following Android applications: Helper+ & Namaz Guide

Mostly apps use Admob Ads in Listview and show Ads when Loaded. Initially, I was confused that how can I add Ads after successfully loading and finally I got success and felt it was very easy. I added AdView widget at bottom of the parent layout & set visibility gone and use Listview widget in whole layout. After setting up XML file, I used below code in Activity:-

ad=(AdView)findViewById(R.id.adView);
ad.setVisibility(View.GONE);
ad.loadAd(new AdRequest.Builder().build());

ad.setAdListener(new AdListener() {
 @Override
 public void onAdLoaded() {
  ad.setVisibility(View.VISIBLE);
 }
});

No comments:

Post a Comment