<p>In the previous article, <strong><span style="color: #008000;"><a style="color: #008000;" href="https://c1ctech.com/android-how-to-integrate-google-admob-in-your-apppart-i/">How to Integrate Banner and Interstitial Ads in your App(Part I)</a></span></strong> we have talked about how to integrate 2 different types of ads in your application, <strong><span style="color: #0000ff;">Banners ads</span> </strong>and <strong><span style="color: #0000ff;">Interstitial ads</span></strong>.</p>
<p>Now In this article, we will talk about how to integrate the other two ads in your application ie. <strong><span style="color: #0000ff;">Rewarded video ads</span></strong> and <span style="color: #0000ff;"><strong>Native Ads</strong></span> in brief.</p>
<p><amp-youtube layout="responsive" width="1200" height="675" data-videoid="EMZDV9RApkU" title="Android Integrating AdMob"><a placeholder href="https://youtu.be/EMZDV9RApkU"><img src="https://i.ytimg.com/vi/EMZDV9RApkU/hqdefault.jpg" layout="fill" object-fit="cover" alt="Android Integrating AdMob"></a></amp-youtube></p>
<h3><span style="color: #000080;"><strong>Creating Ad Units</strong></span></h3>
<p class="p1"><b>1</b>. Sign into your <span style="color: #008000;"><strong><a style="color: #008000;" href="https://apps.admob.com/"><span class="s1">AdMob</span></a></strong></span> account.</p>
<p class="p1"><strong>2 </strong>. From the left side click on <span style="color: #008000;"><strong>Apps</strong></span> and then select the newly created App(In my case <strong>AndroidAdMobDemo</strong>).</p>
<p class="p1"><strong>3</strong>. Inside your App name select <span style="color: #008000;"><strong>Ad units</strong></span> and click on <span style="color: #008000;"><b>ADD AD UNIT</b> </span>button to create a new ad unit.</p>
<p><img class="alignnone size-full wp-image-1484" src="https://c1ctech.com/wp-content/uploads/2020/01/Screenshot-2020-01-24-13.17.28.png" alt="Screenshot 2020-01-24 13.17.28" width="2378" height="1082" /></p>
<p><b>4</b>. To create Rewarded Ad select <span style="color: #0000ff;"><strong>Rewarded</strong></span> ad format. Now give the ad unit a name and also enter the <span style="color: #008000;"><strong>reward amount</strong></span> and <strong><span style="color: #008000;">reward item</span></strong> to offer some reward points/coins only if the user watches the ad video completely. Finally, click on <span style="color: #008000;"><strong>CREATE AD UNIT </strong><span style="color: #000000;">button</span><strong>.</strong></span></p>
<p><img class=" wp-image-1506 aligncenter" src="https://c1ctech.com/wp-content/uploads/2020/01/Screenshot-2020-01-31-15.56.10.png" alt="Screenshot 2020-01-31 15.56.10" width="603" height="375" /></p>
<p> ;</p>
<p><b>5</b>. To create Native Ad select <span style="color: #0000ff;"><strong>Native</strong></span> ad format. Now give the ad unit a name and then click on <span style="color: #008000;"><strong>CREATE AD UNIT </strong><span style="color: #000000;">button</span></span>.</p>
<p><img class=" wp-image-1503 aligncenter" src="https://c1ctech.com/wp-content/uploads/2020/01/Screenshot-2020-01-29-19.51.51.png" alt="Screenshot 2020-01-29 19.51.51" width="612" height="259" /></p>
<p><strong>6</strong>. Once the ad unit is created, you can notice the <span style="color: #008000;"><b>Ad unit ID</b></span> on the dashboard. An example of ad unit id looks like <b><span style="color: #008000;">ca-app-pub-962XXXXXXX/XXXXXXXXXXX</span>.</b></p>
<p>Now you are ready to use above generated ads in your application using its ad unit ID.</p>
<p><span style="color: #0000ff;"><strong>Note: Always use test Ad ID while you are testing your application, you should never use original Ad ID in your application when in testing/developing phase. If you do so you might get blocked from AdMob.</strong></span></p>
<p>Now before adding any types of ads, we have to first follow some steps to set up AdMob in our application:</p>
<ul>
<li>Import the Mobile Ads SDK</li>
<li>Update <span style="color: #008000;"><strong>AndroidManifest.xml</strong></span> with the AdMob<strong><span style="color: #008000;"> App ID</span></strong></li>
<li>Initialize mobile ads SDK</li>
</ul>
<p>To understand the above three-step setup, in brief, refer my previous <strong><span style="color: #0000ff;"><a style="color: #0000ff;" href="https://c1ctech.com/android-how-to-integrate-google-admob-in-your-apppart-i/">article</a></span></strong>.</p>
<h3><span style="color: #000080;"><strong>Adding Rewarded Video Ad</strong></span></h3>
<p><span style="color: #008000;"><strong><a style="color: #008000;" href="https://developers.google.com/admob/android/rewarded-video"><span class="s1">Rewarded</span></a></strong></span> Video Ads are fullscreen video ads which offers some reward points if the user watches the ad video. These ads are very useful to offer some reward points/coins in video games.</p>
<p class="p1">Now we will see how to implement video ads that can be used to give rewards to the user.</p>
<p class="p1">Just as in interstitial ads, we don’t need to add anything to our layout, these ads can only be implemented programmatically.</p>
<p class="p1">Create a global variable of <strong><span style="color: #008000;">RewardedVideoAd</span></strong><span style="color: #008000;"><span style="color: #000000;"> and then </span></span>initialize it in <strong><span style="color: #008000;">onCreate</span></strong> using <strong><span style="color: #008000;">MobileAds.getRewardedVideoAdInstance(this)</span>.</strong></p>
<pre>public class RewardedVideoAdActivity extends AppCompatActivity {

<strong><span style="color: #008000;"> RewardedVideoAd mRewardedVideoAd;</span></strong>

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

 <strong><span style="color: #008000;">mRewardedVideoAd = MobileAds.getRewardedVideoAdInstance(this);
</span></strong>
 }
}</pre>
<h4><span style="color: #0000ff;"><strong>Loading Ad</strong></span></h4>
<p class="p1">To load <strong><span style="color: #008000;">RewardedVideoAd</span></strong> you need to call the <strong><span style="color: #008000;">loadAd(adUnitId, AdRequest)</span></strong> on the ad object.</p>
<pre>AdRequest adRequest = new AdRequest.Builder()
 .build();

