Monday 29 September 2014

How to add Ads in android.

 There are so many ads for mobile application especially in android. Some of them are

1. Admob
2. Airpush
3.Startapp
4.LeadBolt
5. Mopub and so on.

I used all.The best one is top 3 ad networks. Previously Admob provided SDK but now it has been integrated with Google play services so that you don't need to use separate sdk for Admob.

How to use Admob?

Go to apps.admob.com. Create an account and give your app name and it provides you an ad unit.

Use which ad type you are going to display.  Since other networks are providing a clean documentation when you logged in. But Admob you need to surf and for new update, you can't find a clear updates and user miss some important keys in admob which I noticed. Here I am discussing about this.

Admob provides two types.

1. Banner adview

2. Interestitial ads.

If you choose interestitial ads, you can use like this.

import com.google.android.gms.ads.InterstitialAd;

public class MainActivity extends Activity {
private InterstitialAd interstitial; // Declare interstitial ads here.


@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
           interstitial = new InterstitialAd(MainActivity.this);
        // Insert the Ad Unit ID
        interstitial.setAdUnitId("<Ad unit id>");

          AdRequest adRequests = new AdRequest.Builder().build();
   interstitial.loadAd(adRequests);
   interstitial.setAdListener(new AdListener() {
       @Override
       public void onAdLoaded() {
           super.onAdLoaded();
           interstitial.show();

       }

       @Override
       public void onAdFailedToLoad(int errorCode) {
           super.onAdFailedToLoad(errorCode);

       }

   });
 
       }
     

  For Banner ads:

       AdView adView = (AdView) this.findViewById(R.id.adView);
    
        AdRequest adRequest = new AdRequest.Builder()

        // Add a test device to show Test Ads..comment for real app when launching
        // .addTestDevice(AdRequest.DEVICE_ID_EMULATOR)
         //.addTestDevice("CC5F2C72DF2B356BBF0DA198")
                .build();

        // Load ads into Banner Ads
        adView.loadAd(adRequest);

    in xml:

       <com.google.android.gms.ads.AdView
         android:id="@+id/adView"
                         android:layout_width="wrap_content"
                         android:layout_height="wrap_content"
                         ads:adUnitId="<ads id>"
                         ads:adSize="BANNER"/>

 important to note that, you need to declare for xml as:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:ads="http://schemas.android.com/apk/res-auto" // these line is most needed in top of the xml when you are using adview inside layout.
................
<Adview> here
>
</LinearLayout>

 and in manifest file, these lines are to be added

 <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" />
 

You can run and test it and it works.



No comments:

Post a Comment