In the previous article, How to Integrate Banner and Interstitial Ads in your App(Part I) we have talked about how to integrate 2 different types of ads in your application, Banners ads and Interstitial ads.
Now In this article, we will talk about how to integrate the other two ads in your application ie. Rewarded video ads and Native Ads in brief.
Creating Ad Units
1. Sign into your AdMob account.
2 . From the left side click on Apps and then select the newly created App(In my case AndroidAdMobDemo).
3. Inside your App name select Ad units and click on ADD AD UNIT button to create a new ad unit.
4. To create Rewarded Ad select Rewarded ad format. Now give the ad unit a name and also enter the reward amount and reward item to offer some reward points/coins only if the user watches the ad video completely. Finally, click on CREATE AD UNIT button.
5. To create Native Ad select Native ad format. Now give the ad unit a name and then click on CREATE AD UNIT button.
6. Once the ad unit is created, you can notice the Ad unit ID on the dashboard. An example of ad unit id looks like ca-app-pub-962XXXXXXX/XXXXXXXXXXX.
Now you are ready to use above generated ads in your application using its ad unit ID.
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.
Now before adding any types of ads, we have to first follow some steps to set up AdMob in our application:
- Import the Mobile Ads SDK
- Update AndroidManifest.xml with the AdMob App ID
- Initialize mobile ads SDK
To understand the above three-step setup, in brief, refer my previous article.
Adding Rewarded Video Ad
Rewarded 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.
Now we will see how to implement video ads that can be used to give rewards to the user.
Just as in interstitial ads, we don’t need to add anything to our layout, these ads can only be implemented programmatically.
Create a global variable of RewardedVideoAd and then initialize it in onCreate using MobileAds.getRewardedVideoAdInstance(this).
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); } }
Loading Ad
To load RewardedVideoAd you need to call the loadAd(adUnitId, AdRequest) on the ad object.
AdRequest adRequest = new AdRequest.Builder()
.build();
mRewardedVideoAd.loadAd("ca-app-pub-3940256099942544/5224354917", adRequest);
Google provides a testing id for rewarded video ads, make sure to use exactly this ID when testing/debugging your application.
ca-app-pub-3940256099942544/5224354917
Adding Listener
As with other types of ads you can listen to various ad events by setting a RewardedVideoAdListener on RewardedVideoAd object using setRewardedVideoAdListener(RewardedVideoAdListener).
mRewardedVideoAd.setRewardedVideoAdListener(new RewardedVideoAdListener() { //This method is executed when an ad has finished loading @Override public void onRewardedVideoAdLoaded() { Toast.makeText(RewardedVideoAdActivity.this, "onRewardedVideoAdLoaded", Toast.LENGTH_SHORT).show(); showRewardedVideoAd(); } //method is invoked when the user should be rewarded for interacting with the ad @Override public void onRewarded(RewardItem rewardItem) { Toast.makeText(RewardedVideoAdActivity.this, "onRewarded! currency: " + rewardItem.getType() + " amount: " + rewardItem.getAmount(), Toast.LENGTH_SHORT).show(); } //called after onAdOpened(), when a user click opens another app, //backgrounding the current app @Override public void onRewardedVideoAdLeftApplication() { Toast.makeText(RewardedVideoAdActivity.this, "onRewardedVideoAdLeftApplication", Toast.LENGTH_SHORT).show(); } //method is invoked when the rewarded ad is closed // due to the user tapping on the close icon or using the back button. @Override public void onRewardedVideoAdClosed() { Toast.makeText(RewardedVideoAdActivity.this, "onRewardedVideoAdClosed", Toast.LENGTH_SHORT).show(); } //method is invoked when an ad fails to load @Override public void onRewardedVideoAdFailedToLoad(int errorCode) { Toast.makeText(RewardedVideoAdActivity.this, "onRewardedVideoAdFailedToLoad" + errorCode, Toast.LENGTH_SHORT).show(); } //method is invoked when the ad is displayed, covering //the device's screen. @Override public void onRewardedVideoAdOpened() { Toast.makeText(RewardedVideoAdActivity.this, "onRewardedVideoAdOpened", Toast.LENGTH_SHORT).show(); } //method is invoked when rewarded video started @Override public void onRewardedVideoStarted() { Toast.makeText(RewardedVideoAdActivity.this, "onRewardedVideoStarted", Toast.LENGTH_SHORT).show(); } //method is invoked when rewarded video completed @Override public void onRewardedVideoCompleted() { Toast.makeText(RewardedVideoAdActivity.this, "onRewardedVideoCompleted", Toast.LENGTH_SHORT).show(); } });
Always check if the ad is loaded before showing the ad.
private void showRewardedVideoAd() { if (mRewardedVideoAd.isLoaded()) { mRewardedVideoAd.show(); } }
Complete code:
Create a new activity and named it as RewardedVideoAdActivity.java and add the below code.
RewardedVideoAdActivity.java
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() { //This method is executed when an ad has finished loading @Override public void onRewardedVideoAdLoaded() { Toast.makeText(RewardedVideoAdActivity.this, "onRewardedVideoAdLoaded", Toast.LENGTH_SHORT).show(); showRewardedVideoAd(); } //method is invoked when the user should be rewarded //for interacting with the ad @Override public void onRewarded(RewardItem rewardItem) { Toast.makeText(RewardedVideoAdActivity.this, "onRewarded! currency: " + rewardItem.getType() + " amount: " + rewardItem.getAmount(), Toast.LENGTH_SHORT).show(); } //called after onAdOpened(), when a user click opens //another app, backgrounding the current app @Override public void onRewardedVideoAdLeftApplication() { Toast.makeText(RewardedVideoAdActivity.this, "onRewardedVideoAdLeftApplication", Toast.LENGTH_SHORT).show(); } //method is invoked when the rewarded ad is closed // due to the user tapping on the close icon or //using the back button. @Override public void onRewardedVideoAdClosed() { Toast.makeText(RewardedVideoAdActivity.this, "onRewardedVideoAdClosed", Toast.LENGTH_SHORT).show(); } //method is invoked when an ad fails to load @Override public void onRewardedVideoAdFailedToLoad(int errorCode) { Toast.makeText(RewardedVideoAdActivity.this, "onRewardedVideoAdFailedToLoad" + errorCode, Toast.LENGTH_SHORT).show(); } //method is invoked when the ad is displayed, //covering the device's screen. @Override public void onRewardedVideoAdOpened() { Toast.makeText(RewardedVideoAdActivity.this, "onRewardedVideoAdOpened", Toast.LENGTH_SHORT).show(); } //method is invoked when rewarded video started @Override public void onRewardedVideoStarted() { Toast.makeText(RewardedVideoAdActivity.this, "onRewardedVideoStarted", Toast.LENGTH_SHORT).show(); } //method is invoked when rewarded video completed @Override public void onRewardedVideoCompleted() { Toast.makeText(RewardedVideoAdActivity.this, "onRewardedVideoCompleted", Toast.LENGTH_SHORT).show(); } }); } private void showRewardedVideoAd() { if (mRewardedVideoAd.isLoaded()) { mRewardedVideoAd.show(); } } }
Once loaded the ad will look something like this.
Adding Native Ad
NativeAds, 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.
Native Ads implementation can be divided into two tasks:
- Receiving the ads from AdMob server (via the Google Mobile Ads SDK).
- Displaying the ads received in the format we want.
Receiving Native Ads
Build an AdLoader
Native ads are loaded using the AdLoader class. The following code demonstrates how to build an AdLoader that can load unified native ads:
AdLoader adLoader = new AdLoader.Builder(this, "ca-app-pub-3940256099942544/2247696110")
.forUnifiedNativeAd(new UnifiedNativeAd.OnUnifiedNativeAdLoadedListener() {
@Override
public void onUnifiedNativeAdLoaded(UnifiedNativeAd unifiedNativeAd) {
//the native ad will be available inside this method (unifiedNativeAd)
}
}).build;
Google provides a testing id for native ad, make sure to use exactly this ID when testing/debugging your application.
ca-app-pub-3940256099942544/2247696110
forUnifiedNativeAd() used above is responsible for preparing the AdLoader for the UnifiedNativeAd format.
Calling this method configures the AdLoader to request unified native ads. When an ad has loaded successfully, the listener object’s onUnifiedNativeAdLoaded() method is called.
Adding Listener with an AdLoader
During creation of the AdLoader, the withAdListener function sets an AdListener.
withAdListener method takes an AdListener as its lone parameter, which receives callbacks from the AdLoader when ad lifecycle events take place:
AdLoader adLoader = new AdLoader.Builder(this, "ca-app-pub-3940256099942544/2247696110") .forUnifiedNativeAd(new UnifiedNativeAd.OnUnifiedNativeAdLoadedListener() { @Override public void onUnifiedNativeAdLoaded(UnifiedNativeAd unifiedNativeAd) { //the native ad will be available inside this method (unifiedNativeAd) } }) .withAdListener(new AdListener() { // Code to be executed when an ad request fails. @Override public void onAdFailedToLoad(int errorCode) { Toast.makeText(getApplicationContext(), "Ad failed to load! error code: " + errorCode, Toast.LENGTH_SHORT).show(); } //method is invoked when the rewarded ad is closed //due to the user tapping on the close icon or //using the back button. @Override public void onAdClosed() { Toast.makeText(getApplicationContext(), "Ad is closed!", Toast.LENGTH_SHORT).show(); } //called after onAdOpened(), when a user click opens another app, //backgrounding the current app @Override public void onAdLeftApplication() { Toast.makeText(getApplicationContext(), "Ad left application!", Toast.LENGTH_SHORT).show(); } //Code to be executed when an ad opens an overlay that // covers the screen. @Override public void onAdOpened() { Toast.makeText(getApplicationContext(), "Ad is opened!", Toast.LENGTH_SHORT).show(); } // Code to be executed when the user clicks on an ad. @Override public void onAdClicked() { Toast.makeText(getApplicationContext(), "Ad is clicked!", Toast.LENGTH_SHORT).show(); } }) .build();
The one important difference between the way AdListener objects work with native ads and the way they work with banners and interstitials is that AdLoader has its own format-specific listeners (i.e., UnifiedNativeAd.OnUnifiedNativeAdLoadedListener) to use when an ad has loaded, the onAdLoaded() method from AdListener is not called when a native ad loads successfully.
Loading ads
Once you’ve finished building an AdLoader, it’s time to use it to load ads. There are two methods available for this: loadAd() and loadAds().
loadAd() : This method sends a request for a single ad.
adLoader.loadAd(new AdRequest.Builder().build());
loadAds() : This method sends a request for multiple ads(up to 5).
adLoader.loadAds(new AdRequest.Builder().build(),4);
Both of these methods take an AdRequest object as their first parameter but loadAds() takes an additional parameter ie. the number of ads the SDK should attempt to load for the request.
Displaying Native Ads
UnifiedNativeAdView class
To display native ads in our application which we get from AdMob server we make use of UnifiedNativeAdView class which is corresponding to the UnifiedNativeAd format.
This class is a ViewGroup that publishers should use as the root for the UnifiedNativeAd.
A single UnifiedNativeAdView corresponds to a single unified native ad. Each view used to display that ad’s assets (ImageView, TextView, MediaView, etc) should be a child of the UnifiedNativeAdView object.
Create a new layout resource file and name it as native_ad_layout.xml which defines the view hierarchy for a unified native ad that uses a LinearLayout to display its asset views might look like this:
native_ad_layout.xml
<?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> <com.google.android.gms.ads.formats.MediaView android:id="@+id/ad_media" android:layout_width="match_parent" android:layout_height="175dp" android:background="#fff"> </com.google.android.gms.ads.formats.MediaView> <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>
In the above layout, MediaView is a special View designed to display the main media asset, either video or image. It should be placed within the view hierarchy of a NativeAdView, just like any other asset view.
Steps to display unified native ad
These are the steps for displaying a unified native ad:
- Create an instance of the UnifiedNativeAdView class.
- For each ad asset to be displayed:
- Populate the asset view with the asset in the ad object.
- Register the asset view with the ViewGroup class.
- Register the ad object with the ViewGroup class.
Let’s take a look at the individual steps:
Inflate the Layout
We’re inflating an XML layout(ie. native_ad_layout) that contains views for displaying a unified native ad and then locating a reference to the UnifiedNativeAdView.
UnifiedNativeAdView unifiedNativeAdView = (UnifiedNativeAdView) getLayoutInflater() .inflate(R.layout.native_ad_layout, null);
Populate and register the asset views
In UnifiedNativeAdView class corresponding to each asset view, the setter getter method is available.
In the case of MediaView we will first locate the view and then registers it with the UnifiedNativeAdView object.
MediaView mediaView = myAdView.findViewById(R.id.ad_media); myAdView.setMediaView(mediaView);
But In case of asset views other than MediaView(for example body TextView):
- we will first locate the view.
- Corresponding to the view we will call the setter method available inside the UnifiedNativeAdView class to set the view.
- Now we will check is UnifiedNativeAd consist of body content using adFromGoogle.getBody().
- 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 ad_body TextView.
myAdView.setBodyView(myAdView.findViewById(R.id.ad_body)); if (adFromGoogle.getBody() == null) { myAdView.getBodyView().setVisibility(View.GONE); } else { ((TextView) myAdView.getBodyView()).setText(adFromGoogle.getBody()); }
The same process should be repeated for each of the assets provided by the native ad object that the app will display.
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); }
Register the native ad object
This final step registers the native ad object with the view that’s responsible for displaying it:
unifiedNativeAdView.setNativeAd(adFromGoogle);
Corresponding to NativeAdActivity.java we have activity_native_ad.xml file which consists of one FrameLayout between two ImageViews as shown below:
activity_native_ad.xml
<?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>
Now we will add the above unifiedNativeAdView object inside the FrameLayout.
FrameLayout nativeAdLayout = findViewById(R.id.id_native_ad); nativeAdLayout.removeAllViews(); nativeAdLayout.addView(unifiedNativeAdView);
Complete code:
Create another activity named NativeAdActivity.java and add the below code.
NativeAdActivity.java
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) { //the native ad will be available inside this method (unifiedNativeAd) 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() { // Code to be executed when an ad request fails. @Override public void onAdFailedToLoad(int errorCode) { Toast.makeText(getApplicationContext(), "Ad failed to load! error code: " + errorCode, Toast.LENGTH_SHORT).show(); } //method is invoked when the rewarded ad is closed // due to the user tapping on the close icon or //using the back button. @Override public void onAdClosed() { Toast.makeText(getApplicationContext(), "Ad is closed!", Toast.LENGTH_SHORT).show(); } //called after onAdOpened(), when a user click opens another app, //backgrounding the current app @Override public void onAdLeftApplication() { Toast.makeText(getApplicationContext(), "Ad left application!", Toast.LENGTH_SHORT).show(); } //Code to be executed when an ad opens an overlay that // covers the screen. @Override public void onAdOpened() { Toast.makeText(getApplicationContext(), "Ad is opened!", Toast.LENGTH_SHORT).show(); } // Code to be executed when the user clicks on an ad. @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); } }
Once loaded the ad will look something like this.
I hope this article will help you in understanding how to add RewardedVideo Ads and Native Ads in your application.