Cara Pasang Iklan Admob Di Applikasi

0
461

Menampilkan iklan admob pada applikasi android

Cara menampilkan iklan Admob di apllikasi android sangatlah mudah,sebelumnya kalian harus mempunyai akun admob,dan bagi yang belum punya silahkan daftar disini

Dan dibawah ini tampilan iklan interstitial jika button di klik

Kali ini saya membuat applikasi sederhana hanya untuk menampilkan iklan saja,dan menggunakan kode iklan example dari admob,bagi kalian juga disarankan pakai kode iklan example dari admob dulu untuk menghindari invalid activity,nanti kalau applikasi sudah siap diupload ke play store,tinggal ganti kode iklan milik kalian.

Contoh iklan banner

Langsung saja buka android studio kalian,buat project pilih empty activity,

Tambahkan kode berikut pada res/values/strings.xml

<string name="banner_ad_unit_id">ca-app-pub-3940256099942544/6300978111</string>
<string name="admob_interstitial_id">ca-app-pub-3940256099942544/1033173712</string>
<string name="buton_iklan">Iklan interstitial</string>

Tambahkan kode berikut pada Build.gradle

implementation 'com.google.android.gms:play-services-ads:19.0.1'

Code penempatan seperti berikut

Tekan Sync Now

Nah pada activity_main.xml silahkan Tambahkan kode berikut unuk menampilkan iklan banner dibawah

<com.google.android.gms.ads.AdView
       xmlns:ads="http://schemas.android.com/apk/res-auto"
       android:id="@+id/ad_view"
       android:layout_width="match_parent"
       android:layout_height="wrap_content"
       android:layout_centerHorizontal="true"
       android:layout_alignParentBottom="true"
       ads:adSize="BANNER"
       ads:adUnitId="@string/banner_ad_unit_id" />

Atau salin semua kode activity_main.xml berikut

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity">

    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent">


        <Button
            android:id="@+id/buttoniklan"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_centerInParent="true"
            android:text="@string/buton_iklan" />
    <com.google.android.gms.ads.AdView
        xmlns:ads="http://schemas.android.com/apk/res-auto"
        android:id="@+id/ad_view"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_centerHorizontal="true"
        android:layout_alignParentBottom="true"
        ads:adSize="BANNER"
        ads:adUnitId="@string/banner_ad_unit_id" />

</RelativeLayout>
</androidx.constraintlayout.widget.ConstraintLayout>

Selanjutnya pada MainActivity.java pastekan kode berikut

import android.os.Bundle;
import android.view.View;
import android.widget.Button;
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;
import com.google.android.gms.ads.MobileAds;
import com.google.android.gms.ads.initialization.InitializationStatus;
import com.google.android.gms.ads.initialization.OnInitializationCompleteListener;

Tambahkan kode lagi diatas @Override

private AdView adView;
private InterstitialAd interstitial;

Letakkan kode berikut di MainActivity,java di bawah setContentView(R.layout.activity_main);

Button mybutton = (Button) findViewById(R.id.buttoniklan);
       mybutton .setOnClickListener(new View.OnClickListener() {
           @Override public void onClick(View v) {
               interstitial = new InterstitialAd(MainActivity.this);
               interstitial = new InterstitialAd(getApplicationContext());
               interstitial.setAdUnitId(getString(R.string.admob_interstitial_id));
               AdRequest adRequest = new AdRequest.Builder().build();
               interstitial.loadAd(adRequest);
               interstitial.setAdListener(new AdListener() { public void onAdLoaded() { if (interstitial.isLoaded()) { interstitial.show(); } } }); } });


       // Initialize the Mobile Ads SDK.
       MobileAds.initialize(this, new OnInitializationCompleteListener() {
           @Override
           public void onInitializationComplete(InitializationStatus initializationStatus) {}
       });

       // Gets the ad view defined in layout/ad_fragment.xml with ad unit ID set in
       // values/strings.xml.
       adView = findViewById(R.id.ad_view);

       // Create an ad request. Check your logcat output for the hashed device ID to
       // get test ads on a physical device. e.g.
       // "Use AdRequest.Builder.addTestDevice("ABCDEF012345") to get test ads on this device."
       AdRequest adRequest = new AdRequest.Builder()
               .addTestDevice(AdRequest.DEVICE_ID_EMULATOR)
               .build();

       // Start loading the ad in the background.
       adView.loadAd(adRequest);
   }




   /** Called when leaving the activity */
   @Override
   public void onPause() {
       if (adView != null) {
           adView.pause();
       }
       super.onPause();
   }

   /** Called when returning to the activity */
   @Override
   public void onResume() {
       super.onResume();
       if (adView != null) {
           adView.resume();
       }
   }

   /** Called before the activity is destroyed */
   @Override
   public void onDestroy() {
       if (adView != null) {
           adView.destroy();
       }
       super.onDestroy();
   }

 

