Android Code - How to setup Admob Account? How to get your Application Monetized? How to setup Banner Advertisements ? How to setup Interstitial Advertisements?


Admob account is used to earn money through advertisements . These advertisements are shown on the application page . There are two types of ads
1 - Banner Ads
2 - Interstitial Ads


1 - Banner Ads
a - Creat an Account and Log in to AdMob
(Before you proceed to the next step , you need to have your application published in Playstore and it takes 24 to 48hrs before you can search your application here on AdMob)
b - Click on Monetize new app
c - Search you application
d - Choose if Banner Ad.
e - Done

If you are lazy to go through the Video , here's the text :

1 - Change the build.gradle code :

dependencies {
    compile fileTree(dir: 'libs', include: ['*.jar'])
    compile 'com.android.support:appcompat-v7:19.+'
    compile 'com.google.android.gms:play-services:4.+'

2 - Change the AndroidManifest.xml :
a - Add permissions :
<uses-permission android:name="android.permission.INTERNET" />
    <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>

b - add AdMob details:

<!--  admob activity-->
        <activity android:name="com.google.android.gms.ads.AdActivity"
            android:configChanges="keyboard|keyboardHidden|orientation|screenLayout|uiMode|screenSize|smallestScreenSize"
            />
        <!-- admob meta data -->

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

3 - update the Class / Activity
//admob
        AdView adView=(AdView)this.findViewById(R.id.adView);
        AdRequest adRequest=new AdRequest.Builder().addTestDevice("hash code device ID").build();
        adView.loadAd(adRequest);

4 - udpate the <layout.xml>
<!--Admob layout -->
    <com.google.android.gms.ads.AdView
        android:id="@+id/adView"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        ads:adUnitId="ca-app-pub-XXXX"
        ads:adSize="SMART_BANNER"
        android:layout_alignParentTop="true"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="true"
        />

**Done


Watch this Video to add Banner Ad to your application . It worked for me.



2 - Interstitial Ads
a - Creat an Account and Log in to AdMob
(Before you proceed to the next step , you need to have your application published in Playstore and it takes 24 to 48hrs before you can search your application here on AdMob)
b - Click on Monetize new app
c - Search you application
d - Choose Interstitial Ad
e - Done

If you are lazy to go through the Video , here's the text :

1 - Change the build.gradle code :

dependencies {
    compile fileTree(dir: 'libs', include: ['*.jar'])
    compile 'com.android.support:appcompat-v7:19.+'
    compile 'com.google.android.gms:play-services:4.+'

2 - Change the AndroidManifest.xml :
a - Add permissions :
<uses-permission android:name="android.permission.INTERNET" />
    <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>

b - add AdMob details:

<!--  admob activity-->
        <activity android:name="com.google.android.gms.ads.AdActivity"
            android:configChanges="keyboard|keyboardHidden|orientation|screenLayout|uiMode|screenSize|smallestScreenSize"
            />
        <!-- admob meta data -->

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

3 - update the Class / Activity

a - imports :
import com.google.android.gms.ads.AdListener;
import com.google.android.gms.ads.AdRequest;
import com.google.android.gms.ads.AdView;
import com.google.android.gms.ads.InterstitialAd;

b - class declarations :
//interistitialAd
    private InterstitialAd interstitialAd;
    boolean exitApp = false;

c - Call the function :
c-1 - In onCreate method
protected void onCreate(Bundle savedInstanceState) {


        // TODO Auto-generated method stub
        super.onCreate(savedInstanceState);

//interstitial add
        launchInter();
        loadInterstitial();

****rest code as it is

c-2 - In onBackPressed method

public void onBackPressed ()
    {
        //interstitial add
        launchInter();
        loadInterstitial();

        //used to go to the main activity with menu slider
        Intent intent = new Intent(getApplicationContext(), explistmain.class);
        //activity is started
        startActivity(intent);
        //when paused while going to main activity - the intro activity is closed
        finish();
    }

4 - paste the complete code in the main class :

//interstitialAd

    private void launchInter()
    {
        interstitialAd = new InterstitialAd(this);
        interstitialAd.setAdUnitId("ca-app-pub-XXXXX");

        //set the adListener
        interstitialAd.setAdListener(new AdListener() {
            @Override
            public void onAdLoaded() {
                showAdInter();
            }

            @Override
            public void onAdFailedToLoad(int errorCode) {

                String message = String.format("onAdFailedToLoad (%s)", getErrorReason(errorCode));

            }

            @Override
            public void onAdClosed() {
                if (exitApp)
                    finish();
            }
        });
    }
    private void showAdInter()
    {
        if (interstitialAd.isLoaded())
        {
            interstitialAd.show();
        }
        else
        {
            Log.d("", "Interstitial ad was not ready to be shown");
        }
    }
    public void loadInterstitial()
    {
        AdRequest adRequest = new AdRequest.Builder()
                .addTestDevice(AdRequest.DEVICE_ID_EMULATOR)
                .addTestDevice("hash code device ID")
                .build();

        //load the interstitial ad
        interstitialAd.loadAd(adRequest);;
    }


    //get a string error reason from and error code

    private String getErrorReason(int errorcode)
    {
        String errorReason="";
        switch (errorcode)
        {
            case AdRequest.ERROR_CODE_INTERNAL_ERROR:
                errorReason="Internal Error";
                break;

            case AdRequest.ERROR_CODE_INVALID_REQUEST:
                errorReason="Invalid Request";
                break;

            case AdRequest.ERROR_CODE_NETWORK_ERROR:
                errorReason="Network Error";
                break;

            case AdRequest.ERROR_CODE_NO_FILL:
                errorReason="No Fill";
                break;
        }
        return errorReason;
    }

**Done

Watch this Video to add Interstitial Ads to your application . It worked for me.



Comments

Popular posts from this blog