Android Google Admob

In recent years, Google released adaptive banners which are a replacement for the popular 320dp x 50dp banner ad and the smart banner. 

You can calculate its height by SDK at runtime and can optimize the UI for the user. It only requires two steps to upgrade banner ads to adaptive banners.

Google AdMob is a powerful tool for monetizing cross-platform apps and websites. It also provides different ad types for displaying different UIs to users on mobile application

Google AdMob uses banner ads, interstitial ads, native ads, and reward ads. In this blog, we explore the banner ad.

There are Four types of Google ads.

  1. Native
  2. Interstitial
  3. Banner
  4. Rewarded Video
1. Native ads: Native ads are for users' UI components as shown below. 



Step 1.1: The below code is put in the manifest file.

 <meta-data

 android:name=" com.google.android.gms.ads.APPLICATION_ID"

 android:value="ca-app-pub-xxxxxx-xxx"/>

Step 1.2: Create an activity_main.xml and below code put inside.

 <com.google.android.ads.nativetemplates.TemplateView

   android:id="@+id/template"

   app:gnt_template_type="@layout/gnt_medium_template_view"

   android:layout_width="match_parent"

   android:layout_height="wrap_content" >

 </com.google.android.ads.nativetemplates.TemplateView>

Step 1.3: MobileAds.initialize(this)

val adLoader = AdLoader.Builder(this,"ca-app-pub-3940256099942544/2247696110")

.forNativeAd {

val style = NativeTemplateStyle.Builder().withMainBackgroundColor
(ColorDrawable(Color.WHITE))

.build()

val template = findViewById<TemplateView>(R.id.my_template)

template.setStyles(style)

template.setNativeAd(it)

}.build()

//show ad

adLoader.loadAd(AdRequest.Builder().build())


2. Interstitial: Interstitial ads are full-screen UI ads. The below code implements full-screen ads.


 


Step 2.1: Create a MainActivity.java file and put the below code.

import android.support.v7.app.AppCompatActivity;

import android.os.Bundle;

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

public class MainActivity extends AppCompatActivity

{

//This method creates automatically while creating activity.

protected void onCreate (Bundle savedInstanceState)

{

super.onCreate (savedInstanceState);

setContentView (R.layout.activity_main);

// Initialize the Mobile Ads SDK

MobileAds.initialize (this, getString (R.string.admob_app_id));
}
}   

3. Banner: Banner ads are a rectangular image or text that is included in the Screen UI. The below code implements Banner ads.


 


Step 3.1: Create an activity_main.xml and below code put inside.

 <?xml version="1.0" encoding="utf-8"?>

 <RelativeLayout xmlns:android=

"http://schemas.android.com/apk/res/android"

xmlns:tools="http://schemas.android.com/tools"

android:layout_width="match_parent"

android:layout_height="match_parent"

tools:context=".MainActivity">

 //view of AdView

 <com.google.android.gms.ads.AdView

xmlns:ads="http://schemas.android.com/apk/res-auto"

android:id="@+id/adView"

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:layout_centerHorizontal="true"

android:layout_alignParentBottom="true"

ads:adSize="BANNER"

ads:adUnitId="@string/admob_banner_id">

 </com.google.android.gms.ads.AdView>

 </RelativeLayout>


Step 3.2: Create a MainActivity.java file and put the below code.

 import androidx.appcompat.app.AppCompatActivity;

 import android.os.Bundle;

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

 import com.google.android.gms.ads.initialization
 .InitializationStatus;

 import com.google.android.gms.ads.initialization
 .OnInitializationCompleteListener;


 public class MainActivity extends AppCompatActivity {

 protected void onCreate(Bundle savedInstanceState)

 {

   super.onCreate(savedInstanceState);

setContentView(R.layout.activity_main);

// Initialize the Mobile SDK

MobileAds.initialize(this, new OnInitializationCompleteListener() {

@Override

public void onInitializationComplete(InitializationStatus initializationStatus) {

Toast.makeText(this, " successful ", Toast.LENGTH_SHORT).show();}});

 }

 }

4. Rewarded Video: A rewarded video ad is​ a full screen video in an application also used in games.​ App reward ​when they watch the reward video from start to end.

 

Step 4.1: The below code is put in the manifest file.

 <meta-data

 android:name="com.google.android.gms.ads.APPLICATION_ID"

 android:value="ca-app-pub-xxxxxx-xxx"/>

