Free Download Software and Android Tutorials

Tuesday, August 11, 2015

Android Studio Tutorial create banner ads with AdListener

Android studio tutorial had previously been discussed on how to insert a banner ad ID Admob into applications using android studio. On this occasion I will discuss the continuation of the previous article is made by adding a banner ad AdListener command in program code. The purpose of using the command AdListener is so that we can know the status of ad in applications that we make. If previously we insert ads ID on the layout, on this occasion ID banner ads will be inserted in the program code. Without more details at length so as usual we just practice together.

Make sure you've made the android studio, if it is immediately create a new project and name IklanBannerAdListener, and do not forget to select the format of banner ads. IklanBannerAdListener Project consists of one file MainActivity.java and activity_main.xml that serves as the layout. For more details you practice just command script below.

Source code MainActivity.java
package com.dsoccerupdate.iklanbanneradlistener;

import com.google.android.gms.ads.AdListener;
import com.google.android.gms.ads.AdRequest;
import com.google.android.gms.ads.AdSize;
import com.google.android.gms.ads.AdView;

import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
import android.widget.LinearLayout;
import android.widget.Toast;

public class MainActivity extends Activity {
    private AdView adView;

    @Override
    public void onCreate(Bundle SavedInstanceState) {
        super.onCreate(SavedInstanceState);
        setContentView(R.layout.activity_main);

        adView = new AdView(this);
        adView.setAdSize(AdSize.MEDIUM_RECTANGLE);
        adView.setAdUnitId("ca-app-pub-8036741548724898/5354201765");

        adView.setAdListener(new AdListener() {
            @Override
            public void onAdClosed() {
                Toast.makeText(MainActivity.this, "onAdClosed", Toast.LENGTH_SHORT).show();
            }

            @Override
            public void onAdFailedToLoad(int error) {
                String message = "onAdFailedToLoad: " + getErrorReason(error);
                Toast.makeText(MainActivity.this, "onAdFailedToLoad", Toast.LENGTH_SHORT).show();
            }

            @Override
            public void onAdLeftApplication() {
                Toast.makeText(MainActivity.this, "onAdLeftApplication", Toast.LENGTH_SHORT).show();
            }

            @Override
            public void onAdOpened() {
                Toast.makeText(MainActivity.this, "onAdOpened", Toast.LENGTH_SHORT).show();
            }

            @Override
            public void onAdLoaded() {
                Toast.makeText(MainActivity.this, "onAdLoaded", Toast.LENGTH_SHORT).show();
            }
        });

        LinearLayout layout = (LinearLayout) findViewById(R.id.linearLayout);
        layout.addView(adView);

        AdRequest adRequest = new AdRequest.Builder()
                .addTestDevice(AdRequest.DEVICE_ID_EMULATOR)
                .addTestDevice("11F163F3EBE181DAB585531BBEF63689")
                .build();

        adView.loadAd(adRequest);
    }

    @Override
    public void onResume(){
        super.onResume();
        if (adView != null) {
            adView.resume();
        }
    }

    @Override
    public void onPause() {
        if (adView != null) {
            adView.pause();
        }
        super.onPause();
    }

    @Override
    public void onDestroy() {
        if (adView != null) {
            adView.destroy();
        }
        super.onDestroy();
    }

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

}

Source code activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/linearLayout"
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

</LinearLayout>

Before the program is run do not forget the values> strings.xml change it banner_ad_unit_id with ID banner ads that you have created on Google Admob. If you cannot make the ID advertising through Google Admob, it helps you read my previous article that was already discussed it clearly here. The results of the project IklanBannerAdListener if the run is as follows.

android, studio, tutorial, create, banner, ads, AdListener


Android Studio Tutorial create banner ads with AdListener Rating: 4.5 Diposkan Oleh: Unknown

0 comments:

Post a Comment