<strong><span style="color: #008000;">mRewardedVideoAd.loadAd("ca-app-pub-3940256099942544/5224354917", adRequest);</span></strong></pre>
<p class="p1">Google provides a testing id for rewarded video ads, make sure to use exactly this ID when testing/debugging your application.</p>
<pre class="p1">ca-app-pub-3940256099942544/5224354917</pre>
<h4><strong><span style="color: #0000ff;">Adding Listener</span></strong></h4>
<p class="p1">As with other types of ads you can listen to various ad events by setting a <span style="color: #000000;">RewardedVideoAdListener</span> on <span style="color: #008000;"><strong>RewardedVideoAd</strong></span> object using <span style="color: #008000;"><strong>setRewardedVideoAdListener(RewardedVideoAdListener).</strong></span></p>
<pre>mRewardedVideoAd.setRewardedVideoAdListener(new RewardedVideoAdListener() {

 <span style="color: #008000;"><strong> //This method is executed when an ad has finished loading</strong></span>
 @Override
 public void onRewardedVideoAdLoaded() {
 Toast.makeText(RewardedVideoAdActivity.this, "onRewardedVideoAdLoaded", Toast.LENGTH_SHORT).show();

 showRewardedVideoAd();
 }

 <strong><span style="color: #008000;"> //method is invoked when the user should be rewarded for interacting with the ad</span></strong>
 @Override
 public void onRewarded(RewardItem rewardItem) {
 Toast.makeText(RewardedVideoAdActivity.this, "onRewarded! currency: " + rewardItem.getType() + " amount: " + rewardItem.getAmount(), Toast.LENGTH_SHORT).show();

 }

 <strong><span style="color: #008000;">//called after onAdOpened(), when a user click opens another app,
 //backgrounding the current app</span></strong>
 @Override
 public void onRewardedVideoAdLeftApplication() {
 Toast.makeText(RewardedVideoAdActivity.this, "onRewardedVideoAdLeftApplication",
 Toast.LENGTH_SHORT).show();
 }

 <strong><span style="color: #008000;">//method is invoked when the rewarded ad is closed
 // due to the user tapping on the close icon or using the back button.</span></strong>
 @Override
 public void onRewardedVideoAdClosed() {
 Toast.makeText(RewardedVideoAdActivity.this, "onRewardedVideoAdClosed", Toast.LENGTH_SHORT).show();
 }

 <strong><span style="color: #008000;">//method is invoked when an ad fails to load</span></strong>
 @Override
 public void onRewardedVideoAdFailedToLoad(int errorCode) {
 Toast.makeText(RewardedVideoAdActivity.this, "onRewardedVideoAdFailedToLoad" + errorCode, Toast.LENGTH_SHORT).show();
 }

 <strong><span style="color: #008000;">//method is invoked when the ad is displayed, covering 
 //the device's screen.
 @Override</span></strong>
 public void onRewardedVideoAdOpened() {
 Toast.makeText(RewardedVideoAdActivity.this, "onRewardedVideoAdOpened", Toast.LENGTH_SHORT).show();
 }

 <strong><span style="color: #008000;">//method is invoked when rewarded video started</span></strong>
 @Override
 public void onRewardedVideoStarted() {
 Toast.makeText(RewardedVideoAdActivity.this, "onRewardedVideoStarted", Toast.LENGTH_SHORT).show();
 }

 <strong><span style="color: #008000;">//method is invoked when rewarded video completed</span></strong>
 @Override
 public void onRewardedVideoCompleted() {
 Toast.makeText(RewardedVideoAdActivity.this, "onRewardedVideoCompleted", Toast.LENGTH_SHORT).show();
 }
});</pre>
<p class="p1">Always check if the ad is loaded before showing the ad.</p>
<pre>private void showRewardedVideoAd() {
 if (mRewardedVideoAd.isLoaded()) {
 mRewardedVideoAd.show();
 }
}</pre>
<h4><span style="color: #000080;"><strong>Complete code:</strong></span></h4>
<p>Create a new activity and named it as <span style="color: #008000;"><b>RewardedVideoAdActivity.java</b></span> and add the below code.</p>
<p><span style="color: #0000ff;"><b>RewardedVideoAdActivity.java</b></span></p>
<pre>package com.example.androidadmobdemo;