Step 4.2: Create an activity_main.xml and below code put inside.

 <?xml version="1.0" encoding="utf-8"?>

 <RelativeLayout 

    xmlns:android="http://schemas.android.com/apk/res/android"

    xmlns:tools="http://schemas.android.com/tools"

    android:layout_width="match_parent"

    android:layout_height="match_parent"

    tools:context=".MainActivity">  

    <Button

        android:id="@+id/showVideoBtn"

        android:layout_width="match_parent"

        android:layout_height="wrap_content"

        android:layout_centerInParent="true"

        android:layout_margin="8dp"

        android:background="@color/colorPrimary"

        android:padding="16dp"

        android:text="Show  Rewarded  Video Ad "

        android:textColor="#ffff"

        android:textSize="20dp" />

 </RelativeLayout>


Step 4.3: Create a MainActivity.java file and put the below code.

 import android.os.Bundle;

 import android.view.View;

 import android.widget.Button;

 import android.widget.Toast;

 import androidx.appcompat.app.AppCompatActivity;

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

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

 import com.google.android.gms.ads.reward.RewardItem;

 import com.google.android.gms.ads.reward.RewardedVideoAd;

 import com.google.android.gms.ads.reward.RewardedVideoAdListener;

 public class MainActivity extends AppCompatActivity {

 // Creating a object of Button class

 Button showVideoAdBtn;

 @Override

 protected void onCreate(Bundle savedInstanceState)

 {

super.onCreate(savedInstanceState);

setContentView(R.layout.activity_main);

// link those objects with their respective id's

// that we have given in activity_main.xml file

showVideoAdBtn= (Button)findViewById(R.id.showVideoBtn);

// initializing the Google Admob SDK

MobileAds.initialize(this);

// loading Video Ad

loadRewardedVideoAd();

// click listener to show Rewarded Video Ad

showVideoAdBtn.setOnClickListener(

new View.OnClickListener() {

@Override public void onClick(View view)

{

// showing Ad

showRewardedVideoAd();

}

});

 }

// creating RewardedVideoAd object

 private RewardedVideoAd AdMobrewardedVideoAd;

// AdMob Rewarded Video Ad Id

 private String AdId= "ca-app-pub-3940256099942544/5224354917";

void loadRewardedVideoAd()

 {

// initializing RewardedVideoAd Object

// RewardedVideoAd Constructor Takes Context as its

// Argument

AdMobrewardedVideoAd= MobileAds.getRewardedVideoAdInstance(this);

// Rewarded Video Ad Listener

  AdMobrewardedVideoAd.setRewardedVideoAdListener(new RewardedVideoAdListener() {

  @Override

 public void onRewardedVideoAdLoaded()

 {

// Showing Toast Message

Toast

.makeText(MainActivity.this,

"onRewardedVideoAdLoaded",

Toast.LENGTH_SHORT)

.show();

 }

 @Override

public void onRewardedVideoAdOpened()

  {

// Showing Toast Message

Toast

.makeText(MainActivity.this,

"onRewardedVideoAdOpened",

Toast.LENGTH_SHORT)

.show();

 }

 

@Override

public void onRewardedVideoStarted()

{

// Showing Toast Message

Toast

.makeText(MainActivity.this,

"onRewardedVideoStarted",

Toast.LENGTH_SHORT)

.show();

}

  @Override

public void onRewardedVideoAdClosed()

 {

// Showing Toast Message

Toast

.makeText(MainActivity.this,

"onRewardedVideoAdClosed",

Toast.LENGTH_SHORT)

.show();

}

@Override

public void onRewarded(RewardItem rewardItem)

{

// Showing Toast Message

Toast

.makeText(MainActivity.this,

"onRewarded",

Toast.LENGTH_SHORT)

.show();

 }

 @Override

public void onRewardedVideoAdLeftApplication()

{

// Showing Toast Message

Toast

.makeText(

MainActivity.this,

"onRewardedVideoAdLeftApplication",

Toast.LENGTH_SHORT)

.show();

}

@Override

public void onRewardedVideoAdFailedToLoad(int i)

 {

// Showing Toast Message

Toast

.makeText(

MainActivity.this,

"onRewardedVideoAdFailedToLoad",

Toast.LENGTH_SHORT)

.show();

}

 

 @Override

public void onRewardedVideoCompleted()

{

// Showing Toast Message

Toast

.makeText(

MainActivity.this,

"onRewardedVideoCompleted",

Toast.LENGTH_SHORT)

.show();

}

});

 

// Loading Rewarded Video Ad

AdMobrewardedVideoAd.loadAd(

AdId, new AdRequest.Builder().build());

}

public void showRewardedVideoAd()

{

// Checking If Ad is Loaded or Not

if (AdMobrewardedVideoAd.isLoaded()) {

// showing Video Ad

AdMobrewardedVideoAd.show();

}

else {

// Loading Rewarded Video Ad

AdMobrewardedVideoAd.loadAd(

AdId, new AdRequest.Builder().build());

 } 

 }

 }

Happy Coding!


365Bloggy May 7, 2024
Share this post
Tags
SUBSCRIBE THIS FORM


Archive