Untuk kode MainActivity.java selengkapnya seperti berikut

import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
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;
import com.google.android.gms.ads.MobileAds;
import com.google.android.gms.ads.initialization.InitializationStatus;
import com.google.android.gms.ads.initialization.OnInitializationCompleteListener;

/**
 * Main Activity. Inflates main activity xml and child fragments.
 */
public class MainActivity extends AppCompatActivity {

    private AdView adView;
    private InterstitialAd interstitial;

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

        Button mybutton = (Button) findViewById(R.id.buttoniklan);
        mybutton .setOnClickListener(new View.OnClickListener() {
            @Override public void onClick(View v) {
                interstitial = new InterstitialAd(MainActivity.this);
                interstitial = new InterstitialAd(getApplicationContext());
                interstitial.setAdUnitId(getString(R.string.admob_interstitial_id));
                AdRequest adRequest = new AdRequest.Builder().build();
                interstitial.loadAd(adRequest);
                interstitial.setAdListener(new AdListener() { public void onAdLoaded() { if (interstitial.isLoaded()) { interstitial.show(); } } }); } });


        // Initialize the Mobile Ads SDK.
        MobileAds.initialize(this, new OnInitializationCompleteListener() {
            @Override
            public void onInitializationComplete(InitializationStatus initializationStatus) {}
        });

        // Gets the ad view defined in layout/ad_fragment.xml with ad unit ID set in
        // values/strings.xml.
        adView = findViewById(R.id.ad_view);

        // Create an ad request. Check your logcat output for the hashed device ID to
        // get test ads on a physical device. e.g.
        // "Use AdRequest.Builder.addTestDevice("ABCDEF012345") to get test ads on this device."
        AdRequest adRequest = new AdRequest.Builder()
                .addTestDevice(AdRequest.DEVICE_ID_EMULATOR)
                .build();

        // Start loading the ad in the background.
        adView.loadAd(adRequest);
    }




    /** Called when leaving the activity */
    @Override
    public void onPause() {
        if (adView != null) {
            adView.pause();
        }
        super.onPause();
    }

    /** Called when returning to the activity */
    @Override
    public void onResume() {
        super.onResume();
        if (adView != null) {
            adView.resume();
        }
    }

    /** Called before the activity is destroyed */
    @Override
    public void onDestroy() {
        if (adView != null) {
            adView.destroy();
        }
        super.onDestroy();
    }
}

Sekarang tambahkan permision pada AndroidManifest.xml

<uses-permission android:name="android.permission.INTERNET" />

Masih di AndroidManifest.xml,Tambahkan kode berikut sebagai activity,ini adalah id admob,dan nanti kalau kalian mau menggunakan kode admob pakai akun kalian,kode ini harus diganti dengan id kalian,dan kode iklan banner sama interstitial pun harus diganti dengan lode iklan admob kalian

<!-- Sample AdMob App ID: ca-app-pub-3940256099942544~3347511713 -->
       <meta-data
           android:name="com.google.android.gms.ads.APPLICATION_ID"
           android:value="ca-app-pub-3940256099942544~3347511713"/>

Kode keseluruhan AndroidManifest.xml

  <uses-permission android:name="android.permission.INTERNET" />

    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >

        <!-- Sample AdMob App ID: ca-app-pub-3940256099942544~3347511713 -->
        <meta-data
            android:name="com.google.android.gms.ads.APPLICATION_ID"
            android:value="ca-app-pub-3940256099942544~3347511713"/>

        <activity
            android:name=".MainActivity"
            android:label="@string/app_name" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>

</manifest>

Sekarang coba Run,untuk menampilkan iklan Interstitial tekan button lihat iklan,tampilannya akan seperti ini

Nah mudahkan dan Sekali lagi ini hanya applikasi sederhana untuk menampilkan iklan admob example saja,untuk penempatan lainnya kalian bisa berexperiment sendiri atau menunggu tutorial dari saya,semoga artikel ini bisa membantu.

Baca juga Cara Pasang Iklan StartApp Di Applikasi Android