import android.os.Bundle;
import android.widget.Toast;

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;

import androidx.appcompat.app.AppCompatActivity;

public class RewardedVideoAdActivity extends AppCompatActivity {

 RewardedVideoAd mRewardedVideoAd;

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

 mRewardedVideoAd = MobileAds.getRewardedVideoAdInstance(this);

 AdRequest adRequest = new AdRequest.Builder().build();

 mRewardedVideoAd.loadAd("ca-app-pub-3940256099942544/5224354917", adRequest);

 mRewardedVideoAd.setRewardedVideoAdListener(new RewardedVideoAdListener() {

 <strong><span style="color: #008000;">//This method is executed when an ad has finished loading</span></strong>
 @Override
 public void onRewardedVideoAdLoaded() {
 Toast.makeText(RewardedVideoAdActivity.this, "onRewardedVideoAdLoaded", Toast.LENGTH_SHORT).show();

 showRewardedVideoAd();
 }

 <span style="color: #008000;"><strong> //method is invoked when the user should be rewarded </strong></span>
<span style="color: #008000;"><strong> //for interacting with the ad</strong></span>
 @Override
 public void onRewarded(RewardItem rewardItem) {
 Toast.makeText(RewardedVideoAdActivity.this, "onRewarded! currency: " + rewardItem.getType() + " amount: " + rewardItem.getAmount(), Toast.LENGTH_SHORT).show();

 }

 <strong><span style="color: #008000;"> //called after onAdOpened(), when a user click opens </span></strong>
<strong><span style="color: #008000;"> //another app, backgrounding the current app</span></strong>
 @Override
 public void onRewardedVideoAdLeftApplication() {
 Toast.makeText(RewardedVideoAdActivity.this, "onRewardedVideoAdLeftApplication",
 Toast.LENGTH_SHORT).show();
 }

 <strong><span style="color: #008000;">//method is invoked when the rewarded ad is closed
 // due to the user tapping on the close icon or </span></strong>
<strong><span style="color: #008000;"> //using the back button.</span></strong>
 @Override
 public void onRewardedVideoAdClosed() {
 Toast.makeText(RewardedVideoAdActivity.this, "onRewardedVideoAdClosed", Toast.LENGTH_SHORT).show();
 }

 <strong><span style="color: #008000;">//method is invoked when an ad fails to load</span></strong>
 @Override
 public void onRewardedVideoAdFailedToLoad(int errorCode) {
 Toast.makeText(RewardedVideoAdActivity.this, "onRewardedVideoAdFailedToLoad" + errorCode, Toast.LENGTH_SHORT).show();
 }

 <strong><span style="color: #008000;">//method is invoked when the ad is displayed, </span></strong>
<strong><span style="color: #008000;"> //covering the device's screen.</span></strong>
 @Override
 public void onRewardedVideoAdOpened() {
 Toast.makeText(RewardedVideoAdActivity.this, "onRewardedVideoAdOpened", Toast.LENGTH_SHORT).show();
 }

 <strong><span style="color: #008000;">//method is invoked when rewarded video started</span></strong>
 @Override
 public void onRewardedVideoStarted() {
 Toast.makeText(RewardedVideoAdActivity.this, "onRewardedVideoStarted", Toast.LENGTH_SHORT).show();
 }

 <strong><span style="color: #008000;">//method is invoked when rewarded video completed</span></strong>
 @Override
 public void onRewardedVideoCompleted() {
 Toast.makeText(RewardedVideoAdActivity.this, "onRewardedVideoCompleted", Toast.LENGTH_SHORT).show();
 }
 });

 }

 private void showRewardedVideoAd() {
 if (mRewardedVideoAd.isLoaded()) {
 mRewardedVideoAd.show();
 }
 }


}</pre>
<p class="p1">Once loaded the ad will look something like this.</p>
<p><img class="alignnone wp-image-1500" src="https://c1ctech.com/wp-content/uploads/2020/01/Screenshot_1580381980.png" alt="Screenshot_1580381980" width="344" height="612" /></p>
<h3><span style="color: #000080;"><strong>Adding Native Ad</strong></span></h3>
<p><strong><a href="https://developers.google.com/admob/android/native/start"><span style="color: #008000;">Native</span><span style="color: #008000;">Ads</span></a></strong>, as the name suggests allow you to design an ad experience that feels like a natural part of your app. You can customize the look and feel of native ads the way you’d design your app content.</p>
<p>Native Ads implementation can be divided into two tasks:</p>
<ul>
<li>Receiving the ads from AdMob server (via the <strong><span style="color: #008000;">Google Mobile Ads SDK</span></strong>).</li>
<li>Displaying the ads received in the format we want.</li>
</ul>
<h4><span style="color: #000080;"><strong>Receiving Native Ads</strong></span></h4>
<h5><span style="color: #0000ff;"><strong>Build an AdLoader</strong></span></h5>
<p class="p1">Native ads are loaded using the <span style="color: #0000ff;"><strong><a style="color: #0000ff;" href="https://developers.google.com/android/reference/com/google/android/gms/ads/AdLoader"><span class="s1">AdLoader</span></a></strong></span> class. The following code demonstrates how to build an <span class="s2">AdLoader</span> that can load unified native ads:</p>
<pre>AdLoader adLoader = new AdLoader.Builder(this, "ca-app-pub-3940256099942544/2247696110")
 .forUnifiedNativeAd(new UnifiedNativeAd.OnUnifiedNativeAdLoadedListener() {
 @Override
 public void onUnifiedNativeAdLoaded(UnifiedNativeAd unifiedNativeAd) {
 <strong><span style="color: #008000;"> //the native ad will be available inside this method (unifiedNativeAd)</span></strong>

 }
 }).build;</pre>
