Free Download Software and Android Tutorials

Monday, August 10, 2015

Android Studio tutorial how to insert banner ads Admob ID on Application

Android studio tutorial will discuss how to insert an advertisement ID of Admob into applications that we make using android studio. Before I proceed further into our discussion this time, it helps you read my previous article about making your ads on Google Admob, the purpose of which is after the ad was made later we will get a good advertisement for the types of ID-shaped banner ads or instertitial which then ID The ads will later be inserted into the source code of applications that we created through the android studio.

Without a longer length we just practice to make your banner ad for a new project through the android studio with name IklanBanner, and then on Create New Project window you select Google Admob Ads Activity. Click the next button, and then fill in your name Activity MainActivity, and the Banner Ad format you select, if it is you click finish. For more details you can see in the image below.

android, studio, tutorial, banner, ads, Admob, ID, Application
Create New Project
android, studio, tutorial, banner, ads, Admob, ID, Application
Create Ad Format with Banner
In this IklanBanner project consists of MainActivity.java and activity_main.xml. The following source code of both files and the xml java.

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

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

import android.os.Bundle;
import android.app.Activity;

public class MainActivity extends Activity {
    private AdView adView;

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

        adView = (AdView) this.findViewById(R.id.adView);
        AdRequest adRequest = new AdRequest.Builder()
                .addTestDevice(AdRequest.DEVICE_ID_EMULATOR)
                .addTestDevice("11F163F3EBE181DAB585531BBEF63689")
                .build();
        adView.loadAd(adRequest);
    }

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

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

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

Explanation of source code
import com.google.android.gms.ads.AdRequest;
import com.google.android.gms.ads.AdSize;
import com.google.android.gms.ads.AdView;

The above command is used to ask the android studio in order to insert ads on the Google Admob is by importing. When we want to test this IklanBanner application through an emulator, then we need the emulator ID and define it through MainActivity.java through command
AdRequest adRequest = new AdRequest.Builder()
                .addTestDevice(AdRequest.DEVICE_ID_EMULATOR)
                .addTestDevice("11F163F3EBE181DAB585531BBEF63689")
                .build();
        adView.loadAd(adRequest);


To get the ID emulator can be known by running applications through android studio, and then you open the ADB logs. Incidentally, I use the emulator genymotion with ID emulator ("11F163F3EBE181DAB585531BBEF63689").

Source code activity_main.xml
<?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"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical">

    <com.google.android.gms.ads.AdView
        android:id="@+id/adView"
        android:layout_gravity="center_horizontal"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        ads:adSize="BANNER"
        ads:adUnitId="ca-app-pub-8036741548724898/5354201765"/>

</LinearLayout>

Explanation of source code
This activity_main.xml Layout function to determine the size of the screen on the applications we make, but it is also used to insert ID from Google Admob Banner ads are defined by the order,

You need to know when you may run this IklanBanner application will see an error message in the emulator as shown below:
<com.google.android.gms.ads.AdView
        android:id="@+id/adView"
        android:layout_gravity="center_horizontal"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        ads:adSize="BANNER"
        ads:adUnitId="ca-app-pub-8036741548724898/5354201765"/>

This activity_main.xml Layout function to determine the size of the screen on the applications we make, but it is also used to insert ID from Google Admob Banner ads are defined by the order,
You need to know when you may run this IklanBanner application will see an error message in the emulator as shown below:

android, studio, tutorial, banner, ads, Admob, ID, Application
Error Message in Genymotion
You do not have to worry if it finds an error message like the one above, the solution is from android studio> res> values> strings.xml on the ad ID you change with your own banner ad ID, and the problem was resolved. Display IklanBanner project results are as shown below.

android, studio, tutorial, banner, ads, Admob, ID, Application
Result Project IklanBanner
Admob ads in order to look more beautiful, then when you make it adapted to the size of the layout of the device. Similarly android studio tutorial how to insert ID Admob ads in the app, look forward to the continuation of this article will discuss about the ad banner by using the Listener. Thank you has been to this blog, hopefully a lot of benefits that you can take.




Android Studio tutorial how to insert banner ads Admob ID on Application Rating: 4.5 Diposkan Oleh: Unknown

1 comments:

  1. hi, i have problem when using my own app id... it show blank and no add fill . why?

    ReplyDelete