Search Tutorials

Loading...

Sunday, 29 December 2013

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);
 }
});

Related Tutorials:-

Display Time using Broadcast Receiver in Android

Check Battery level using Broadcast Receiver in Android

Display all Mobile Contacts in Android

Perform Action on any Hardware Button in Android

How to Add Facebook SDK in Android Application

39 comments:

  1. Can you please give details on how to add Admob interstitial ads to an android BuzzTouch v3 project? I mean how and where exactly do I put those codes in my BT project in eclipse?

    ReplyDelete
    Replies
    1. I didn't make application on buzzTouch but i made two android applications which are mentioned above. I am using interstitial ads on first page on back button. when user open application ad will start to load and after opening application, user can visit other pages of the application. At the end user go back and want to close application using back button at that time if interstitial ad has loaded successfully than ad will open in full page and use flag to show it once time & if user click back button again than application will close.
      Note: No need to use XML code for interstitial ads, just define it in Java and show it when you want.

      Delete
    2. So how and where do I define it?

      Delete
    3. Use the below 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());

      And show Ads when you want like on any button click or without any button click..using below code:

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

      And don't forgot to change AndroidManifast.xml file and to replace YOUR_AD_UNIT_ID with your id or use my id for example: a152c80c278af19

      Delete
    4. Thanks for your quick responses, Only the banner ads work fine for me now. You see I am new in android app development.

      Can you please show me an example java code with the above Interstitial code in it? I don't know how to place the above interstitial code in java.

      Delete
    5. Create New Android Project and do nothing in XML files & open Your Java File and paste below code in oncreate(Bundle obj) method:

      InterstitialAd end_ad=new InterstitialAd(this); end_ad.setAdUnitId("a152c80c278af19");
      end_ad.loadAd(new AdRequest.Builder().build());
      if(end_ad.isLoaded())
      {
      end_ad.show();
      }

      Now open your AndroidManifest.xml file and change according to above given AndroidManifast.xml: give Internet access & permission, use meta tag and define Google activity.

      Now save and run & install apk file in your mobile. Open Internet connection and open application and wait for a moment, you will see Ad.

      Thanks for comment bro and if you like my work please share it.

      Delete
    6. I am showing some part of my android project to help you(See it, it will help you):

      public class MainActivity extends Activity {
      TextView tv=null;
      SQLiteDatabase db;
      AutoCompleteTextView et=null;
      String[] arr;
      private InterstitialAd end_ad=null;
      boolean flag=false;
      @Override
      protected void onCreate(Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);
      setContentView(R.layout.activity_main);
      tv=(TextView)findViewById(R.id.textView1);
      et=(AutoCompleteTextView)findViewById(R.id.autoCompleteTextView1);
      db=openOrCreateDatabase("freelance", MODE_PRIVATE, null);
      db.execSQL("create table if not exists mytable(name1 varchar,name2 varchar,name3 varchar)");
      end_ad=new InterstitialAd(this);
      end_ad.setAdUnitId(getResources().getString(R.string.ad_unit_id));
      end_ad.loadAd(new AdRequest.Builder().build());
      end_ad.show();
      if(end_ad.isLoaded())
      {
      tv.setText("Ad loaded");
      }

      }

      @Override
      public void onBackPressed()
      {
      if(flag==false && end_ad.isLoaded())
      {
      end_ad.show();
      flag=true;
      }
      else
      {
      finish();
      }
      }
      }

      Delete
    7. when I add the above code to any of my app screen, it crashes that particular screen.

      Delete
    8. Give me your email id, i will send you complete project in rar file. Extract File and import in SDK than Add Google play services package as a library(as mentioned in post) and run. Install Apk file in mobile and wait for a moment to display Ads.

      Delete
    9. The problem is that BuzzTouch uses Fragment and not Activity as explained in the above code, can you please add Admob Interstitial ads using BuzzTouch sample code?

      Delete
    10. I didn't try BuzzTouch for making Application, So can't give any example...sorry.

      Delete
    11. I have Solved it Here:

      https://www.buzztouch.com/forum/thread.php?tid=64DE8F7C56FC178B149FAFB

      Delete
    12. Congrats...good work. :)

      Delete
  2. Is the process same for adding adfonic id instead admob

    ReplyDelete
    Replies
    1. I never tried adfonic ads but i know this works with google adsense id and admob id. You have to read instructions and visit developer site of adfonic.

      Delete
  3. Nice information. Thank you sir..

    ReplyDelete
  4. i have created app on appsyet.com website
    and i also added publisher id and its shows adds but dont know that how to see that how many i earn?
    help me

    mail me if you know trick
    divyeshvaghani96@gmail.com

    ReplyDelete
    Replies
    1. Use Google Admob..it is best and don't believe any others sites because they might be pay or not.

      Delete
  5. Hey guys can u help me on how to add banner in android eclipse its not working on me :(

    ReplyDelete
    Replies
    1. Already given on this post..just chat me on hangout to resolve problem.

      Delete
  6. Hi
    When I goto admob.com it redirects to https://www.google.com/ads/admob/. I can't find Sites and Apps option. Please help.

    ReplyDelete
    Replies
    1. Oh AdMob change website and look..use it you will get easily to add code.

      Delete
  7. hi i made a quiz application and integrated intersitial ads in it but app is crashing without internet if we open with internet when u click before ad loaded app is crashing but if we wait for ad after displaying ad app is working fine plz help me to resolve it

    ReplyDelete
    Replies
    1. send logcat error on mail.

      Delete
  8. This comment has been removed by a blog administrator.

    ReplyDelete
  9. I am having heap space error after adding google play library could you please help me?

    ReplyDelete
    Replies
    1. hi ..
      open your eclipse folder > open eclipse.txt file >
      after this put the values in your file
      -Dhelp.lucene.tokenizer=standard
      -Xms512m
      -Xmx1024m

      after save and restart your eclipse.
      enjoy

      Delete
  10. This comment has been removed by a blog administrator.

    ReplyDelete
  11. hey this is very good tutorial , its work for me. thanks.

    ReplyDelete
  12. I have an apk file downloded from a web site.
    Can i get money on uploading to admob.

    ReplyDelete
  13. how can i get the source and what changes should be made to upload to admob

    ReplyDelete
  14. Nice work sir .i will try this code .if any query i will ask you .reply for my query .thank you sir

    ReplyDelete
  15. A/a.it is showing me:"The method getString(int) in the type Resources is not applicable for the arguments (String)",plz reply where is mistake

    ReplyDelete
  16. at this:end_ad.setAdUnitId(getResources().getString(""));

    ReplyDelete
    Replies
    1. please add your Ad Id in setAdUnitId("your_ad_id") method.

      Delete
  17. HI
    I am not able to display banner ads can you please help me by sending me the instuction or the souce file emailid- devanshchaudhary2002@gmail.com

    ReplyDelete
  18. HI
    I am not able to display banner ads can you please help me by sending me the instuction or the souce file. Can we add on android applications developed by appypie.com. emailid- qazilko@gmail.com

    ReplyDelete
    Replies
    1. Hi, yes you can add it in application developed by appypie.com and for instruction you can go through above tutorial.

      Delete

Back to Top