Site icon C1CTech

Android how to Integrate Rewarded and Native Ads in your App(part II)

<p>In the previous article&comma; <strong><span style&equals;"color&colon; &num;008000&semi;"><a style&equals;"color&colon; &num;008000&semi;" href&equals;"https&colon;&sol;&sol;c1ctech&period;com&sol;android-how-to-integrate-google-admob-in-your-apppart-i&sol;">How to Integrate Banner and Interstitial Ads in your App&lpar;Part I&rpar;<&sol;a><&sol;span><&sol;strong>  we have talked about how to integrate 2 different types of ads in your application&comma; <strong><span style&equals;"color&colon; &num;0000ff&semi;">Banners ads<&sol;span> <&sol;strong>and <strong><span style&equals;"color&colon; &num;0000ff&semi;">Interstitial ads<&sol;span><&sol;strong>&period;<&sol;p>&NewLine;<p>Now In this article&comma; we will talk about how to integrate the other two ads in your application ie&period; <strong><span style&equals;"color&colon; &num;0000ff&semi;">Rewarded video ads<&sol;span><&sol;strong> and <span style&equals;"color&colon; &num;0000ff&semi;"><strong>Native Ads<&sol;strong><&sol;span> in brief&period;<&sol;p>&NewLine;<p><amp-youtube layout&equals;"responsive" width&equals;"1200" height&equals;"675" data-videoid&equals;"EMZDV9RApkU" title&equals;"Android Integrating AdMob"><a placeholder href&equals;"https&colon;&sol;&sol;youtu&period;be&sol;EMZDV9RApkU"><img src&equals;"https&colon;&sol;&sol;i&period;ytimg&period;com&sol;vi&sol;EMZDV9RApkU&sol;hqdefault&period;jpg" layout&equals;"fill" object-fit&equals;"cover" alt&equals;"Android Integrating AdMob"><&sol;a><&sol;amp-youtube><&sol;p>&NewLine;<h3><span style&equals;"color&colon; &num;000080&semi;"><strong>Creating Ad Units<&sol;strong><&sol;span><&sol;h3>&NewLine;<p class&equals;"p1"><b>1<&sol;b>&period; Sign into your <span style&equals;"color&colon; &num;008000&semi;"><strong><a style&equals;"color&colon; &num;008000&semi;" href&equals;"https&colon;&sol;&sol;apps&period;admob&period;com&sol;"><span class&equals;"s1">AdMob<&sol;span><&sol;a><&sol;strong><&sol;span> account&period;<&sol;p>&NewLine;<p class&equals;"p1"><strong>2 <&sol;strong>&period; From the left side click on <span style&equals;"color&colon; &num;008000&semi;"><strong>Apps<&sol;strong><&sol;span> and then select the newly created App&lpar;In my case <strong>AndroidAdMobDemo<&sol;strong>&rpar;&period;<&sol;p>&NewLine;<p class&equals;"p1"><strong>3<&sol;strong>&period; Inside your App name select <span style&equals;"color&colon; &num;008000&semi;"><strong>Ad units<&sol;strong><&sol;span> and click on <span style&equals;"color&colon; &num;008000&semi;"><b>ADD AD UNIT<&sol;b> <&sol;span>button to create a new ad unit&period;<&sol;p>&NewLine;<p><img class&equals;"alignnone size-full wp-image-1484" src&equals;"https&colon;&sol;&sol;c1ctech&period;com&sol;wp-content&sol;uploads&sol;2020&sol;01&sol;Screenshot-2020-01-24-13&period;17&period;28&period;png" alt&equals;"Screenshot 2020-01-24 13&period;17&period;28" width&equals;"2378" height&equals;"1082" &sol;><&sol;p>&NewLine;<p><b>4<&sol;b>&period; To create Rewarded Ad select <span style&equals;"color&colon; &num;0000ff&semi;"><strong>Rewarded<&sol;strong><&sol;span> ad format&period; Now give the ad unit a name and also enter the <span style&equals;"color&colon; &num;008000&semi;"><strong>reward amount<&sol;strong><&sol;span> and <strong><span style&equals;"color&colon; &num;008000&semi;">reward item<&sol;span><&sol;strong> to offer some reward points&sol;coins only if the user watches the ad video completely&period; Finally&comma; click on <span style&equals;"color&colon; &num;008000&semi;"><strong>CREATE AD UNIT <&sol;strong><span style&equals;"color&colon; &num;000000&semi;">button<&sol;span><strong>&period;<&sol;strong><&sol;span><&sol;p>&NewLine;<p><img class&equals;" wp-image-1506 aligncenter" src&equals;"https&colon;&sol;&sol;c1ctech&period;com&sol;wp-content&sol;uploads&sol;2020&sol;01&sol;Screenshot-2020-01-31-15&period;56&period;10&period;png" alt&equals;"Screenshot 2020-01-31 15&period;56&period;10" width&equals;"603" height&equals;"375" &sol;><&sol;p>&NewLine;<p>&nbsp&semi;<&sol;p>&NewLine;<p><b>5<&sol;b>&period; To create Native Ad select <span style&equals;"color&colon; &num;0000ff&semi;"><strong>Native<&sol;strong><&sol;span> ad format&period; Now give the ad unit a name and then click on <span style&equals;"color&colon; &num;008000&semi;"><strong>CREATE AD UNIT <&sol;strong><span style&equals;"color&colon; &num;000000&semi;">button<&sol;span><&sol;span>&period;<&sol;p>&NewLine;<p><img class&equals;" wp-image-1503 aligncenter" src&equals;"https&colon;&sol;&sol;c1ctech&period;com&sol;wp-content&sol;uploads&sol;2020&sol;01&sol;Screenshot-2020-01-29-19&period;51&period;51&period;png" alt&equals;"Screenshot 2020-01-29 19&period;51&period;51" width&equals;"612" height&equals;"259" &sol;><&sol;p>&NewLine;<p><strong>6<&sol;strong>&period; Once the ad unit is created&comma; you can notice the <span style&equals;"color&colon; &num;008000&semi;"><b>Ad unit ID<&sol;b><&sol;span> on the dashboard&period; An example of ad unit id looks like <b><span style&equals;"color&colon; &num;008000&semi;">ca-app-pub-962XXXXXXX&sol;XXXXXXXXXXX<&sol;span>&period;<&sol;b><&sol;p>&NewLine;<p>Now you are ready to use above generated ads in your application using its ad unit ID&period;<&sol;p>&NewLine;<p><span style&equals;"color&colon; &num;0000ff&semi;"><strong>Note&colon; Always use test Ad ID while you are testing your application&comma; you should never use original Ad ID in your application when in testing&sol;developing phase&period; If you do so you might get blocked from AdMob&period;<&sol;strong><&sol;span><&sol;p>&NewLine;<p>Now before adding any types of ads&comma; we have to first follow some steps to set up AdMob in our application&colon;<&sol;p>&NewLine;<ul>&NewLine;<li>Import the Mobile Ads SDK<&sol;li>&NewLine;<li>Update <span style&equals;"color&colon; &num;008000&semi;"><strong>AndroidManifest&period;xml<&sol;strong><&sol;span> with the AdMob<strong><span style&equals;"color&colon; &num;008000&semi;"> App ID<&sol;span><&sol;strong><&sol;li>&NewLine;<li>Initialize mobile ads SDK<&sol;li>&NewLine;<&sol;ul>&NewLine;<p>To understand the above three-step setup&comma; in brief&comma; refer my previous <strong><span style&equals;"color&colon; &num;0000ff&semi;"><a style&equals;"color&colon; &num;0000ff&semi;" href&equals;"https&colon;&sol;&sol;c1ctech&period;com&sol;android-how-to-integrate-google-admob-in-your-apppart-i&sol;">article<&sol;a><&sol;span><&sol;strong>&period;<&sol;p>&NewLine;<h3><span style&equals;"color&colon; &num;000080&semi;"><strong>Adding Rewarded Video Ad<&sol;strong><&sol;span><&sol;h3>&NewLine;<p><span style&equals;"color&colon; &num;008000&semi;"><strong><a style&equals;"color&colon; &num;008000&semi;" href&equals;"https&colon;&sol;&sol;developers&period;google&period;com&sol;admob&sol;android&sol;rewarded-video"><span class&equals;"s1">Rewarded<&sol;span><&sol;a><&sol;strong><&sol;span> Video Ads are fullscreen video ads which offers some reward points if the user watches the ad video&period; These ads are very useful to offer some reward points&sol;coins in video games&period;<&sol;p>&NewLine;<p class&equals;"p1">Now we will see how to implement video ads that can be used to give rewards to the user&period;<&sol;p>&NewLine;<p class&equals;"p1">Just as in interstitial ads&comma; we don’t need to add anything to our layout&comma; these ads can only be implemented programmatically&period;<&sol;p>&NewLine;<p class&equals;"p1">Create a global variable of <strong><span style&equals;"color&colon; &num;008000&semi;">RewardedVideoAd<&sol;span><&sol;strong><span style&equals;"color&colon; &num;008000&semi;"><span style&equals;"color&colon; &num;000000&semi;"> and then <&sol;span><&sol;span>initialize it in <strong><span style&equals;"color&colon; &num;008000&semi;">onCreate<&sol;span><&sol;strong> using <strong><span style&equals;"color&colon; &num;008000&semi;">MobileAds&period;getRewardedVideoAdInstance&lpar;this&rpar;<&sol;span>&period;<&sol;strong><&sol;p>&NewLine;<pre>public class RewardedVideoAdActivity extends AppCompatActivity &lbrace;&NewLine;&NewLine;<strong><span style&equals;"color&colon; &num;008000&semi;"> RewardedVideoAd mRewardedVideoAd&semi;<&sol;span><&sol;strong>&NewLine;&NewLine; &commat;Override&NewLine; protected void onCreate&lpar;Bundle savedInstanceState&rpar; &lbrace;&NewLine; super&period;onCreate&lpar;savedInstanceState&rpar;&semi;&NewLine; setContentView&lpar;R&period;layout&period;activity&lowbar;rewarded&lowbar;ad&rpar;&semi;&NewLine;&NewLine; <strong><span style&equals;"color&colon; &num;008000&semi;">mRewardedVideoAd &equals; MobileAds&period;getRewardedVideoAdInstance&lpar;this&rpar;&semi;&NewLine;<&sol;span><&sol;strong>&NewLine; &rcub;&NewLine;&rcub;<&sol;pre>&NewLine;<h4><span style&equals;"color&colon; &num;0000ff&semi;"><strong>Loading Ad<&sol;strong><&sol;span><&sol;h4>&NewLine;<p class&equals;"p1">To load <strong><span style&equals;"color&colon; &num;008000&semi;">RewardedVideoAd<&sol;span><&sol;strong> you need to call the <strong><span style&equals;"color&colon; &num;008000&semi;">loadAd&lpar;adUnitId&comma; AdRequest&rpar;<&sol;span><&sol;strong> on the ad object&period;<&sol;p>&NewLine;<pre>AdRequest adRequest &equals; new AdRequest&period;Builder&lpar;&rpar;&NewLine; &period;build&lpar;&rpar;&semi;&NewLine;&NewLine;<strong><span style&equals;"color&colon; &num;008000&semi;">mRewardedVideoAd&period;loadAd&lpar;"ca-app-pub-3940256099942544&sol;5224354917"&comma; adRequest&rpar;&semi;<&sol;span><&sol;strong><&sol;pre>&NewLine;<p class&equals;"p1">Google provides a testing id for rewarded video ads&comma; make sure to use exactly this ID when testing&sol;debugging your application&period;<&sol;p>&NewLine;<pre class&equals;"p1">ca-app-pub-3940256099942544&sol;5224354917<&sol;pre>&NewLine;<h4><strong><span style&equals;"color&colon; &num;0000ff&semi;">Adding Listener<&sol;span><&sol;strong><&sol;h4>&NewLine;<p class&equals;"p1">As with other types of ads you can listen to various ad events by setting a <span style&equals;"color&colon; &num;000000&semi;">RewardedVideoAdListener<&sol;span>  on <span style&equals;"color&colon; &num;008000&semi;"><strong>RewardedVideoAd<&sol;strong><&sol;span> object using <span style&equals;"color&colon; &num;008000&semi;"><strong>setRewardedVideoAdListener&lpar;RewardedVideoAdListener&rpar;&period;<&sol;strong><&sol;span><&sol;p>&NewLine;<pre>mRewardedVideoAd&period;setRewardedVideoAdListener&lpar;new RewardedVideoAdListener&lpar;&rpar; &lbrace;&NewLine;&NewLine; <span style&equals;"color&colon; &num;008000&semi;"><strong> &sol;&sol;This method is executed when an ad has finished loading<&sol;strong><&sol;span>&NewLine; &commat;Override&NewLine; public void onRewardedVideoAdLoaded&lpar;&rpar; &lbrace;&NewLine; Toast&period;makeText&lpar;RewardedVideoAdActivity&period;this&comma; "onRewardedVideoAdLoaded"&comma; Toast&period;LENGTH&lowbar;SHORT&rpar;&period;show&lpar;&rpar;&semi;&NewLine;&NewLine; showRewardedVideoAd&lpar;&rpar;&semi;&NewLine; &rcub;&NewLine;&NewLine; <strong><span style&equals;"color&colon; &num;008000&semi;"> &sol;&sol;method is invoked when the user should be rewarded for interacting with the ad<&sol;span><&sol;strong>&NewLine; &commat;Override&NewLine; public void onRewarded&lpar;RewardItem rewardItem&rpar; &lbrace;&NewLine; Toast&period;makeText&lpar;RewardedVideoAdActivity&period;this&comma; "onRewarded&excl; currency&colon; " &plus; rewardItem&period;getType&lpar;&rpar; &plus; " amount&colon; " &plus; rewardItem&period;getAmount&lpar;&rpar;&comma; Toast&period;LENGTH&lowbar;SHORT&rpar;&period;show&lpar;&rpar;&semi;&NewLine;&NewLine; &rcub;&NewLine;&NewLine; <strong><span style&equals;"color&colon; &num;008000&semi;">&sol;&sol;called after onAdOpened&lpar;&rpar;&comma; when a user click opens another app&comma;&NewLine; &sol;&sol;backgrounding the current app<&sol;span><&sol;strong>&NewLine; &commat;Override&NewLine; public void onRewardedVideoAdLeftApplication&lpar;&rpar; &lbrace;&NewLine; Toast&period;makeText&lpar;RewardedVideoAdActivity&period;this&comma; "onRewardedVideoAdLeftApplication"&comma;&NewLine; Toast&period;LENGTH&lowbar;SHORT&rpar;&period;show&lpar;&rpar;&semi;&NewLine; &rcub;&NewLine;&NewLine; <strong><span style&equals;"color&colon; &num;008000&semi;">&sol;&sol;method is invoked when the rewarded ad is closed&NewLine; &sol;&sol; due to the user tapping on the close icon or using the back button&period;<&sol;span><&sol;strong>&NewLine; &commat;Override&NewLine; public void onRewardedVideoAdClosed&lpar;&rpar; &lbrace;&NewLine; Toast&period;makeText&lpar;RewardedVideoAdActivity&period;this&comma; "onRewardedVideoAdClosed"&comma; Toast&period;LENGTH&lowbar;SHORT&rpar;&period;show&lpar;&rpar;&semi;&NewLine; &rcub;&NewLine;&NewLine; <strong><span style&equals;"color&colon; &num;008000&semi;">&sol;&sol;method is invoked when an ad fails to load<&sol;span><&sol;strong>&NewLine; &commat;Override&NewLine; public void onRewardedVideoAdFailedToLoad&lpar;int errorCode&rpar; &lbrace;&NewLine; Toast&period;makeText&lpar;RewardedVideoAdActivity&period;this&comma; "onRewardedVideoAdFailedToLoad" &plus; errorCode&comma; Toast&period;LENGTH&lowbar;SHORT&rpar;&period;show&lpar;&rpar;&semi;&NewLine; &rcub;&NewLine;&NewLine; <strong><span style&equals;"color&colon; &num;008000&semi;">&sol;&sol;method is invoked when the ad is displayed&comma; covering &NewLine; &sol;&sol;the device's screen&period;&NewLine; &commat;Override<&sol;span><&sol;strong>&NewLine; public void onRewardedVideoAdOpened&lpar;&rpar; &lbrace;&NewLine; Toast&period;makeText&lpar;RewardedVideoAdActivity&period;this&comma; "onRewardedVideoAdOpened"&comma; Toast&period;LENGTH&lowbar;SHORT&rpar;&period;show&lpar;&rpar;&semi;&NewLine; &rcub;&NewLine;&NewLine; <strong><span style&equals;"color&colon; &num;008000&semi;">&sol;&sol;method is invoked when rewarded video started<&sol;span><&sol;strong>&NewLine; &commat;Override&NewLine; public void onRewardedVideoStarted&lpar;&rpar; &lbrace;&NewLine; Toast&period;makeText&lpar;RewardedVideoAdActivity&period;this&comma; "onRewardedVideoStarted"&comma; Toast&period;LENGTH&lowbar;SHORT&rpar;&period;show&lpar;&rpar;&semi;&NewLine; &rcub;&NewLine;&NewLine; <strong><span style&equals;"color&colon; &num;008000&semi;">&sol;&sol;method is invoked when rewarded video completed<&sol;span><&sol;strong>&NewLine; &commat;Override&NewLine; public void onRewardedVideoCompleted&lpar;&rpar; &lbrace;&NewLine; Toast&period;makeText&lpar;RewardedVideoAdActivity&period;this&comma; "onRewardedVideoCompleted"&comma; Toast&period;LENGTH&lowbar;SHORT&rpar;&period;show&lpar;&rpar;&semi;&NewLine; &rcub;&NewLine;&rcub;&rpar;&semi;<&sol;pre>&NewLine;<p class&equals;"p1">Always check if the ad is loaded before showing the ad&period;<&sol;p>&NewLine;<pre>private void showRewardedVideoAd&lpar;&rpar; &lbrace;&NewLine; if &lpar;mRewardedVideoAd&period;isLoaded&lpar;&rpar;&rpar; &lbrace;&NewLine; mRewardedVideoAd&period;show&lpar;&rpar;&semi;&NewLine; &rcub;&NewLine;&rcub;<&sol;pre>&NewLine;<h4><span style&equals;"color&colon; &num;000080&semi;"><strong>Complete code&colon;<&sol;strong><&sol;span><&sol;h4>&NewLine;<p>Create a new activity and named it as <span style&equals;"color&colon; &num;008000&semi;"><b>RewardedVideoAdActivity&period;java<&sol;b><&sol;span> and add the below code&period;<&sol;p>&NewLine;<p><span style&equals;"color&colon; &num;0000ff&semi;"><b>RewardedVideoAdActivity&period;java<&sol;b><&sol;span><&sol;p>&NewLine;<pre>package com&period;example&period;androidadmobdemo&semi;&NewLine;&NewLine;import android&period;os&period;Bundle&semi;&NewLine;import android&period;widget&period;Toast&semi;&NewLine;&NewLine;import com&period;google&period;android&period;gms&period;ads&period;AdRequest&semi;&NewLine;import com&period;google&period;android&period;gms&period;ads&period;MobileAds&semi;&NewLine;import com&period;google&period;android&period;gms&period;ads&period;reward&period;RewardItem&semi;&NewLine;import com&period;google&period;android&period;gms&period;ads&period;reward&period;RewardedVideoAd&semi;&NewLine;import com&period;google&period;android&period;gms&period;ads&period;reward&period;RewardedVideoAdListener&semi;&NewLine;&NewLine;import androidx&period;appcompat&period;app&period;AppCompatActivity&semi;&NewLine;&NewLine;public class RewardedVideoAdActivity extends AppCompatActivity &lbrace;&NewLine;&NewLine; RewardedVideoAd mRewardedVideoAd&semi;&NewLine;&NewLine; &commat;Override&NewLine; protected void onCreate&lpar;Bundle savedInstanceState&rpar; &lbrace;&NewLine; super&period;onCreate&lpar;savedInstanceState&rpar;&semi;&NewLine; setContentView&lpar;R&period;layout&period;activity&lowbar;rewarded&lowbar;ad&rpar;&semi;&NewLine;&NewLine; mRewardedVideoAd &equals; MobileAds&period;getRewardedVideoAdInstance&lpar;this&rpar;&semi;&NewLine;&NewLine; AdRequest adRequest &equals; new AdRequest&period;Builder&lpar;&rpar;&period;build&lpar;&rpar;&semi;&NewLine;&NewLine; mRewardedVideoAd&period;loadAd&lpar;"ca-app-pub-3940256099942544&sol;5224354917"&comma; adRequest&rpar;&semi;&NewLine;&NewLine; mRewardedVideoAd&period;setRewardedVideoAdListener&lpar;new RewardedVideoAdListener&lpar;&rpar; &lbrace;&NewLine;&NewLine; <strong><span style&equals;"color&colon; &num;008000&semi;">&sol;&sol;This method is executed when an ad has finished loading<&sol;span><&sol;strong>&NewLine; &commat;Override&NewLine; public void onRewardedVideoAdLoaded&lpar;&rpar; &lbrace;&NewLine; Toast&period;makeText&lpar;RewardedVideoAdActivity&period;this&comma; "onRewardedVideoAdLoaded"&comma; Toast&period;LENGTH&lowbar;SHORT&rpar;&period;show&lpar;&rpar;&semi;&NewLine;&NewLine; showRewardedVideoAd&lpar;&rpar;&semi;&NewLine; &rcub;&NewLine;&NewLine; <span style&equals;"color&colon; &num;008000&semi;"><strong> &sol;&sol;method is invoked when the user should be rewarded <&sol;strong><&sol;span>&NewLine;<span style&equals;"color&colon; &num;008000&semi;"><strong> &sol;&sol;for interacting with the ad<&sol;strong><&sol;span>&NewLine; &commat;Override&NewLine; public void onRewarded&lpar;RewardItem rewardItem&rpar; &lbrace;&NewLine; Toast&period;makeText&lpar;RewardedVideoAdActivity&period;this&comma; "onRewarded&excl; currency&colon; " &plus; rewardItem&period;getType&lpar;&rpar; &plus; " amount&colon; " &plus; rewardItem&period;getAmount&lpar;&rpar;&comma; Toast&period;LENGTH&lowbar;SHORT&rpar;&period;show&lpar;&rpar;&semi;&NewLine;&NewLine; &rcub;&NewLine;&NewLine; <strong><span style&equals;"color&colon; &num;008000&semi;"> &sol;&sol;called after onAdOpened&lpar;&rpar;&comma; when a user click opens <&sol;span><&sol;strong>&NewLine;<strong><span style&equals;"color&colon; &num;008000&semi;"> &sol;&sol;another app&comma; backgrounding the current app<&sol;span><&sol;strong>&NewLine; &commat;Override&NewLine; public void onRewardedVideoAdLeftApplication&lpar;&rpar; &lbrace;&NewLine; Toast&period;makeText&lpar;RewardedVideoAdActivity&period;this&comma; "onRewardedVideoAdLeftApplication"&comma;&NewLine; Toast&period;LENGTH&lowbar;SHORT&rpar;&period;show&lpar;&rpar;&semi;&NewLine; &rcub;&NewLine;&NewLine; <strong><span style&equals;"color&colon; &num;008000&semi;">&sol;&sol;method is invoked when the rewarded ad is closed&NewLine; &sol;&sol; due to the user tapping on the close icon or <&sol;span><&sol;strong>&NewLine;<strong><span style&equals;"color&colon; &num;008000&semi;"> &sol;&sol;using the back button&period;<&sol;span><&sol;strong>&NewLine; &commat;Override&NewLine; public void onRewardedVideoAdClosed&lpar;&rpar; &lbrace;&NewLine; Toast&period;makeText&lpar;RewardedVideoAdActivity&period;this&comma; "onRewardedVideoAdClosed"&comma; Toast&period;LENGTH&lowbar;SHORT&rpar;&period;show&lpar;&rpar;&semi;&NewLine; &rcub;&NewLine;&NewLine; <strong><span style&equals;"color&colon; &num;008000&semi;">&sol;&sol;method is invoked when an ad fails to load<&sol;span><&sol;strong>&NewLine; &commat;Override&NewLine; public void onRewardedVideoAdFailedToLoad&lpar;int errorCode&rpar; &lbrace;&NewLine; Toast&period;makeText&lpar;RewardedVideoAdActivity&period;this&comma; "onRewardedVideoAdFailedToLoad" &plus; errorCode&comma; Toast&period;LENGTH&lowbar;SHORT&rpar;&period;show&lpar;&rpar;&semi;&NewLine; &rcub;&NewLine;&NewLine; <strong><span style&equals;"color&colon; &num;008000&semi;">&sol;&sol;method is invoked when the ad is displayed&comma; <&sol;span><&sol;strong>&NewLine;<strong><span style&equals;"color&colon; &num;008000&semi;"> &sol;&sol;covering the device's screen&period;<&sol;span><&sol;strong>&NewLine; &commat;Override&NewLine; public void onRewardedVideoAdOpened&lpar;&rpar; &lbrace;&NewLine; Toast&period;makeText&lpar;RewardedVideoAdActivity&period;this&comma; "onRewardedVideoAdOpened"&comma; Toast&period;LENGTH&lowbar;SHORT&rpar;&period;show&lpar;&rpar;&semi;&NewLine; &rcub;&NewLine;&NewLine; <strong><span style&equals;"color&colon; &num;008000&semi;">&sol;&sol;method is invoked when rewarded video started<&sol;span><&sol;strong>&NewLine; &commat;Override&NewLine; public void onRewardedVideoStarted&lpar;&rpar; &lbrace;&NewLine; Toast&period;makeText&lpar;RewardedVideoAdActivity&period;this&comma; "onRewardedVideoStarted"&comma; Toast&period;LENGTH&lowbar;SHORT&rpar;&period;show&lpar;&rpar;&semi;&NewLine; &rcub;&NewLine;&NewLine; <strong><span style&equals;"color&colon; &num;008000&semi;">&sol;&sol;method is invoked when rewarded video completed<&sol;span><&sol;strong>&NewLine; &commat;Override&NewLine; public void onRewardedVideoCompleted&lpar;&rpar; &lbrace;&NewLine; Toast&period;makeText&lpar;RewardedVideoAdActivity&period;this&comma; "onRewardedVideoCompleted"&comma; Toast&period;LENGTH&lowbar;SHORT&rpar;&period;show&lpar;&rpar;&semi;&NewLine; &rcub;&NewLine; &rcub;&rpar;&semi;&NewLine;&NewLine; &rcub;&NewLine;&NewLine; private void showRewardedVideoAd&lpar;&rpar; &lbrace;&NewLine; if &lpar;mRewardedVideoAd&period;isLoaded&lpar;&rpar;&rpar; &lbrace;&NewLine; mRewardedVideoAd&period;show&lpar;&rpar;&semi;&NewLine; &rcub;&NewLine; &rcub;&NewLine;&NewLine;&NewLine;&rcub;<&sol;pre>&NewLine;<p class&equals;"p1">Once loaded the ad will look something like this&period;<&sol;p>&NewLine;<p><img class&equals;"alignnone wp-image-1500" src&equals;"https&colon;&sol;&sol;c1ctech&period;com&sol;wp-content&sol;uploads&sol;2020&sol;01&sol;Screenshot&lowbar;1580381980&period;png" alt&equals;"Screenshot&lowbar;1580381980" width&equals;"344" height&equals;"612" &sol;><&sol;p>&NewLine;<h3><span style&equals;"color&colon; &num;000080&semi;"><strong>Adding Native Ad<&sol;strong><&sol;span><&sol;h3>&NewLine;<p><strong><a href&equals;"https&colon;&sol;&sol;developers&period;google&period;com&sol;admob&sol;android&sol;native&sol;start"><span style&equals;"color&colon; &num;008000&semi;">Native<&sol;span><span style&equals;"color&colon; &num;008000&semi;">Ads<&sol;span><&sol;a><&sol;strong>&comma; as the name suggests allow you to design an ad experience that feels like a natural part of your app&period; You can customize the look and feel of native ads the way you’d design your app content&period;<&sol;p>&NewLine;<p>Native Ads implementation can be divided into two tasks&colon;<&sol;p>&NewLine;<ul>&NewLine;<li>Receiving the ads from AdMob server &lpar;via the <strong><span style&equals;"color&colon; &num;008000&semi;">Google Mobile Ads SDK<&sol;span><&sol;strong>&rpar;&period;<&sol;li>&NewLine;<li>Displaying the ads received in the format we want&period;<&sol;li>&NewLine;<&sol;ul>&NewLine;<h4><span style&equals;"color&colon; &num;000080&semi;"><strong>Receiving Native Ads<&sol;strong><&sol;span><&sol;h4>&NewLine;<h5><span style&equals;"color&colon; &num;0000ff&semi;"><strong>Build an AdLoader<&sol;strong><&sol;span><&sol;h5>&NewLine;<p class&equals;"p1">Native ads are loaded using the <span style&equals;"color&colon; &num;0000ff&semi;"><strong><a style&equals;"color&colon; &num;0000ff&semi;" href&equals;"https&colon;&sol;&sol;developers&period;google&period;com&sol;android&sol;reference&sol;com&sol;google&sol;android&sol;gms&sol;ads&sol;AdLoader"><span class&equals;"s1">AdLoader<&sol;span><&sol;a><&sol;strong><&sol;span> class&period; The following code demonstrates how to build an <span class&equals;"s2">AdLoader<&sol;span> that can load unified native ads&colon;<&sol;p>&NewLine;<pre>AdLoader adLoader &equals; new AdLoader&period;Builder&lpar;this&comma; "ca-app-pub-3940256099942544&sol;2247696110"&rpar;&NewLine; &period;forUnifiedNativeAd&lpar;new UnifiedNativeAd&period;OnUnifiedNativeAdLoadedListener&lpar;&rpar; &lbrace;&NewLine; &commat;Override&NewLine; public void onUnifiedNativeAdLoaded&lpar;UnifiedNativeAd unifiedNativeAd&rpar; &lbrace;&NewLine; <strong><span style&equals;"color&colon; &num;008000&semi;"> &sol;&sol;the native ad will be available inside this method &lpar;unifiedNativeAd&rpar;<&sol;span><&sol;strong>&NewLine;&NewLine; &rcub;&NewLine; &rcub;&rpar;&period;build&semi;<&sol;pre>&NewLine;<p>Google provides a testing id for native ad&comma; make sure to use exactly this ID when testing&sol;debugging your application&period;<&sol;p>&NewLine;<pre>ca-app-pub-3940256099942544&sol;2247696110<&sol;pre>&NewLine;<p><span class&equals;"s1"><span style&equals;"color&colon; &num;0000ff&semi;"><a style&equals;"color&colon; &num;0000ff&semi;" href&equals;"https&colon;&sol;&sol;developers&period;google&period;com&sol;android&sol;reference&sol;com&sol;google&sol;android&sol;gms&sol;ads&sol;AdLoader&period;Builder&num;forUnifiedNativeAd&lpar;com&period;google&period;android&period;gms&period;ads&period;formats&period;UnifiedNativeAd&period;OnUnifiedNativeAdLoadedListener&rpar;"><strong>forUnifiedNativeAd&lpar;&rpar;<&sol;strong><&sol;a><&sol;span> used above<&sol;span> is responsible for preparing the <span class&equals;"s1">AdLoader<&sol;span> for the <strong><span style&equals;"color&colon; &num;008000&semi;">UnifiedNativeAd<&sol;span><&sol;strong> format&period;<&sol;p>&NewLine;<p class&equals;"p1">Calling this method configures the <span class&equals;"s1">AdLoader<&sol;span> to request unified native ads&period; When an ad has loaded successfully&comma; the listener object&&num;8217&semi;s <span style&equals;"color&colon; &num;008000&semi;"><strong><span class&equals;"s1">onUnifiedNativeAdLoaded&lpar;&rpar;<&sol;span><&sol;strong><&sol;span> method is called&period;<&sol;p>&NewLine;<h5><strong><span style&equals;"color&colon; &num;0000ff&semi;">Adding Listener with an AdLoader<&sol;span><&sol;strong><&sol;h5>&NewLine;<p class&equals;"p1">During creation of the <span class&equals;"s1">AdLoader<&sol;span>&comma; the <span style&equals;"color&colon; &num;008000&semi;"><strong><span class&equals;"s2">withAdListener<&sol;span><&sol;strong><&sol;span> function sets an <span style&equals;"color&colon; &num;008000&semi;"><strong><span class&equals;"s2">AdListener<&sol;span><&sol;strong><&sol;span>&period;<&sol;p>&NewLine;<p class&equals;"p1"><span style&equals;"color&colon; &num;008000&semi;"><strong><span class&equals;"s2">withAdListener<&sol;span><&sol;strong><&sol;span> method takes an <span class&equals;"s1">AdListener<&sol;span> as its lone parameter&comma; which receives callbacks from the <span class&equals;"s1">AdLoader<&sol;span> when ad lifecycle events take place&colon;<&sol;p>&NewLine;<pre>AdLoader adLoader &equals; new AdLoader&period;Builder&lpar;this&comma; "ca-app-pub-3940256099942544&sol;2247696110"&rpar;&NewLine; &period;forUnifiedNativeAd&lpar;new UnifiedNativeAd&period;OnUnifiedNativeAdLoadedListener&lpar;&rpar; &lbrace;&NewLine; &commat;Override&NewLine; public void onUnifiedNativeAdLoaded&lpar;UnifiedNativeAd unifiedNativeAd&rpar; &lbrace;&NewLine; <strong><span style&equals;"color&colon; &num;008000&semi;">&sol;&sol;the native ad will be available inside this method &lpar;unifiedNativeAd&rpar;<&sol;span><&sol;strong>&NewLine;&NewLine; &rcub;&NewLine; &rcub;&rpar;&NewLine; &period;withAdListener&lpar;new AdListener&lpar;&rpar; &lbrace;&NewLine; <strong><span style&equals;"color&colon; &num;008000&semi;">&sol;&sol; Code to be executed when an ad request fails&period;<&sol;span><&sol;strong>&NewLine; &commat;Override&NewLine; public void onAdFailedToLoad&lpar;int errorCode&rpar; &lbrace;&NewLine; Toast&period;makeText&lpar;getApplicationContext&lpar;&rpar;&comma; "Ad failed to load&excl; error code&colon; " &plus; errorCode&comma; Toast&period;LENGTH&lowbar;SHORT&rpar;&period;show&lpar;&rpar;&semi;&NewLine; &rcub;&NewLine;&NewLine; <strong><span style&equals;"color&colon; &num;008000&semi;"> &sol;&sol;method is invoked when the rewarded ad is closed<&sol;span><&sol;strong>&NewLine;<strong><span style&equals;"color&colon; &num;008000&semi;"> &sol;&sol;due to the user tapping on the close icon or <&sol;span><&sol;strong>&NewLine;<strong><span style&equals;"color&colon; &num;008000&semi;"> &sol;&sol;using the back button&period;<&sol;span><&sol;strong>&NewLine; &commat;Override&NewLine; public void onAdClosed&lpar;&rpar; &lbrace;&NewLine; Toast&period;makeText&lpar;getApplicationContext&lpar;&rpar;&comma; "Ad is closed&excl;"&comma; Toast&period;LENGTH&lowbar;SHORT&rpar;&period;show&lpar;&rpar;&semi;&NewLine; &rcub;&NewLine;&NewLine; <strong><span style&equals;"color&colon; &num;008000&semi;">&sol;&sol;called after onAdOpened&lpar;&rpar;&comma; when a user click opens another app&comma;<&sol;span><&sol;strong>&NewLine;<strong><span style&equals;"color&colon; &num;008000&semi;"> &sol;&sol;backgrounding the current app<&sol;span><&sol;strong>&NewLine; &commat;Override&NewLine; public void onAdLeftApplication&lpar;&rpar; &lbrace;&NewLine; Toast&period;makeText&lpar;getApplicationContext&lpar;&rpar;&comma; "Ad left application&excl;"&comma; Toast&period;LENGTH&lowbar;SHORT&rpar;&period;show&lpar;&rpar;&semi;&NewLine; &rcub;&NewLine;&NewLine; <strong><span style&equals;"color&colon; &num;008000&semi;">&sol;&sol;Code to be executed when an ad opens an overlay that<&sol;span><&sol;strong>&NewLine;<strong><span style&equals;"color&colon; &num;008000&semi;"> &sol;&sol; covers the screen&period;<&sol;span><&sol;strong>&NewLine; &commat;Override&NewLine; public void onAdOpened&lpar;&rpar; &lbrace;&NewLine; Toast&period;makeText&lpar;getApplicationContext&lpar;&rpar;&comma; "Ad is opened&excl;"&comma; Toast&period;LENGTH&lowbar;SHORT&rpar;&period;show&lpar;&rpar;&semi;&NewLine; &rcub;&NewLine;&NewLine; <span style&equals;"color&colon; &num;008000&semi;"><strong>&sol;&sol; Code to be executed when the user clicks on an ad&period;<&sol;strong><&sol;span>&NewLine; &commat;Override&NewLine; public void onAdClicked&lpar;&rpar; &lbrace;&NewLine; Toast&period;makeText&lpar;getApplicationContext&lpar;&rpar;&comma; "Ad is clicked&excl;"&comma; Toast&period;LENGTH&lowbar;SHORT&rpar;&period;show&lpar;&rpar;&semi;&NewLine;&NewLine; &rcub;&NewLine;&NewLine; &rcub;&rpar;&NewLine; &period;build&lpar;&rpar;&semi;<&sol;pre>&NewLine;<p class&equals;"p1">The one important difference between the way <span class&equals;"s1">AdListener<&sol;span> objects work with native ads and the way they work with banners and interstitials is that <span class&equals;"s1">AdLoader<&sol;span> has its own format-specific listeners<span style&equals;"color&colon; &num;008000&semi;"><strong> &lpar;i&period;e&period;&comma; <span class&equals;"s2">UnifiedNativeAd&period;OnUnifiedNativeAdLoadedListener<&sol;span>&rpar;<&sol;strong> <&sol;span>to use when an ad has loaded&comma; the<strong><span style&equals;"color&colon; &num;008000&semi;"> <&sol;span><&sol;strong><span class&equals;"s2"><strong><span style&equals;"color&colon; &num;008000&semi;">onAdLoaded&lpar;<&sol;span><&sol;strong>&rpar;<&sol;span> method from <span class&equals;"s1">AdListener<&sol;span> is not called when a native ad loads successfully&period;<&sol;p>&NewLine;<h5 class&equals;"p1"><span style&equals;"color&colon; &num;0000ff&semi;"><b>Loading ads<&sol;b><&sol;span><&sol;h5>&NewLine;<p class&equals;"p2">Once you&&num;8217&semi;ve finished building an <span class&equals;"s1">AdLoader<&sol;span>&comma; it&&num;8217&semi;s time to use it to load ads&period; There are two methods available for this&colon; <span class&equals;"s1">loadAd&lpar;&rpar;<&sol;span> and <span class&equals;"s1">loadAds&lpar;&rpar;<&sol;span>&period;<&sol;p>&NewLine;<p class&equals;"p2"><span class&equals;"s1"><strong><span style&equals;"color&colon; &num;0000ff&semi;">loadAd&lpar;&rpar;<&sol;span> <&sol;strong>&colon; <&sol;span>This method sends a request for a single ad&period;<&sol;p>&NewLine;<&excl;-- WP QUADS Content Ad Plugin v&period; 2&period;0&period;98&period;1 -->&NewLine;<div class&equals;"quads-location quads-ad2" id&equals;"quads-ad2" style&equals;"float&colon;none&semi;margin&colon;0px&semi;">&NewLine;&NewLine;<&sol;div>&NewLine;&NewLine;<pre>adLoader&period;loadAd&lpar;new AdRequest&period;Builder&lpar;&rpar;&period;build&lpar;&rpar;&rpar;&semi;<&sol;pre>&NewLine;<p><span class&equals;"s1"><strong><span style&equals;"color&colon; &num;0000ff&semi;">loadAds&lpar;&rpar;<&sol;span><&sol;strong> &colon; <&sol;span>This method sends a request for multiple ads&lpar;up to 5&rpar;&period;<&sol;p>&NewLine;<pre>adLoader&period;loadAds&lpar;new AdRequest&period;Builder&lpar;&rpar;&period;build&lpar;&rpar;&comma;4&rpar;&semi;<&sol;pre>&NewLine;<p class&equals;"p1">Both of these methods take an <strong><span style&equals;"color&colon; &num;008000&semi;"><a style&equals;"color&colon; &num;008000&semi;" href&equals;"https&colon;&sol;&sol;developers&period;google&period;com&sol;android&sol;reference&sol;com&sol;google&sol;android&sol;gms&sol;ads&sol;AdRequest"><span class&equals;"s1">AdRequest<&sol;span><&sol;a><&sol;span><&sol;strong> object as their first parameter but <strong><span class&equals;"s2" style&equals;"color&colon; &num;008000&semi;">loadAds&lpar;&rpar; <&sol;span><&sol;strong>takes an additional parameter ie&period; the number of ads the SDK should attempt to load for the request&period;<&sol;p>&NewLine;<h4><span style&equals;"color&colon; &num;000080&semi;"><strong>Displaying Native Ads<&sol;strong><&sol;span><&sol;h4>&NewLine;<p class&equals;"p1"><span style&equals;"color&colon; &num;0000ff&semi;"><span class&equals;"s1"><b>UnifiedNativeAdView<&sol;b><&sol;span><b> class<&sol;b><&sol;span><&sol;p>&NewLine;<p>To display native ads in our application which we get from AdMob server we make use of <span class&equals;"s2"><strong><span style&equals;"color&colon; &num;008000&semi;">UnifiedNativeAdView<&sol;span><&sol;strong> class which is corresponding to the <strong><span style&equals;"color&colon; &num;008000&semi;"><span class&equals;"s1">UnifiedNativeAd<&sol;span><&sol;span><&sol;strong> format&period;<&sol;span><&sol;p>&NewLine;<p class&equals;"p2">This class is a <strong><span style&equals;"color&colon; &num;008000&semi;"><span class&equals;"s2">ViewGroup<&sol;span><&sol;span><&sol;strong> that publishers should use as the root for the <span class&equals;"s1">UnifiedNativeAd<&sol;span>&period;<&sol;p>&NewLine;<p class&equals;"p2">A single <span style&equals;"color&colon; &num;008000&semi;"><strong><span class&equals;"s1">UnifiedNativeAdView<&sol;span><&sol;strong><&sol;span> corresponds to a single unified native ad&period; Each view used to display that ad&&num;8217&semi;s assets &lpar;<span class&equals;"s1">ImageView&comma; TextView&comma; MediaView&comma; etc<&sol;span>&rpar; should be a child of the <span class&equals;"s1">UnifiedNativeAdView<&sol;span> object&period;<&sol;p>&NewLine;<p class&equals;"p1">Create a new layout resource file and name it as <span style&equals;"color&colon; &num;008000&semi;"><strong>native&lowbar;ad&lowbar;layout&period;xml<&sol;strong><&sol;span>  which defines the view hierarchy for a unified native ad that uses a <span style&equals;"color&colon; &num;008000&semi;"><strong><span class&equals;"s1">LinearLayout<&sol;span><&sol;strong><&sol;span> to display its asset views might look like this&colon;<&sol;p>&NewLine;<p><span style&equals;"color&colon; &num;0000ff&semi;"><strong>native&lowbar;ad&lowbar;layout&period;xml<&sol;strong><&sol;span><&sol;p>&NewLine;<pre>&lt&semi;&quest;xml version&equals;"1&period;0" encoding&equals;"utf-8"&quest;&gt&semi;&NewLine;&lt&semi;com&period;google&period;android&period;gms&period;ads&period;formats&period;UnifiedNativeAdView&NewLine; xmlns&colon;android&equals;"http&colon;&sol;&sol;schemas&period;android&period;com&sol;apk&sol;res&sol;android" android&colon;layout&lowbar;width&equals;"match&lowbar;parent"&NewLine; android&colon;layout&lowbar;height&equals;"match&lowbar;parent"&gt&semi;&NewLine;&NewLine; &lt&semi;LinearLayout&NewLine; android&colon;layout&lowbar;width&equals;"match&lowbar;parent"&NewLine; android&colon;layout&lowbar;height&equals;"wrap&lowbar;content"&NewLine; android&colon;background&equals;"&num;000"&NewLine; android&colon;orientation&equals;"vertical"&gt&semi;&NewLine;&NewLine; &lt&semi;LinearLayout&NewLine; android&colon;layout&lowbar;width&equals;"match&lowbar;parent"&NewLine; android&colon;layout&lowbar;height&equals;"wrap&lowbar;content"&NewLine; android&colon;orientation&equals;"horizontal"&NewLine; android&colon;padding&equals;"10dp"&gt&semi;&NewLine;&NewLine; &lt&semi;ImageView&NewLine; android&colon;id&equals;"&commat;&plus;id&sol;ad&lowbar;icon"&NewLine; android&colon;layout&lowbar;width&equals;"40dp"&NewLine; android&colon;layout&lowbar;height&equals;"40dp"&NewLine; android&colon;layout&lowbar;marginRight&equals;"20dp"&NewLine; android&colon;background&equals;"&num;fff" &sol;&gt&semi;&NewLine;&NewLine; &lt&semi;LinearLayout&NewLine; android&colon;layout&lowbar;width&equals;"match&lowbar;parent"&NewLine; android&colon;layout&lowbar;height&equals;"wrap&lowbar;content"&NewLine; android&colon;orientation&equals;"vertical"&gt&semi;&NewLine;&NewLine; &lt&semi;TextView&NewLine; android&colon;id&equals;"&commat;&plus;id&sol;ad&lowbar;headline"&NewLine; android&colon;layout&lowbar;width&equals;"wrap&lowbar;content"&NewLine; android&colon;layout&lowbar;height&equals;"wrap&lowbar;content"&NewLine; android&colon;text&equals;"headline"&NewLine; android&colon;textColor&equals;"&num;fff" &sol;&gt&semi;&NewLine;&NewLine; &lt&semi;TextView&NewLine; android&colon;id&equals;"&commat;&plus;id&sol;ad&lowbar;advertiser"&NewLine; android&colon;layout&lowbar;width&equals;"wrap&lowbar;content"&NewLine; android&colon;layout&lowbar;height&equals;"wrap&lowbar;content"&NewLine; android&colon;text&equals;"advertiser"&NewLine; android&colon;textColor&equals;"&num;fff" &sol;&gt&semi;&NewLine;&NewLine; &lt&semi;RatingBar&NewLine; android&colon;id&equals;"&commat;&plus;id&sol;ad&lowbar;rating"&NewLine; style&equals;"&quest;android&colon;attr&sol;ratingBarStyleSmall"&NewLine; android&colon;layout&lowbar;width&equals;"wrap&lowbar;content"&NewLine; android&colon;layout&lowbar;height&equals;"wrap&lowbar;content"&NewLine; android&colon;isIndicator&equals;"true"&NewLine; android&colon;numStars&equals;"5"&NewLine; android&colon;stepSize&equals;"0&period;5" &sol;&gt&semi;&NewLine; &lt&semi;&sol;LinearLayout&gt&semi;&NewLine; &lt&semi;&sol;LinearLayout&gt&semi;&NewLine;&NewLine; <strong><span style&equals;"color&colon; &num;008000&semi;">&lt&semi;com&period;google&period;android&period;gms&period;ads&period;formats&period;MediaView<&sol;span><&sol;strong>&NewLine;<strong><span style&equals;"color&colon; &num;008000&semi;"> android&colon;id&equals;"&commat;&plus;id&sol;ad&lowbar;media"<&sol;span><&sol;strong>&NewLine;<strong><span style&equals;"color&colon; &num;008000&semi;"> android&colon;layout&lowbar;width&equals;"match&lowbar;parent"<&sol;span><&sol;strong>&NewLine;<strong><span style&equals;"color&colon; &num;008000&semi;"> android&colon;layout&lowbar;height&equals;"175dp"<&sol;span><&sol;strong>&NewLine;<strong><span style&equals;"color&colon; &num;008000&semi;"> android&colon;background&equals;"&num;fff"&gt&semi;<&sol;span><&sol;strong>&NewLine;&NewLine;<strong><span style&equals;"color&colon; &num;008000&semi;"> &lt&semi;&sol;com&period;google&period;android&period;gms&period;ads&period;formats&period;MediaView&gt&semi;<&sol;span><&sol;strong>&NewLine;&NewLine; &lt&semi;TextView&NewLine; android&colon;id&equals;"&commat;&plus;id&sol;ad&lowbar;body"&NewLine; android&colon;layout&lowbar;width&equals;"match&lowbar;parent"&NewLine; android&colon;layout&lowbar;height&equals;"match&lowbar;parent"&NewLine; android&colon;padding&equals;"10dp"&NewLine; android&colon;text&equals;"body of ad"&NewLine; android&colon;textColor&equals;"&num;fff" &sol;&gt&semi;&NewLine;&NewLine; &lt&semi;LinearLayout&NewLine; android&colon;layout&lowbar;width&equals;"match&lowbar;parent"&NewLine; android&colon;layout&lowbar;height&equals;"wrap&lowbar;content"&NewLine; android&colon;gravity&equals;"right"&NewLine; android&colon;padding&equals;"10dp"&gt&semi;&NewLine;&NewLine; &lt&semi;TextView&NewLine; android&colon;id&equals;"&commat;&plus;id&sol;ad&lowbar;store"&NewLine; android&colon;layout&lowbar;width&equals;"wrap&lowbar;content"&NewLine; android&colon;layout&lowbar;height&equals;"wrap&lowbar;content"&NewLine; android&colon;padding&equals;"10dp"&NewLine; android&colon;text&equals;"Store"&NewLine; android&colon;textColor&equals;"&num;fff" &sol;&gt&semi;&NewLine;&NewLine; &lt&semi;TextView&NewLine; android&colon;id&equals;"&commat;&plus;id&sol;ad&lowbar;price"&NewLine; android&colon;layout&lowbar;width&equals;"wrap&lowbar;content"&NewLine; android&colon;layout&lowbar;height&equals;"wrap&lowbar;content"&NewLine; android&colon;padding&equals;"10dp"&NewLine; android&colon;text&equals;"Price"&NewLine; android&colon;layout&lowbar;marginRight&equals;"8dp"&NewLine; android&colon;textColor&equals;"&num;fff" &sol;&gt&semi;&NewLine;&NewLine;&NewLine; &lt&semi;Button&NewLine; android&colon;id&equals;"&commat;&plus;id&sol;ad&lowbar;call&lowbar;to&lowbar;action"&NewLine; android&colon;layout&lowbar;width&equals;"wrap&lowbar;content"&NewLine; android&colon;layout&lowbar;height&equals;"wrap&lowbar;content"&NewLine; android&colon;background&equals;"&num;fff"&NewLine; android&colon;text&equals;"Action"&NewLine; android&colon;padding&equals;"10dp"&NewLine; android&colon;textColor&equals;"&num;000" &sol;&gt&semi;&NewLine; &lt&semi;&sol;LinearLayout&gt&semi;&NewLine; &lt&semi;&sol;LinearLayout&gt&semi;&NewLine;&NewLine;&NewLine;&lt&semi;&sol;com&period;google&period;android&period;gms&period;ads&period;formats&period;UnifiedNativeAdView&gt&semi;<&sol;pre>&NewLine;<p class&equals;"p1">In the above layout&comma; <span style&equals;"color&colon; &num;008000&semi;"><strong><span class&equals;"s1">MediaView<&sol;span><&sol;strong><&sol;span> is a special <span style&equals;"color&colon; &num;008000&semi;"><strong><span class&equals;"s1">View<&sol;span><&sol;strong><&sol;span> designed to display the main media asset&comma; either video or image&period; It should be placed within the view hierarchy of a <strong><span class&equals;"s1" style&equals;"color&colon; &num;008000&semi;">NativeAdView<&sol;span><&sol;strong>&comma; just like any other asset view&period;<&sol;p>&NewLine;<h4><span style&equals;"color&colon; &num;000080&semi;"><strong>Steps to display unified native ad<&sol;strong><&sol;span><&sol;h4>&NewLine;<p class&equals;"p1">These are the steps for displaying a unified native ad&colon;<&sol;p>&NewLine;<ol class&equals;"ol1">&NewLine;<li class&equals;"li1">Create an instance of the <span class&equals;"s1">UnifiedNativeAdView<&sol;span> class&period;<&sol;li>&NewLine;<li class&equals;"li1">For each ad asset to be displayed&colon;&NewLine;<ul class&equals;"ul1">&NewLine;<li class&equals;"li1">Populate the asset view with the asset in the ad object&period;<&sol;li>&NewLine;<li class&equals;"li1">Register the asset view with the <span class&equals;"s1">ViewGroup<&sol;span> class&period;<&sol;li>&NewLine;<&sol;ul>&NewLine;<&sol;li>&NewLine;<li class&equals;"li1">Register the ad object with the <span class&equals;"s1">ViewGroup<&sol;span> class&period;<&sol;li>&NewLine;<&sol;ol>&NewLine;<p>Let&&num;8217&semi;s take a look at the individual steps&colon;<&sol;p>&NewLine;<h5><strong><span style&equals;"color&colon; &num;0000ff&semi;">Inflate the Layout<&sol;span><&sol;strong><&sol;h5>&NewLine;<p>We&&num;8217&semi;re inflating an XML layout&lpar;<strong><span style&equals;"color&colon; &num;008000&semi;">ie&period; native&lowbar;ad&lowbar;layout<&sol;span><&sol;strong>&rpar; that contains views for displaying a unified native ad and then locating a reference to the <strong><span style&equals;"color&colon; &num;008000&semi;">UnifiedNativeAdView<&sol;span><&sol;strong>&period;<&sol;p>&NewLine;<pre>UnifiedNativeAdView unifiedNativeAdView &equals; &lpar;UnifiedNativeAdView&rpar; getLayoutInflater&lpar;&rpar;&NewLine;&period;inflate&lpar;R&period;layout&period;native&lowbar;ad&lowbar;layout&comma; null&rpar;&semi;<&sol;pre>&NewLine;<h5 id&equals;"populate&lowbar;and&lowbar;register&lowbar;the&lowbar;asset&lowbar;views"><strong><span style&equals;"color&colon; &num;0000ff&semi;">Populate and register the asset views<&sol;span><&sol;strong><&sol;h5>&NewLine;<p>In <span class&equals;"s1"><strong><span style&equals;"color&colon; &num;008000&semi;">UnifiedNativeAdView<&sol;span><&sol;strong> class corresponding to each asset view&comma; the setter getter method is available&period;<&sol;span><&sol;p>&NewLine;<p>In the case of MediaView we will first locate the view and then registers it with the <span class&equals;"s1">UnifiedNativeAdView<&sol;span> object&period;<&sol;p>&NewLine;<pre>MediaView mediaView &equals; myAdView&period;findViewById&lpar;R&period;id&period;ad&lowbar;media&rpar;&semi;&NewLine;myAdView&period;setMediaView&lpar;mediaView&rpar;&semi;<&sol;pre>&NewLine;<p>But In case of asset views other than MediaView&lpar;for example <span style&equals;"color&colon; &num;008000&semi;"><strong>body<&sol;strong><&sol;span> TextView&rpar;&colon;<&sol;p>&NewLine;<ul>&NewLine;<li>we will first locate the view&period;<&sol;li>&NewLine;<li>Corresponding to the view we will call the setter method available inside the <span class&equals;"s1"><strong><span style&equals;"color&colon; &num;008000&semi;">UnifiedNativeAdView<&sol;span><&sol;strong> class to set the view&period;<&sol;span><&sol;li>&NewLine;<li>Now we will check is <strong><span style&equals;"color&colon; &num;008000&semi;">UnifiedNativeAd<&sol;span><&sol;strong> consist of body content using <span style&equals;"color&colon; &num;008000&semi;"><strong>adFromGoogle&period;getBody&lpar;&rpar;<&sol;strong><&sol;span>&period;<&sol;li>&NewLine;<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&period; But In case it contains body content then we will set the data&lpar;from adFromGoogle&rpar; in<strong><span style&equals;"color&colon; &num;008000&semi;"> ad&lowbar;body<&sol;span><&sol;strong> TextView&period;<&sol;li>&NewLine;<&sol;ul>&NewLine;<pre>myAdView&period;setBodyView&lpar;myAdView&period;findViewById&lpar;R&period;id&period;ad&lowbar;body&rpar;&rpar;&semi;&NewLine;&NewLine;if &lpar;adFromGoogle&period;getBody&lpar;&rpar; &equals;&equals; null&rpar; &lbrace;&NewLine; myAdView&period;getBodyView&lpar;&rpar;&period;setVisibility&lpar;View&period;GONE&rpar;&semi;&NewLine;&rcub; else &lbrace;&NewLine; &lpar;&lpar;TextView&rpar; myAdView&period;getBodyView&lpar;&rpar;&rpar;&period;setText&lpar;adFromGoogle&period;getBody&lpar;&rpar;&rpar;&semi;&NewLine;&rcub;<&sol;pre>&NewLine;<p>The same process should be repeated for each of the assets provided by the native ad object that the app will display&period;<&sol;p>&NewLine;<pre>MediaView mediaView &equals; myAdView&period;findViewById&lpar;R&period;id&period;ad&lowbar;media&rpar;&semi;&NewLine; myAdView&period;setMediaView&lpar;mediaView&rpar;&semi;&NewLine;&NewLine; myAdView&period;setHeadlineView&lpar;myAdView&period;findViewById&lpar;R&period;id&period;ad&lowbar;headline&rpar;&rpar;&semi;&NewLine; myAdView&period;setBodyView&lpar;myAdView&period;findViewById&lpar;R&period;id&period;ad&lowbar;body&rpar;&rpar;&semi;&NewLine; myAdView&period;setCallToActionView&lpar;myAdView&period;findViewById&lpar;R&period;id&period;ad&lowbar;call&lowbar;to&lowbar;action&rpar;&rpar;&semi;&NewLine; myAdView&period;setIconView&lpar;myAdView&period;findViewById&lpar;R&period;id&period;ad&lowbar;icon&rpar;&rpar;&semi;&NewLine; myAdView&period;setPriceView&lpar;myAdView&period;findViewById&lpar;R&period;id&period;ad&lowbar;price&rpar;&rpar;&semi;&NewLine; myAdView&period;setStoreView&lpar;myAdView&period;findViewById&lpar;R&period;id&period;ad&lowbar;store&rpar;&rpar;&semi;&NewLine; myAdView&period;setStarRatingView&lpar;myAdView&period;findViewById&lpar;R&period;id&period;ad&lowbar;rating&rpar;&rpar;&semi;&NewLine; myAdView&period;setAdvertiserView&lpar;myAdView&period;findViewById&lpar;R&period;id&period;ad&lowbar;advertiser&rpar;&rpar;&semi;&NewLine;&NewLine; &lpar;&lpar;TextView&rpar; myAdView&period;getHeadlineView&lpar;&rpar;&rpar;&period;setText&lpar;adFromGoogle&period;getHeadline&lpar;&rpar;&rpar;&semi;&NewLine;&NewLine; if &lpar;adFromGoogle&period;getBody&lpar;&rpar; &equals;&equals; null&rpar; &lbrace;&NewLine; myAdView&period;getBodyView&lpar;&rpar;&period;setVisibility&lpar;View&period;GONE&rpar;&semi;&NewLine; &rcub; else &lbrace;&NewLine; &lpar;&lpar;TextView&rpar; myAdView&period;getBodyView&lpar;&rpar;&rpar;&period;setText&lpar;adFromGoogle&period;getBody&lpar;&rpar;&rpar;&semi;&NewLine; &rcub;&NewLine;&NewLine; if &lpar;adFromGoogle&period;getCallToAction&lpar;&rpar; &equals;&equals; null&rpar; &lbrace;&NewLine; myAdView&period;getCallToActionView&lpar;&rpar;&period;setVisibility&lpar;View&period;GONE&rpar;&semi;&NewLine; &rcub; else &lbrace;&NewLine; &lpar;&lpar;Button&rpar; myAdView&period;getCallToActionView&lpar;&rpar;&rpar;&period;setText&lpar;adFromGoogle&period;getCallToAction&lpar;&rpar;&rpar;&semi;&NewLine; &rcub;&NewLine;&NewLine; if &lpar;adFromGoogle&period;getIcon&lpar;&rpar; &equals;&equals; null&rpar; &lbrace;&NewLine; myAdView&period;getIconView&lpar;&rpar;&period;setVisibility&lpar;View&period;GONE&rpar;&semi;&NewLine; &rcub; else &lbrace;&NewLine; &lpar;&lpar;ImageView&rpar; myAdView&period;getIconView&lpar;&rpar;&rpar;&period;setImageDrawable&lpar;adFromGoogle&period;getIcon&lpar;&rpar;&period;getDrawable&lpar;&rpar;&rpar;&semi;&NewLine; &rcub;&NewLine;&NewLine; if &lpar;adFromGoogle&period;getPrice&lpar;&rpar; &equals;&equals; null&rpar; &lbrace;&NewLine; myAdView&period;getPriceView&lpar;&rpar;&period;setVisibility&lpar;View&period;GONE&rpar;&semi;&NewLine; &rcub; else &lbrace;&NewLine; &lpar;&lpar;TextView&rpar; myAdView&period;getPriceView&lpar;&rpar;&rpar;&period;setText&lpar;adFromGoogle&period;getPrice&lpar;&rpar;&rpar;&semi;&NewLine; &rcub;&NewLine; if &lpar;adFromGoogle&period;getStore&lpar;&rpar; &equals;&equals; null&rpar; &lbrace;&NewLine; myAdView&period;getStoreView&lpar;&rpar;&period;setVisibility&lpar;View&period;GONE&rpar;&semi;&NewLine; &rcub; else &lbrace;&NewLine; &lpar;&lpar;TextView&rpar; myAdView&period;getStoreView&lpar;&rpar;&rpar;&period;setText&lpar;adFromGoogle&period;getStore&lpar;&rpar;&rpar;&semi;&NewLine; &rcub;&NewLine;&NewLine; if &lpar;adFromGoogle&period;getStarRating&lpar;&rpar; &equals;&equals; null&rpar; &lbrace;&NewLine; myAdView&period;getStarRatingView&lpar;&rpar;&period;setVisibility&lpar;View&period;GONE&rpar;&semi;&NewLine; &rcub; else &lbrace;&NewLine; &lpar;&lpar;RatingBar&rpar; myAdView&period;getStarRatingView&lpar;&rpar;&rpar;&period;setRating&lpar;adFromGoogle&period;getStarRating&lpar;&rpar;&period;floatValue&lpar;&rpar;&rpar;&semi;&NewLine; &rcub;&NewLine;&NewLine; if &lpar;adFromGoogle&period;getAdvertiser&lpar;&rpar; &equals;&equals; null&rpar; &lbrace;&NewLine; myAdView&period;getAdvertiserView&lpar;&rpar;&period;setVisibility&lpar;View&period;GONE&rpar;&semi;&NewLine; &rcub; else &lbrace;&NewLine; &lpar;&lpar;TextView&rpar; myAdView&period;getAdvertiserView&lpar;&rpar;&rpar;&period;setText&lpar;adFromGoogle&period;getAdvertiser&lpar;&rpar;&rpar;&semi;&NewLine; &rcub;&NewLine;&NewLine; myAdView&period;setNativeAd&lpar;adFromGoogle&rpar;&semi;&NewLine;&rcub;<&sol;pre>&NewLine;<h5 id&equals;"register&lowbar;the&lowbar;native&lowbar;ad&lowbar;object"><span style&equals;"color&colon; &num;0000ff&semi;"><strong>Register the native ad object<&sol;strong><&sol;span><&sol;h5>&NewLine;<p class&equals;"p1">This final step registers the native ad object with the view that&&num;8217&semi;s responsible for displaying it&colon;<&sol;p>&NewLine;<pre>unifiedNativeAdView&period;setNativeAd&lpar;adFromGoogle&rpar;&semi;<&sol;pre>&NewLine;<p>Corresponding to <span style&equals;"color&colon; &num;008000&semi;"><strong>NativeAdActivity&period;java<&sol;strong><&sol;span> we have <span style&equals;"color&colon; &num;008000&semi;"><strong>activity&lowbar;native&lowbar;ad&period;xml<&sol;strong><&sol;span> file which consists of one FrameLayout between two ImageViews as shown below&colon;<&sol;p>&NewLine;<p><span style&equals;"color&colon; &num;0000ff&semi;"><strong>activity&lowbar;native&lowbar;ad&period;xml<&sol;strong><&sol;span><&sol;p>&NewLine;<pre>&lt&semi;&quest;xml version&equals;"1&period;0" encoding&equals;"utf-8"&quest;&gt&semi;&NewLine;&lt&semi;ScrollView xmlns&colon;android&equals;"http&colon;&sol;&sol;schemas&period;android&period;com&sol;apk&sol;res&sol;android"&NewLine; xmlns&colon;app&equals;"http&colon;&sol;&sol;schemas&period;android&period;com&sol;apk&sol;res-auto"&NewLine; xmlns&colon;tools&equals;"http&colon;&sol;&sol;schemas&period;android&period;com&sol;tools"&NewLine; android&colon;layout&lowbar;width&equals;"match&lowbar;parent"&NewLine; android&colon;layout&lowbar;height&equals;"match&lowbar;parent"&NewLine; tools&colon;context&equals;"&period;NativeAdActivity"&gt&semi;&NewLine;&NewLine; &lt&semi;LinearLayout&NewLine; android&colon;layout&lowbar;width&equals;"match&lowbar;parent"&NewLine; android&colon;layout&lowbar;height&equals;"wrap&lowbar;content"&NewLine; android&colon;orientation&equals;"vertical"&gt&semi;&NewLine;&NewLine;&NewLine; &lt&semi;ImageView&NewLine; android&colon;layout&lowbar;width&equals;"match&lowbar;parent"&NewLine; android&colon;layout&lowbar;height&equals;"200dp"&NewLine; android&colon;scaleType&equals;"fitXY"&NewLine; android&colon;src&equals;"&commat;drawable&sol;native&lowbar;model" &sol;&gt&semi;&NewLine;&NewLine; &lt&semi;FrameLayout&NewLine; android&colon;id&equals;"&commat;&plus;id&sol;id&lowbar;native&lowbar;ad"&NewLine; android&colon;layout&lowbar;width&equals;"match&lowbar;parent"&NewLine; android&colon;layout&lowbar;height&equals;"wrap&lowbar;content"&gt&semi;&NewLine;&NewLine; &lt&semi;&sol;FrameLayout&gt&semi;&NewLine;&NewLine; &lt&semi;ImageView&NewLine; android&colon;layout&lowbar;width&equals;"match&lowbar;parent"&NewLine; android&colon;layout&lowbar;height&equals;"200dp"&NewLine; android&colon;scaleType&equals;"fitXY"&NewLine; android&colon;src&equals;"&commat;drawable&sol;native&lowbar;model" &sol;&gt&semi;&NewLine;&NewLine;&NewLine; &lt&semi;&sol;LinearLayout&gt&semi;&NewLine;&NewLine;&lt&semi;&sol;ScrollView&gt&semi;<&sol;pre>&NewLine;<p>Now we will add the above <strong><span style&equals;"color&colon; &num;008000&semi;">unifiedNativeAdView<&sol;span><&sol;strong> object inside the FrameLayout&period;<&sol;p>&NewLine;<pre>FrameLayout nativeAdLayout &equals; findViewById&lpar;R&period;id&period;id&lowbar;native&lowbar;ad&rpar;&semi;&NewLine;nativeAdLayout&period;removeAllViews&lpar;&rpar;&semi;&NewLine;nativeAdLayout&period;addView&lpar;unifiedNativeAdView&rpar;&semi;<&sol;pre>&NewLine;<h4><span style&equals;"color&colon; &num;000080&semi;"><strong>Complete code&colon;<&sol;strong><&sol;span><&sol;h4>&NewLine;<p>Create another activity named <span style&equals;"color&colon; &num;008000&semi;"><b>NativeAdActivity&period;java<&sol;b><&sol;span> and add the below code&period;<&sol;p>&NewLine;<p><span style&equals;"color&colon; &num;0000ff&semi;"><b>NativeAdActivity&period;java<&sol;b><&sol;span><&sol;p>&NewLine;<pre>package com&period;example&period;androidadmobdemo&semi;&NewLine;&NewLine;import android&period;os&period;Bundle&semi;&NewLine;import android&period;view&period;View&semi;&NewLine;import android&period;widget&period;Button&semi;&NewLine;import android&period;widget&period;FrameLayout&semi;&NewLine;import android&period;widget&period;ImageView&semi;&NewLine;import android&period;widget&period;RatingBar&semi;&NewLine;import android&period;widget&period;TextView&semi;&NewLine;import android&period;widget&period;Toast&semi;&NewLine;&NewLine;import com&period;google&period;android&period;gms&period;ads&period;AdListener&semi;&NewLine;import com&period;google&period;android&period;gms&period;ads&period;AdLoader&semi;&NewLine;import com&period;google&period;android&period;gms&period;ads&period;AdRequest&semi;&NewLine;import com&period;google&period;android&period;gms&period;ads&period;formats&period;MediaView&semi;&NewLine;import com&period;google&period;android&period;gms&period;ads&period;formats&period;UnifiedNativeAd&semi;&NewLine;import com&period;google&period;android&period;gms&period;ads&period;formats&period;UnifiedNativeAdView&semi;&NewLine;&NewLine;import androidx&period;appcompat&period;app&period;AppCompatActivity&semi;&NewLine;&NewLine;public class NativeAdActivity extends AppCompatActivity &lbrace;&NewLine;&NewLine; &commat;Override&NewLine; protected void onCreate&lpar;Bundle savedInstanceState&rpar; &lbrace;&NewLine; super&period;onCreate&lpar;savedInstanceState&rpar;&semi;&NewLine; setContentView&lpar;R&period;layout&period;activity&lowbar;native&lowbar;ad&rpar;&semi;&NewLine;&NewLine; AdLoader adLoader &equals; new AdLoader&period;Builder&lpar;this&comma; "ca-app-pub-3940256099942544&sol;2247696110"&rpar;&NewLine; &period;forUnifiedNativeAd&lpar;new UnifiedNativeAd&period;OnUnifiedNativeAdLoadedListener&lpar;&rpar; &lbrace;&NewLine; &commat;Override&NewLine; public void onUnifiedNativeAdLoaded&lpar;UnifiedNativeAd unifiedNativeAd&rpar; &lbrace;&NewLine; <strong><span style&equals;"color&colon; &num;008000&semi;">&sol;&sol;the native ad will be available inside this method &lpar;unifiedNativeAd&rpar;<&sol;span><&sol;strong>&NewLine;&NewLine;&NewLine; UnifiedNativeAdView unifiedNativeAdView &equals; &lpar;UnifiedNativeAdView&rpar; getLayoutInflater&lpar;&rpar;&period;inflate&lpar;R&period;layout&period;native&lowbar;ad&lowbar;layout&comma; null&rpar;&semi;&NewLine;&NewLine; mapUnifiedNativeAdToLayout&lpar;unifiedNativeAd&comma; unifiedNativeAdView&rpar;&semi;&NewLine;&NewLine; FrameLayout nativeAdLayout &equals; findViewById&lpar;R&period;id&period;id&lowbar;native&lowbar;ad&rpar;&semi;&NewLine; nativeAdLayout&period;removeAllViews&lpar;&rpar;&semi;&NewLine; nativeAdLayout&period;addView&lpar;unifiedNativeAdView&rpar;&semi;&NewLine;&NewLine; &rcub;&NewLine; &rcub;&rpar;&NewLine; &period;withAdListener&lpar;new AdListener&lpar;&rpar; &lbrace;&NewLine; <strong><span style&equals;"color&colon; &num;008000&semi;"> &sol;&sol; Code to be executed when an ad request fails&period;<&sol;span><&sol;strong>&NewLine; &commat;Override&NewLine; public void onAdFailedToLoad&lpar;int errorCode&rpar; &lbrace;&NewLine; Toast&period;makeText&lpar;getApplicationContext&lpar;&rpar;&comma; "Ad failed to load&excl; error code&colon; " &plus; errorCode&comma; Toast&period;LENGTH&lowbar;SHORT&rpar;&period;show&lpar;&rpar;&semi;&NewLine; &rcub;&NewLine;&NewLine; <strong><span style&equals;"color&colon; &num;008000&semi;"> &sol;&sol;method is invoked when the rewarded ad is closed<&sol;span><&sol;strong>&NewLine;<strong><span style&equals;"color&colon; &num;008000&semi;"> &sol;&sol; due to the user tapping on the close icon or <&sol;span><&sol;strong>&NewLine;<strong><span style&equals;"color&colon; &num;008000&semi;"> &sol;&sol;using the back button&period;<&sol;span><&sol;strong>&NewLine; &commat;Override&NewLine; public void onAdClosed&lpar;&rpar; &lbrace;&NewLine; Toast&period;makeText&lpar;getApplicationContext&lpar;&rpar;&comma; "Ad is closed&excl;"&comma; Toast&period;LENGTH&lowbar;SHORT&rpar;&period;show&lpar;&rpar;&semi;&NewLine; &rcub;&NewLine;&NewLine; <span style&equals;"color&colon; &num;008000&semi;"><strong> &sol;&sol;called after onAdOpened&lpar;&rpar;&comma; when a user click opens another app&comma;<&sol;strong><&sol;span>&NewLine;<span style&equals;"color&colon; &num;008000&semi;"><strong> &sol;&sol;backgrounding the current app<&sol;strong><&sol;span>&NewLine; &commat;Override&NewLine; public void onAdLeftApplication&lpar;&rpar; &lbrace;&NewLine; Toast&period;makeText&lpar;getApplicationContext&lpar;&rpar;&comma; "Ad left application&excl;"&comma; Toast&period;LENGTH&lowbar;SHORT&rpar;&period;show&lpar;&rpar;&semi;&NewLine; &rcub;&NewLine;&NewLine; <span style&equals;"color&colon; &num;008000&semi;"><strong>&sol;&sol;Code to be executed when an ad opens an overlay that<&sol;strong><&sol;span>&NewLine;<span style&equals;"color&colon; &num;008000&semi;"><strong> &sol;&sol; covers the screen&period;<&sol;strong><&sol;span>&NewLine; &commat;Override&NewLine; public void onAdOpened&lpar;&rpar; &lbrace;&NewLine; Toast&period;makeText&lpar;getApplicationContext&lpar;&rpar;&comma; "Ad is opened&excl;"&comma; Toast&period;LENGTH&lowbar;SHORT&rpar;&period;show&lpar;&rpar;&semi;&NewLine; &rcub;&NewLine;&NewLine; <span style&equals;"color&colon; &num;008000&semi;"><strong>&sol;&sol; Code to be executed when the user clicks on an ad&period;<&sol;strong><&sol;span>&NewLine; &commat;Override&NewLine; public void onAdClicked&lpar;&rpar; &lbrace;&NewLine; Toast&period;makeText&lpar;getApplicationContext&lpar;&rpar;&comma; "Ad is clicked&excl;"&comma; Toast&period;LENGTH&lowbar;SHORT&rpar;&period;show&lpar;&rpar;&semi;&NewLine;&NewLine; &rcub;&NewLine;&NewLine; &rcub;&rpar;&NewLine; &period;build&lpar;&rpar;&semi;&NewLine;&NewLine; adLoader&period;loadAd&lpar;new AdRequest&period;Builder&lpar;&rpar;&period;build&lpar;&rpar;&rpar;&semi;&NewLine; &rcub;&NewLine;&NewLine; public void mapUnifiedNativeAdToLayout&lpar;UnifiedNativeAd adFromGoogle&comma; UnifiedNativeAdView myAdView&rpar; &lbrace;&NewLine;&NewLine;&NewLine; MediaView mediaView &equals; myAdView&period;findViewById&lpar;R&period;id&period;ad&lowbar;media&rpar;&semi;&NewLine; myAdView&period;setMediaView&lpar;mediaView&rpar;&semi;&NewLine;&NewLine; myAdView&period;setHeadlineView&lpar;myAdView&period;findViewById&lpar;R&period;id&period;ad&lowbar;headline&rpar;&rpar;&semi;&NewLine; myAdView&period;setBodyView&lpar;myAdView&period;findViewById&lpar;R&period;id&period;ad&lowbar;body&rpar;&rpar;&semi;&NewLine; myAdView&period;setCallToActionView&lpar;myAdView&period;findViewById&lpar;R&period;id&period;ad&lowbar;call&lowbar;to&lowbar;action&rpar;&rpar;&semi;&NewLine; myAdView&period;setIconView&lpar;myAdView&period;findViewById&lpar;R&period;id&period;ad&lowbar;icon&rpar;&rpar;&semi;&NewLine; myAdView&period;setPriceView&lpar;myAdView&period;findViewById&lpar;R&period;id&period;ad&lowbar;price&rpar;&rpar;&semi;&NewLine; myAdView&period;setStoreView&lpar;myAdView&period;findViewById&lpar;R&period;id&period;ad&lowbar;store&rpar;&rpar;&semi;&NewLine; myAdView&period;setStarRatingView&lpar;myAdView&period;findViewById&lpar;R&period;id&period;ad&lowbar;rating&rpar;&rpar;&semi;&NewLine;&NewLine; myAdView&period;setAdvertiserView&lpar;myAdView&period;findViewById&lpar;R&period;id&period;ad&lowbar;advertiser&rpar;&rpar;&semi;&NewLine;&NewLine; &lpar;&lpar;TextView&rpar; myAdView&period;getHeadlineView&lpar;&rpar;&rpar;&period;setText&lpar;adFromGoogle&period;getHeadline&lpar;&rpar;&rpar;&semi;&NewLine;&NewLine; if &lpar;adFromGoogle&period;getBody&lpar;&rpar; &equals;&equals; null&rpar; &lbrace;&NewLine; myAdView&period;getBodyView&lpar;&rpar;&period;setVisibility&lpar;View&period;GONE&rpar;&semi;&NewLine; &rcub; else &lbrace;&NewLine; &lpar;&lpar;TextView&rpar; myAdView&period;getBodyView&lpar;&rpar;&rpar;&period;setText&lpar;adFromGoogle&period;getBody&lpar;&rpar;&rpar;&semi;&NewLine; &rcub;&NewLine;&NewLine; if &lpar;adFromGoogle&period;getCallToAction&lpar;&rpar; &equals;&equals; null&rpar; &lbrace;&NewLine; myAdView&period;getCallToActionView&lpar;&rpar;&period;setVisibility&lpar;View&period;GONE&rpar;&semi;&NewLine; &rcub; else &lbrace;&NewLine; &lpar;&lpar;Button&rpar; myAdView&period;getCallToActionView&lpar;&rpar;&rpar;&period;setText&lpar;adFromGoogle&period;getCallToAction&lpar;&rpar;&rpar;&semi;&NewLine; &rcub;&NewLine;&NewLine; if &lpar;adFromGoogle&period;getIcon&lpar;&rpar; &equals;&equals; null&rpar; &lbrace;&NewLine; myAdView&period;getIconView&lpar;&rpar;&period;setVisibility&lpar;View&period;GONE&rpar;&semi;&NewLine; &rcub; else &lbrace;&NewLine; &lpar;&lpar;ImageView&rpar; myAdView&period;getIconView&lpar;&rpar;&rpar;&period;setImageDrawable&lpar;adFromGoogle&period;getIcon&lpar;&rpar;&period;getDrawable&lpar;&rpar;&rpar;&semi;&NewLine; &rcub;&NewLine;&NewLine; if &lpar;adFromGoogle&period;getPrice&lpar;&rpar; &equals;&equals; null&rpar; &lbrace;&NewLine; myAdView&period;getPriceView&lpar;&rpar;&period;setVisibility&lpar;View&period;GONE&rpar;&semi;&NewLine; &rcub; else &lbrace;&NewLine; &lpar;&lpar;TextView&rpar; myAdView&period;getPriceView&lpar;&rpar;&rpar;&period;setText&lpar;adFromGoogle&period;getPrice&lpar;&rpar;&rpar;&semi;&NewLine; &rcub;&NewLine; if &lpar;adFromGoogle&period;getStore&lpar;&rpar; &equals;&equals; null&rpar; &lbrace;&NewLine; myAdView&period;getStoreView&lpar;&rpar;&period;setVisibility&lpar;View&period;GONE&rpar;&semi;&NewLine; &rcub; else &lbrace;&NewLine; &lpar;&lpar;TextView&rpar; myAdView&period;getStoreView&lpar;&rpar;&rpar;&period;setText&lpar;adFromGoogle&period;getStore&lpar;&rpar;&rpar;&semi;&NewLine; &rcub;&NewLine;&NewLine; if &lpar;adFromGoogle&period;getStarRating&lpar;&rpar; &equals;&equals; null&rpar; &lbrace;&NewLine; myAdView&period;getStarRatingView&lpar;&rpar;&period;setVisibility&lpar;View&period;GONE&rpar;&semi;&NewLine; &rcub; else &lbrace;&NewLine; &lpar;&lpar;RatingBar&rpar; myAdView&period;getStarRatingView&lpar;&rpar;&rpar;&period;setRating&lpar;adFromGoogle&period;getStarRating&lpar;&rpar;&period;floatValue&lpar;&rpar;&rpar;&semi;&NewLine; &rcub;&NewLine;&NewLine; if &lpar;adFromGoogle&period;getAdvertiser&lpar;&rpar; &equals;&equals; null&rpar; &lbrace;&NewLine; myAdView&period;getAdvertiserView&lpar;&rpar;&period;setVisibility&lpar;View&period;GONE&rpar;&semi;&NewLine; &rcub; else &lbrace;&NewLine; &lpar;&lpar;TextView&rpar; myAdView&period;getAdvertiserView&lpar;&rpar;&rpar;&period;setText&lpar;adFromGoogle&period;getAdvertiser&lpar;&rpar;&rpar;&semi;&NewLine; &rcub;&NewLine;&NewLine; myAdView&period;setNativeAd&lpar;adFromGoogle&rpar;&semi;&NewLine; &rcub;&NewLine;&NewLine;&rcub;<&sol;pre>&NewLine;<p>Once loaded the ad will look something like this&period;<&sol;p>&NewLine;<p><img class&equals;"alignnone wp-image-1502" src&equals;"https&colon;&sol;&sol;c1ctech&period;com&sol;wp-content&sol;uploads&sol;2020&sol;01&sol;Screenshot&lowbar;1580382272&period;png" alt&equals;"Screenshot&lowbar;1580382272" width&equals;"344" height&equals;"612" &sol;><&sol;p>&NewLine;<p>I hope this article will help you in understanding how to add RewardedVideo Ads and Native Ads in your application&period;&NewLine;

Exit mobile version