<p>Google provides a testing id for native ad, make sure to use exactly this ID when testing/debugging your application.</p>
<pre>ca-app-pub-3940256099942544/2247696110</pre>
<p><span class="s1"><span style="color: #0000ff;"><a style="color: #0000ff;" href="https://developers.google.com/android/reference/com/google/android/gms/ads/AdLoader.Builder#forUnifiedNativeAd(com.google.android.gms.ads.formats.UnifiedNativeAd.OnUnifiedNativeAdLoadedListener)"><strong>forUnifiedNativeAd()</strong></a></span> used above</span> is responsible for preparing the <span class="s1">AdLoader</span> for the <strong><span style="color: #008000;">UnifiedNativeAd</span></strong> format.</p>
<p class="p1">Calling this method configures the <span class="s1">AdLoader</span> to request unified native ads. When an ad has loaded successfully, the listener object&#8217;s <span style="color: #008000;"><strong><span class="s1">onUnifiedNativeAdLoaded()</span></strong></span> method is called.</p>
<h5><strong><span style="color: #0000ff;">Adding Listener with an AdLoader</span></strong></h5>
<p class="p1">During creation of the <span class="s1">AdLoader</span>, the <span style="color: #008000;"><strong><span class="s2">withAdListener</span></strong></span> function sets an <span style="color: #008000;"><strong><span class="s2">AdListener</span></strong></span>.</p>
<p class="p1"><span style="color: #008000;"><strong><span class="s2">withAdListener</span></strong></span> method takes an <span class="s1">AdListener</span> as its lone parameter, which receives callbacks from the <span class="s1">AdLoader</span> when ad lifecycle events take place:</p>
<pre>AdLoader adLoader = new AdLoader.Builder(this, "ca-app-pub-3940256099942544/2247696110")
 .forUnifiedNativeAd(new UnifiedNativeAd.OnUnifiedNativeAdLoadedListener() {
 @Override
 public void onUnifiedNativeAdLoaded(UnifiedNativeAd unifiedNativeAd) {
 <strong><span style="color: #008000;">//the native ad will be available inside this method (unifiedNativeAd)</span></strong>

 }
 })
 .withAdListener(new AdListener() {
 <strong><span style="color: #008000;">// Code to be executed when an ad request fails.</span></strong>
 @Override
 public void onAdFailedToLoad(int errorCode) {
 Toast.makeText(getApplicationContext(), "Ad failed to load! error code: " + errorCode, Toast.LENGTH_SHORT).show();
 }

 <strong><span style="color: #008000;"> //method is invoked when the rewarded ad is closed</span></strong>
<strong><span style="color: #008000;"> //due to the user tapping on the close icon or </span></strong>
<strong><span style="color: #008000;"> //using the back button.</span></strong>
 @Override
 public void onAdClosed() {
 Toast.makeText(getApplicationContext(), "Ad is closed!", Toast.LENGTH_SHORT).show();
 }

 <strong><span style="color: #008000;">//called after onAdOpened(), when a user click opens another app,</span></strong>
<strong><span style="color: #008000;"> //backgrounding the current app</span></strong>
 @Override
 public void onAdLeftApplication() {
 Toast.makeText(getApplicationContext(), "Ad left application!", Toast.LENGTH_SHORT).show();
 }

 <strong><span style="color: #008000;">//Code to be executed when an ad opens an overlay that</span></strong>
<strong><span style="color: #008000;"> // covers the screen.</span></strong>
 @Override
 public void onAdOpened() {
 Toast.makeText(getApplicationContext(), "Ad is opened!", Toast.LENGTH_SHORT).show();
 }

 <span style="color: #008000;"><strong>// Code to be executed when the user clicks on an ad.</strong></span>
 @Override
 public void onAdClicked() {
 Toast.makeText(getApplicationContext(), "Ad is clicked!", Toast.LENGTH_SHORT).show();

 }

 })
 .build();</pre>
