#google #admob #native #android
Admob guide app: https://play.google.com/store/apps/de...
Native Ads [Part 5]
Learn how to earn money (monetize) from your android applications using google admob.
This video explains the implementation of Native Ad type for android applications.
Test Ad Unit ID for Interstitial Ads: ca-app-pub-3940256099942544/2247696110
Source code of application: https://github.com/shaheershukur/Admo...
Closed Captioning:
Native Ads are customizable ads which we can modify inorder to match with our application design.
Let's implement 'Native Ads' in our demo application.
For demo purpose, let's use black color and some images to demonstrate a dark theme based application.
In between these two items in our application, we will add a 'FrameLayout', which will be used to display the native ad.
As always, initialize the admob ads using 'MobileAds.initialize()' method.
Now for receiving ads from admob, we need to build an 'Adloader' that can load native ads.
The 'Adloader.Builder()' method takes context and our admob ad unit ID as parameters.
To generate the ad unit ID, open admob website, choose 'Native Ad' type.
Give an ad unit name and click on 'Add'.
From here, we can copy the ad unit ID and paste it in our code.
following which, we must call the 'forUnifiedNativeAd()' method,
following which we must call the 'build()' method.
The 'forUnifiedNativeAd()' method requires a 'OnUnifiedNativeAdLoadedListener()' as its parameter.
When the native ad is loaded, the 'OnUnifiedNativeAdLoaded()' method will be invoked.
Inside this method, later, we will be performing the second task.
Call the 'loadAd()' method on the 'adLoader' to start loading the ad from admob.
the variable 'unifiedNativeAd' contains the ad received from google admob.
The received ad must be mapped to our application as per our design.
For that, let's design a layout for displaying native ad.
Right click on 'Layout', New , Layout Resource File
Let's name the file 'native_ad_layout'.
The root element must be 'UnifiedNativeAdView'.
We can design the layout as required by our application.
A native ad will be having a media, headline, body, action text, icon, price, rating, store and advertiser.
So, we must create views for each of these in our design.
Also, we will add ids to each of these views.
Now in the java file, where the native ad is loaded, we will inflate the 'native_ad_layout' to a 'UnifiedNativeAdView' variable.
We will create a function 'mapUnifiedNativeAdToLayout' to map the native ad received to our native ad view.
Define the function like this.
The first argument is the native ad received from google admob, and the second argument is our ad view layout.
For all the elements we have created in the native ad layout, we must do the mapping.
First we will use the 'findViewById()' method to get the 'MediaView' and set the MediaView using 'setMediaView()' method.
In a similar way, we will use the 'findViewById()' method to get the view, and predefined setter methods to set corresponding elements to 'myAdView'.
Now we will assign the native ad data received to each element one by one.
We must typecast each element to its type. In this case, typecast to 'TextView'.
Every native ad will have a 'headline' resource for sure, hence we can set the headline text directly without checking if its available or not.
Use the predefined getter methods on the 'adFromGoogle' variable to get the data.
For all other remaining elements, we must check whether the data is available or not.
If not available, the getter method will return 'null'.
In that case, we can remove the element from 'native_ad_layout', by setting view as 'GONE' in 'setVisibility()' method.
Similarly, we will do the same for other native ad data.
At the end of the function, call 'setNativeAd()' method on 'myAdView' variable and pass 'adFromGoogle' as parameter to it.
Once the mapping is done, let's place the native ad in the desired place in our activity XML file.
We had created a 'FrameLayout' in the XML, where we need to place the ad.
using the 'findViewById()' method, create a reference variable to it in the java file.
Now call the 'removeAllViews()' method to remove any child view if present,
following which, call the 'addView()' method and pass the 'unifiedNativeAdView' variable to it.
before that, let's do a small change in the XML file.
We will change the 'RelativeLayout' to 'ScrollView' so that we can scroll through the screen once the ad is loaded.
One last thing, we can set ad listener to perform any task on ad events, if required.
for that, call the 'withAdListener()' method before the 'build()' method is called.
As a parameter, pass an instance of 'AdListener' class.
Inside that, press 'Alt' + 'Insert' to override the methods.
We can override these methods as required.
For example, if we need to perform some tasks when the user clicks in the ad, we can override 'onAdClicked()' method.