<p class="p1">The one important difference between the way <span class="s1">AdListener</span> objects work with native ads and the way they work with banners and interstitials is that <span class="s1">AdLoader</span> has its own format-specific listeners<span style="color: #008000;"><strong> (i.e., <span class="s2">UnifiedNativeAd.OnUnifiedNativeAdLoadedListener</span>)</strong> </span>to use when an ad has loaded, the<strong><span style="color: #008000;"> </span></strong><span class="s2"><strong><span style="color: #008000;">onAdLoaded(</span></strong>)</span> method from <span class="s1">AdListener</span> is not called when a native ad loads successfully.</p>
<h5 class="p1"><span style="color: #0000ff;"><b>Loading ads</b></span></h5>
<p class="p2">Once you&#8217;ve finished building an <span class="s1">AdLoader</span>, it&#8217;s time to use it to load ads. There are two methods available for this: <span class="s1">loadAd()</span> and <span class="s1">loadAds()</span>.</p>
<p class="p2"><span class="s1"><strong><span style="color: #0000ff;">loadAd()</span> </strong>: </span>This method sends a request for a single ad.</p>
<!-- WP QUADS Content Ad Plugin v. 2.0.98.1 -->
<div class="quads-location quads-ad2" id="quads-ad2" style="float:none;margin:0px;">

</div>

<pre>adLoader.loadAd(new AdRequest.Builder().build());</pre>
<p><span class="s1"><strong><span style="color: #0000ff;">loadAds()</span></strong> : </span>This method sends a request for multiple ads(up to 5).</p>
<pre>adLoader.loadAds(new AdRequest.Builder().build(),4);</pre>
<p class="p1">Both of these methods take an <strong><span style="color: #008000;"><a style="color: #008000;" href="https://developers.google.com/android/reference/com/google/android/gms/ads/AdRequest"><span class="s1">AdRequest</span></a></span></strong> object as their first parameter but <strong><span class="s2" style="color: #008000;">loadAds() </span></strong>takes an additional parameter ie. the number of ads the SDK should attempt to load for the request.</p>
<h4><span style="color: #000080;"><strong>Displaying Native Ads</strong></span></h4>
<p class="p1"><span style="color: #0000ff;"><span class="s1"><b>UnifiedNativeAdView</b></span><b> class</b></span></p>
<p>To display native ads in our application which we get from AdMob server we make use of <span class="s2"><strong><span style="color: #008000;">UnifiedNativeAdView</span></strong> class which is corresponding to the <strong><span style="color: #008000;"><span class="s1">UnifiedNativeAd</span></span></strong> format.</span></p>
<p class="p2">This class is a <strong><span style="color: #008000;"><span class="s2">ViewGroup</span></span></strong> that publishers should use as the root for the <span class="s1">UnifiedNativeAd</span>.</p>
<p class="p2">A single <span style="color: #008000;"><strong><span class="s1">UnifiedNativeAdView</span></strong></span> corresponds to a single unified native ad. Each view used to display that ad&#8217;s assets (<span class="s1">ImageView, TextView, MediaView, etc</span>) should be a child of the <span class="s1">UnifiedNativeAdView</span> object.</p>
<p class="p1">Create a new layout resource file and name it as <span style="color: #008000;"><strong>native_ad_layout.xml</strong></span> which defines the view hierarchy for a unified native ad that uses a <span style="color: #008000;"><strong><span class="s1">LinearLayout</span></strong></span> to display its asset views might look like this:</p>
<p><span style="color: #0000ff;"><strong>native_ad_layout.xml</strong></span></p>
<pre><;?xml version="1.0" encoding="utf-8"?>;
<;com.google.android.gms.ads.formats.UnifiedNativeAdView
 xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent"
 android:layout_height="match_parent">;

 <;LinearLayout
 android:layout_width="match_parent"
 android:layout_height="wrap_content"
 android:background="#000"
 android:orientation="vertical">;

 <;LinearLayout
 android:layout_width="match_parent"
 android:layout_height="wrap_content"
 android:orientation="horizontal"
 android:padding="10dp">;

 <;ImageView
 android:id="@+id/ad_icon"
 android:layout_width="40dp"
 android:layout_height="40dp"
 android:layout_marginRight="20dp"
 android:background="#fff" />;

 <;LinearLayout
 android:layout_width="match_parent"
 android:layout_height="wrap_content"
 android:orientation="vertical">;

 <;TextView
 android:id="@+id/ad_headline"
 android:layout_width="wrap_content"
 android:layout_height="wrap_content"
 android:text="headline"
 android:textColor="#fff" />;

 <;TextView
 android:id="@+id/ad_advertiser"
 android:layout_width="wrap_content"
 android:layout_height="wrap_content"
 android:text="advertiser"
 android:textColor="#fff" />;

 <;RatingBar
 android:id="@+id/ad_rating"
 style="?android:attr/ratingBarStyleSmall"
 android:layout_width="wrap_content"
 android:layout_height="wrap_content"
 android:isIndicator="true"
 android:numStars="5"
 android:stepSize="0.5" />;
 <;/LinearLayout>;
 <;/LinearLayout>;

 <strong><span style="color: #008000;"><;com.google.android.gms.ads.formats.MediaView</span></strong>
<strong><span style="color: #008000;"> android:id="@+id/ad_media"</span></strong>
<strong><span style="color: #008000;"> android:layout_width="match_parent"</span></strong>
<strong><span style="color: #008000;"> android:layout_height="175dp"</span></strong>
<strong><span style="color: #008000;"> android:background="#fff">;</span></strong>

<strong><span style="color: #008000;"> <;/com.google.android.gms.ads.formats.MediaView>;</span></strong>

 <;TextView
 android:id="@+id/ad_body"
 android:layout_width="match_parent"
 android:layout_height="match_parent"
 android:padding="10dp"
 android:text="body of ad"
 android:textColor="#fff" />;

 <;LinearLayout
 android:layout_width="match_parent"
 android:layout_height="wrap_content"
 android:gravity="right"
 android:padding="10dp">;

 <;TextView
 android:id="@+id/ad_store"
 android:layout_width="wrap_content"
 android:layout_height="wrap_content"
 android:padding="10dp"
 android:text="Store"
 android:textColor="#fff" />;

 <;TextView
 android:id="@+id/ad_price"
 android:layout_width="wrap_content"
 android:layout_height="wrap_content"
 android:padding="10dp"
 android:text="Price"
 android:layout_marginRight="8dp"
 android:textColor="#fff" />;


 <;Button
 android:id="@+id/ad_call_to_action"
 android:layout_width="wrap_content"
 android:layout_height="wrap_content"
 android:background="#fff"
 android:text="Action"
 android:padding="10dp"
 android:textColor="#000" />;
 <;/LinearLayout>;
 <;/LinearLayout>;


<;/com.google.android.gms.ads.formats.UnifiedNativeAdView>;</pre>
<p class="p1">In the above layout, <span style="color: #008000;"><strong><span class="s1">MediaView</span></strong></span> is a special <span style="color: #008000;"><strong><span class="s1">View</span></strong></span> designed to display the main media asset, either video or image. It should be placed within the view hierarchy of a <strong><span class="s1" style="color: #008000;">NativeAdView</span></strong>, just like any other asset view.</p>
<h4><span style="color: #000080;"><strong>Steps to display unified native ad</strong></span></h4>
<p class="p1">These are the steps for displaying a unified native ad:</p>
<ol class="ol1">
<li class="li1">Create an instance of the <span class="s1">UnifiedNativeAdView</span> class.</li>
<li class="li1">For each ad asset to be displayed:
<ul class="ul1">
<li class="li1">Populate the asset view with the asset in the ad object.</li>
<li class="li1">Register the asset view with the <span class="s1">ViewGroup</span> class.</li>
</ul>
</li>
<li class="li1">Register the ad object with the <span class="s1">ViewGroup</span> class.</li>
</ol>
<p>Let&#8217;s take a look at the individual steps:</p>
<h5><strong><span style="color: #0000ff;">Inflate the Layout</span></strong></h5>
<p>We&#8217;re inflating an XML layout(<strong><span style="color: #008000;">ie. native_ad_layout</span></strong>) that contains views for displaying a unified native ad and then locating a reference to the <strong><span style="color: #008000;">UnifiedNativeAdView</span></strong>.</p>
<pre>UnifiedNativeAdView unifiedNativeAdView = (UnifiedNativeAdView) getLayoutInflater()
.inflate(R.layout.native_ad_layout, null);</pre>
<h5 id="populate_and_register_the_asset_views"><strong><span style="color: #0000ff;">Populate and register the asset views</span></strong></h5>
<p>In <span class="s1"><strong><span style="color: #008000;">UnifiedNativeAdView</span></strong> class corresponding to each asset view, the setter getter method is available.</span></p>
<p>In the case of MediaView we will first locate the view and then registers it with the <span class="s1">UnifiedNativeAdView</span> object.</p>
<pre>MediaView mediaView = myAdView.findViewById(R.id.ad_media);
myAdView.setMediaView(mediaView);</pre>
<p>But In case of asset views other than MediaView(for example <span style="color: #008000;"><strong>body</strong></span> TextView):</p>
<ul>
<li>we will first locate the view.</li>
<li>Corresponding to the view we will call the setter method available inside the <span class="s1"><strong><span style="color: #008000;">UnifiedNativeAdView</span></strong> class to set the view.</span></li>
<li>Now we will check is <strong><span style="color: #008000;">UnifiedNativeAd</span></strong> consist of body content using <span style="color: #008000;"><strong>adFromGoogle.getBody()</strong></span>.</li>
<li>In case if it does not contain any body content then it will return null and then we will set its visibility to GONE. But In case it contains body content then we will set the data(from adFromGoogle) in<strong><span style="color: #008000;"> ad_body</span></strong> TextView.</li>
</ul>
<pre>myAdView.setBodyView(myAdView.findViewById(R.id.ad_body));

if (adFromGoogle.getBody() == null) {
 myAdView.getBodyView().setVisibility(View.GONE);
} else {
 ((TextView) myAdView.getBodyView()).setText(adFromGoogle.getBody());
}</pre>
<p>The same process should be repeated for each of the assets provided by the native ad object that the app will display.</p>
<pre>MediaView mediaView = myAdView.findViewById(R.id.ad_media);
 myAdView.setMediaView(mediaView);

 myAdView.setHeadlineView(myAdView.findViewById(R.id.ad_headline));
 myAdView.setBodyView(myAdView.findViewById(R.id.ad_body));
 myAdView.setCallToActionView(myAdView.findViewById(R.id.ad_call_to_action));
 myAdView.setIconView(myAdView.findViewById(R.id.ad_icon));
 myAdView.setPriceView(myAdView.findViewById(R.id.ad_price));
 myAdView.setStoreView(myAdView.findViewById(R.id.ad_store));
 myAdView.setStarRatingView(myAdView.findViewById(R.id.ad_rating));
 myAdView.setAdvertiserView(myAdView.findViewById(R.id.ad_advertiser));

 ((TextView) myAdView.getHeadlineView()).setText(adFromGoogle.getHeadline());

 if (adFromGoogle.getBody() == null) {
 myAdView.getBodyView().setVisibility(View.GONE);
 } else {
 ((TextView) myAdView.getBodyView()).setText(adFromGoogle.getBody());
 }

 if (adFromGoogle.getCallToAction() == null) {
 myAdView.getCallToActionView().setVisibility(View.GONE);
 } else {
 ((Button) myAdView.getCallToActionView()).setText(adFromGoogle.getCallToAction());
 }

 if (adFromGoogle.getIcon() == null) {
 myAdView.getIconView().setVisibility(View.GONE);
 } else {
 ((ImageView) myAdView.getIconView()).setImageDrawable(adFromGoogle.getIcon().getDrawable());
 }

 if (adFromGoogle.getPrice() == null) {
 myAdView.getPriceView().setVisibility(View.GONE);
 } else {
 ((TextView) myAdView.getPriceView()).setText(adFromGoogle.getPrice());
 }
 if (adFromGoogle.getStore() == null) {
 myAdView.getStoreView().setVisibility(View.GONE);
 } else {
 ((TextView) myAdView.getStoreView()).setText(adFromGoogle.getStore());
 }

 if (adFromGoogle.getStarRating() == null) {
 myAdView.getStarRatingView().setVisibility(View.GONE);
 } else {
 ((RatingBar) myAdView.getStarRatingView()).setRating(adFromGoogle.getStarRating().floatValue());
 }

 if (adFromGoogle.getAdvertiser() == null) {
 myAdView.getAdvertiserView().setVisibility(View.GONE);
 } else {
 ((TextView) myAdView.getAdvertiserView()).setText(adFromGoogle.getAdvertiser());
 }

 myAdView.setNativeAd(adFromGoogle);
}</pre>
<h5 id="register_the_native_ad_object"><span style="color: #0000ff;"><strong>Register the native ad object</strong></span></h5>
<p class="p1">This final step registers the native ad object with the view that&#8217;s responsible for displaying it:</p>
<pre>unifiedNativeAdView.setNativeAd(adFromGoogle);</pre>
<p>Corresponding to <span style="color: #008000;"><strong>NativeAdActivity.java</strong></span> we have <span style="color: #008000;"><strong>activity_native_ad.xml</strong></span> file which consists of one FrameLayout between two ImageViews as shown below:</p>
<p><span style="color: #0000ff;"><strong>activity_native_ad.xml</strong></span></p>
<pre><;?xml version="1.0" encoding="utf-8"?>;
<;ScrollView 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=".NativeAdActivity">;

 <;LinearLayout
 android:layout_width="match_parent"
 android:layout_height="wrap_content"
 android:orientation="vertical">;


 <;ImageView
 android:layout_width="match_parent"
 android:layout_height="200dp"
 android:scaleType="fitXY"
 android:src="@drawable/native_model" />;

 <;FrameLayout
 android:id="@+id/id_native_ad"
 android:layout_width="match_parent"
 android:layout_height="wrap_content">;

 <;/FrameLayout>;

 <;ImageView
 android:layout_width="match_parent"
 android:layout_height="200dp"
 android:scaleType="fitXY"
 android:src="@drawable/native_model" />;


 <;/LinearLayout>;

<;/ScrollView>;</pre>
<p>Now we will add the above <strong><span style="color: #008000;">unifiedNativeAdView</span></strong> object inside the FrameLayout.</p>
<pre>FrameLayout nativeAdLayout = findViewById(R.id.id_native_ad);
nativeAdLayout.removeAllViews();
nativeAdLayout.addView(unifiedNativeAdView);</pre>
<h4><span style="color: #000080;"><strong>Complete code:</strong></span></h4>
<p>Create another activity named <span style="color: #008000;"><b>NativeAdActivity.java</b></span> and add the below code.</p>
<p><span style="color: #0000ff;"><b>NativeAdActivity.java</b></span></p>
<pre>package com.example.androidadmobdemo;

import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.RatingBar;
import android.widget.TextView;
import android.widget.Toast;

import com.google.android.gms.ads.AdListener;
import com.google.android.gms.ads.AdLoader;
import com.google.android.gms.ads.AdRequest;
import com.google.android.gms.ads.formats.MediaView;
import com.google.android.gms.ads.formats.UnifiedNativeAd;
import com.google.android.gms.ads.formats.UnifiedNativeAdView;

import androidx.appcompat.app.AppCompatActivity;

public class NativeAdActivity extends AppCompatActivity {

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

 AdLoader adLoader = new AdLoader.Builder(this, "ca-app-pub-3940256099942544/2247696110")
 .forUnifiedNativeAd(new UnifiedNativeAd.OnUnifiedNativeAdLoadedListener() {
 @Override
 public void onUnifiedNativeAdLoaded(UnifiedNativeAd unifiedNativeAd) {
 <strong><span style="color: #008000;">//the native ad will be available inside this method (unifiedNativeAd)</span></strong>


 UnifiedNativeAdView unifiedNativeAdView = (UnifiedNativeAdView) getLayoutInflater().inflate(R.layout.native_ad_layout, null);

 mapUnifiedNativeAdToLayout(unifiedNativeAd, unifiedNativeAdView);

 FrameLayout nativeAdLayout = findViewById(R.id.id_native_ad);
 nativeAdLayout.removeAllViews();
 nativeAdLayout.addView(unifiedNativeAdView);

 }
 })
 .withAdListener(new AdListener() {
 <strong><span style="color: #008000;"> // Code to be executed when an ad request fails.</span></strong>
 @Override
 public void onAdFailedToLoad(int errorCode) {
 Toast.makeText(getApplicationContext(), "Ad failed to load! error code: " + errorCode, Toast.LENGTH_SHORT).show();
 }

 <strong><span style="color: #008000;"> //method is invoked when the rewarded ad is closed</span></strong>
<strong><span style="color: #008000;"> // due to the user tapping on the close icon or </span></strong>
<strong><span style="color: #008000;"> //using the back button.</span></strong>
 @Override
 public void onAdClosed() {
 Toast.makeText(getApplicationContext(), "Ad is closed!", Toast.LENGTH_SHORT).show();
 }

 <span style="color: #008000;"><strong> //called after onAdOpened(), when a user click opens another app,</strong></span>
<span style="color: #008000;"><strong> //backgrounding the current app</strong></span>
 @Override
 public void onAdLeftApplication() {
 Toast.makeText(getApplicationContext(), "Ad left application!", Toast.LENGTH_SHORT).show();
 }

 <span style="color: #008000;"><strong>//Code to be executed when an ad opens an overlay that</strong></span>
<span style="color: #008000;"><strong> // covers the screen.</strong></span>
 @Override
 public void onAdOpened() {
 Toast.makeText(getApplicationContext(), "Ad is opened!", Toast.LENGTH_SHORT).show();
 }

 <span style="color: #008000;"><strong>// Code to be executed when the user clicks on an ad.</strong></span>
 @Override
 public void onAdClicked() {
 Toast.makeText(getApplicationContext(), "Ad is clicked!", Toast.LENGTH_SHORT).show();

 }

 })
 .build();

 adLoader.loadAd(new AdRequest.Builder().build());
 }

 public void mapUnifiedNativeAdToLayout(UnifiedNativeAd adFromGoogle, UnifiedNativeAdView myAdView) {


 MediaView mediaView = myAdView.findViewById(R.id.ad_media);
 myAdView.setMediaView(mediaView);

 myAdView.setHeadlineView(myAdView.findViewById(R.id.ad_headline));
 myAdView.setBodyView(myAdView.findViewById(R.id.ad_body));
 myAdView.setCallToActionView(myAdView.findViewById(R.id.ad_call_to_action));
 myAdView.setIconView(myAdView.findViewById(R.id.ad_icon));
 myAdView.setPriceView(myAdView.findViewById(R.id.ad_price));
 myAdView.setStoreView(myAdView.findViewById(R.id.ad_store));
 myAdView.setStarRatingView(myAdView.findViewById(R.id.ad_rating));

 myAdView.setAdvertiserView(myAdView.findViewById(R.id.ad_advertiser));

 ((TextView) myAdView.getHeadlineView()).setText(adFromGoogle.getHeadline());

 if (adFromGoogle.getBody() == null) {
 myAdView.getBodyView().setVisibility(View.GONE);
 } else {
 ((TextView) myAdView.getBodyView()).setText(adFromGoogle.getBody());
 }

 if (adFromGoogle.getCallToAction() == null) {
 myAdView.getCallToActionView().setVisibility(View.GONE);
 } else {
 ((Button) myAdView.getCallToActionView()).setText(adFromGoogle.getCallToAction());
 }

 if (adFromGoogle.getIcon() == null) {
 myAdView.getIconView().setVisibility(View.GONE);
 } else {
 ((ImageView) myAdView.getIconView()).setImageDrawable(adFromGoogle.getIcon().getDrawable());
 }

 if (adFromGoogle.getPrice() == null) {
 myAdView.getPriceView().setVisibility(View.GONE);
 } else {
 ((TextView) myAdView.getPriceView()).setText(adFromGoogle.getPrice());
 }
 if (adFromGoogle.getStore() == null) {
 myAdView.getStoreView().setVisibility(View.GONE);
 } else {
 ((TextView) myAdView.getStoreView()).setText(adFromGoogle.getStore());
 }

 if (adFromGoogle.getStarRating() == null) {
 myAdView.getStarRatingView().setVisibility(View.GONE);
 } else {
 ((RatingBar) myAdView.getStarRatingView()).setRating(adFromGoogle.getStarRating().floatValue());
 }

 if (adFromGoogle.getAdvertiser() == null) {
 myAdView.getAdvertiserView().setVisibility(View.GONE);
 } else {
 ((TextView) myAdView.getAdvertiserView()).setText(adFromGoogle.getAdvertiser());
 }

 myAdView.setNativeAd(adFromGoogle);
 }

}</pre>
<p>Once loaded the ad will look something like this.</p>
<p><img class="alignnone wp-image-1502" src="https://c1ctech.com/wp-content/uploads/2020/01/Screenshot_1580382272.png" alt="Screenshot_1580382272" width="344" height="612" /></p>
<p>I hope this article will help you in understanding how to add RewardedVideo Ads and Native Ads in your application.

