How to implement Native Ads without MediaView - android

I want to implement Native Ads in RecyclerView without MediaView, So referred to this tutorial after following this I successfully implemented native ads in RecyclerView but my need is to load only icon, heading, advertiser and call to action button not the MediaView, etc.
So I removed the MediView and other elements Code from the implementation which I made after which the code looks like this
UnifiedNativeAdViewHolder.java
package com.mishracodes.myapplication;
import android.view.View;
import androidx.recyclerview.widget.RecyclerView;
import com.google.android.gms.ads.formats.UnifiedNativeAdView;
public class UnifiedNativeAdViewHolder extends RecyclerView.ViewHolder {
private UnifiedNativeAdView adView;
public UnifiedNativeAdView getAdView() {
return adView;
}
UnifiedNativeAdViewHolder(View view) {
super(view);
adView = view.findViewById(R.id.ad_view);
adView.setHeadlineView(adView.findViewById(R.id.ad_headline));
adView.setCallToActionView(adView.findViewById(R.id.ad_call_to_action));
adView.setIconView(adView.findViewById(R.id.ad_icon));
adView.setAdvertiserView(adView.findViewById(R.id.ad_advertiser));
}
}
and inside the RecyclerViewAdapter.java the part of code which manages NativeAdView
private void populateNativeAdView(UnifiedNativeAd nativeAd,
UnifiedNativeAdView adView) {
((TextView) adView.getHeadlineView()).setText(nativeAd.getHeadline());
((Button) adView.getCallToActionView()).setText(nativeAd.getCallToAction());
NativeAd.Image icon = nativeAd.getIcon();
if (icon == null) {
adView.getIconView().setVisibility(View.INVISIBLE);
} else {
((ImageView) adView.getIconView()).setImageDrawable(icon.getDrawable());
adView.getIconView().setVisibility(View.VISIBLE);
}
if (nativeAd.getAdvertiser() == null) {
adView.getAdvertiserView().setVisibility(View.INVISIBLE);
} else {
((TextView) adView.getAdvertiserView()).setText(nativeAd.getAdvertiser());
adView.getAdvertiserView().setVisibility(View.VISIBLE);
}
adView.setNativeAd(nativeAd);
}
loading Native Ad function in MainActivity.java
private void loadNativeAds() {
AdLoader.Builder builder = new AdLoader.Builder(this, getString(R.string.ad_unit_id));
adLoader = builder.forUnifiedNativeAd(
new UnifiedNativeAd.OnUnifiedNativeAdLoadedListener() {
#Override
public void onUnifiedNativeAdLoaded(UnifiedNativeAd unifiedNativeAd) {
mNativeAds.add(unifiedNativeAd);
if (!adLoader.isLoading()) {
insertAdsInMenuItems();
}
}
}).withAdListener(
new AdListener() {
#Override
public void onAdFailedToLoad(int errorCode) {
Log.d("MainActivity", "The previous native ad failed to load. Attempting to"
+ " load another.");
if (!adLoader.isLoading()) {
insertAdsInMenuItems();
}
}
}).build();
adLoader.loadAds(new AdRequest.Builder().build(), NUMBER_OF_ADS);
}
Now after doing this I used Native Ad Validator to validate if the ads are implemented correctly
Result:
But After testing it for sometime I got this error
Warning in detail
So Is there any way to Call for Loading Native Ad so that only those ads are called which do not have Media View. As I have Seen Many Apps which uses native ads like this without Media View.

We can but it was possible before as Google made it mandatory to add MediaView. Also it's a good idea as your revenue would be increased but it may affect the user experience. Finally, you can't do this because if you upload it to the play store then Google will send you a warning notice saying that you didn't add MediaView. It may affect your earnings.

I'm too late, but it helps someone. I found a way to implement native ads without that MediaView.
The logic is Google has provided some native templates. These are for beginners who want to get started with native ads without implementing them from scratch. There are two templates - gnt_small_template_view and gnt_medium_template_view. gnt_small_template_view doesn't have MediaView. You can happily implement it in recycler view and also you can change the layout as per your requirement (but don't change the view ids). The original layout doesn't look good. You mostly change it.
Here is the documentation.
I have added these templates to my app, I didn't face any issue.

Related

Xamarin, Android: Using the AdListener right

I'm trying to use the AdListener in C#.
I have an interstitial ad loading when the app is first started, but sometimes my ad gets skipped because it isn't fully loaded yet. I think an Adlistener should do the trick.
Unfortunately, I have NO CLUE on how to implement it. Also, there is no tutorial on how to do it in C# only in Java and I couldn't find a translation for it :(
Add:
adListener.OnAdLoaded() += (o, e) =>
{
mInterstitialAd.Show();
};
This doesn't work :(
Any help would be awesome!
You can create a class which inherits from Android.Gms.Ads.AdListener, then use the instance of this class as the Listener for your mInterstitialAd, for example:
mInterstitialAd.AdListener = new AdListener(this);
AdListener:
private class AdListener : Android.Gms.Ads.AdListener
{
private MainActivity that;
public AdListener(MainActivity t)
{
that = t;
}
public override void OnAdLoaded()
{
base.OnAdLoaded();
}
public override void OnAdClosed()
{
that.RequestNewInterstitial();
that.BeginSecondActivity();
}
}
You can also checked the official demo for xamarin android ad: AdMobExample Sample.

Android AdView not showing

I'm having issues with ADs in my Android App.
Sometimes the AD is not shown, probably depending on the DPI or Size of the screen's device.
In my code, I implemented a check to set View.GONE in case the AD fails to load, but despite that on some devices I still have blank spaces where it's supposed to be the AD
For example, here one of my ADs
if(holder.nativeExpressAdView != null) {
AdRequest request = new AdRequest.Builder()
.build();
holder.nativeExpressAdView.loadAd(request);
Utilities.setAdListenerNative(holder.nativeExpressAdView);
}
//ADs listener to remove view if it fails load
//NATIVE ADS
public static void setAdListenerNative(final NativeExpressAdView nativeExpressAdView) {
nativeExpressAdView.setAdListener(new AdListener() {
#Override
public void onAdFailedToLoad(int i) {
super.onAdFailedToLoad(i);
nativeExpressAdView.setVisibility(View.GONE);
}
});
}
First show me your log
Second Dont use "Gone" because this will change the position of the adView if you are using other sizes of Android scale you will see that.
Third use a debugger.
Use "invisible"

Implementing native ads in android using Admob? is it possible?

I'm trying to implement native ads in my android application. But I want to do it using admob only. I searched a lot for solution but could not find exact one to do so.
I know it is possible using MoPub.
What I wanted to do is this:
Show ads inside list item which means one of the ListView/RecyclerView item can be one ad like below image.
I found some links and references but that doesn't explain proper implementation of the native ads.
Link 1 : Native ads overview
Link 2 : DFP Android Guides > Targeting
Link 3 : DFP Quick Start Guide
If it is not possible to do it using admob, MoPub is best solution for me right now.
Any help and guidance would be helpful. thanks.
Recently I stucked with the same question. Then I decided to post my solution for that to admobadapter. Hope it will help you.
The basic usage could look like:
ListView lvMessages;
AdmobAdapterWrapper adapterWrapper;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
initListViewItems();
}
/**
* Inits an adapter with items, wrapping your adapter with a {#link AdmobAdapterWrapper} and setting the listview to this wrapper
* FIRST OF ALL Please notice that the following code will work on a real devices but emulator!
*/
private void initListViewItems() {
lvMessages = (ListView) findViewById(R.id.lvMessages);
//creating your adapter, it could be a custom adapter as well
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,
android.R.layout.simple_list_item_1);
adapterWrapper = new AdmobAdapterWrapper(this);
adapterWrapper.setAdapter(adapter); //wrapping your adapter with a AdmobAdapterWrapper.
//here you can use the following string to set your custom layouts for a different types of native ads
//adapterWrapper.setInstallAdsLayoutId(R.layout.your_installad_layout);
//adapterWrapper.setcontentAdsLayoutId(R.layout.your_installad_layout);
//Sets the max count of ad blocks per dataset, by default it equals to 3 (according to the Admob's policies and rules)
adapterWrapper.setLimitOfAds(3);
//Sets the number of your data items between ad blocks, by default it equals to 10.
//You should set it according to the Admob's policies and rules which says not to
//display more than one ad block at the visible part of the screen,
// so you should choose this parameter carefully and according to your item's height and screen resolution of a target devices
adapterWrapper.setNoOfDataBetweenAds(10);
//It's a test admob ID. Please replace it with a real one only when you will be ready to deploy your product to the Release!
//Otherwise your Admob account could be banned
//String admobUnitId = getResources().getString(R.string.banner_admob_unit_id);
//adapterWrapper.setAdmobReleaseUnitId(admobUnitId);
lvMessages.setAdapter(adapterWrapper); // setting an AdmobAdapterWrapper to a ListView
//preparing the collection of data
final String sItem = "item #";
ArrayList<String> lst = new ArrayList<String>(100);
for(int i=1;i<=100;i++)
lst.add(sItem.concat(Integer.toString(i)));
//adding a collection of data to your adapter and rising the data set changed event
adapter.addAll(lst);
adapter.notifyDataSetChanged();
}
And the result will look like the following
Try using some other ad networking where it provides different types of native ads. Developers can customize the ads where to place and use it. For eg: if you need to place ads at second cell every 15 row, you can use like this.
Avocarrot provides that.
AvocarrotInstream myAd = new AvocarrotInstream(<yourListAdapter>);
myAd.initWithKey( "<your API Key>" );
myAd.setSandbox(true);
myAd.setLogger(true ,"ALL");
// Populate with In-Stream ads
myAd.loadAdForPlacement(this, "<your Placement Name>" );
// Bind the adapter to your list view component
<yourListView>.setAdapter(myAd);// here you are integrating ads to listview
myAd.setFrequency(2,15); // every 15 cells starting from the 2nd cell.
Here is Documentation it provides List ads and Feed ads.
Native ads are included in Google Play Services along with the rest of DFP/AdMob advertising. Make sure you have the following listed as dependencies in your build.gradle (note that 7.5.0 is the highest version as of this posting).
compile "com.google.android.gms:play-services-base:7.5.0"
compile "com.google.android.gms:play-services-ads:7.5.0"
Then you can display native ads
AdLoader adLoader = new AdLoader.Builder(context, "/6499/example/native")
.forAppInstallAd(new OnAppInstallAdLoadedListener() {
#Override
public void onAppInstallAdLoaded(NativeAppInstallAd appInstallAd) {
// Show the app install ad.
}
})
.forContentAd(new OnContentAdLoadedListener() {
#Override
public void onContentAdLoaded(NativeContentAd contentAd) {
// Show the content ad.
}
})
.withAdListener(new AdListener() {
#Override
public void onAdFailedToLoad(int errorCode) {
// Handle the failure by logging, altering the UI, etc.
}
})
.withNativeAdOptions(new NativeAdOptions.Builder()
// Methods in the NativeAdOptions.Builder class can be
// used here to specify individual options settings.
.build())
.build();
Click here for complete documentation.
As an addition to this thread, you can now implement NativeAds for Admob very easily by following the guide provided by Google using NativeExpressAdView .
For more information, check the google documentation :
https://firebase.google.com/docs/admob/android/native-express?hl=en
Add this code to your Listview Adapter
builder.forAppInstallAd(new NativeAppInstallAd.OnAppInstallAdLoadedListener() {
#Override
public void onAppInstallAdLoaded(NativeAppInstallAd ad) {
FrameLayout frameLayout =
(FrameLayout) findViewById(R.id.fl_adplaceholder);
NativeAppInstallAdView adView = (NativeAppInstallAdView) getLayoutInflater()
.inflate(R.layout.ad_app_install, null);
populateAppInstallAdView(ad, adView);
frameLayout.removeAllViews();
frameLayout.addView(adView);
}
});
AdLoader adLoader = builder.withAdListener(new AdListener() {
#Override
public void onAdFailedToLoad(int errorCode) {
Toast.makeText(MainActivity.this, "Failed to load native ad: "
+ errorCode, Toast.LENGTH_SHORT).show();
}
}).build();
adLoader.loadAd(new AdRequest.Builder().build());
Make some changes for listview Adapter and you will get populateAppInstallAdView() method from below link
Everything is covered in this example please go through this
https://github.com/googleads/googleads-mobile-android-examples/tree/master/admob
Adding to the mix, Tooleap Ads SDK provides a simple way to implement Admob's native ads.
Instead of requiring you to use the traditional listView adapter and showing the ad inside you content, they are showing admob native ads as a small floating bubble. When pressing on it you can see the full native ad.
Here is an example of using their SDK inside your activity class:
BubbleImageAd = new BubbleImageAd(this);
bubbleImageAd.setAdUnitId("YOUR_AD_UNIT_ID");
bubbleImageAd.loadAndShowAd(this);
You can check them out here.
This is working for me:
Copy Native template in project library.
in Build.gradle(Module:Appname) - add this:
implementation project(':nativetemplates')
implementation 'com.google.android.gms:play-services-ads:20.4.0'
in Setting.gradle(Appname) - add this:
include ':nativetemplates'
in Mainfest - update as earlier.
Before Application:
<meta-data
android:name="com.google.android.gms.ads.APPLICATION_ID"
android:value="ca-app-pub-xxxxxxxxxxxxxxxx~yyyyyyyyyy"/>
add this Permission:
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE"/>
In Layout file add this:
<com.google.android.ads.nativetemplates.TemplateView
android:id="#+id/my_template"
app:gnt_template_type="#layout/gnt_medium_template_view"
android:layout_width="match_parent"
android:layout_height="wrap_content" >
</com.google.android.ads.nativetemplates.TemplateView>
In program file:
MobileAds.initialize(this)
//build ad
val adLoader = AdLoader.Builder(this,"ca-app-pub-3940256099942544/2247696110")
.forNativeAd {
val style = NativeTemplateStyle.Builder().withMainBackgroundColor(ColorDrawable(Color.WHITE))
.build()
val template = findViewById<TemplateView>(R.id.my_template)
template.setStyles(style)
template.setNativeAd(it)
}.build()
//show ad
adLoader.loadAd(AdRequest.Builder().build())
If still not loading, Check with your admod credentials.
Well, this thread is probably outdated. But starting May 2015, and as of now, AdMob does support native ads (still in beta though).
https://support.google.com/admob/answer/6239795
Also it's available only to a limited number of developers as of its beta stage.
Admob in your android these are the codes needed.
<com.google.android.gms.ads.AdView
android:layout_alignParentBottom="true"
xmlns:ads="http://schemas.android.com/apk/res-auto"
android:id="#+id/adView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
ads:adSize="SMART_BANNER"
ads:adUnitId="ca-app-pub-4549020480017205/6066702579"
/>
//In your java class file
AdView mAdView = (AdView) findViewById(R.id.adView);
AdRequest adRequest = new AdRequest.Builder().build();
mAdView.loadAd(adRequest);
⟩⟩ In project structure, navigate to activity_main.xml and paste the following code in your layout.
<com.google.android.gms.ads.NativeExpressAdView
android:id="#+id/adView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_alignParentBottom="true"
ads:adSize="320x300"
ads:adUnitId="#string/ad_unit_id">
</com.google.android.gms.ads.NativeExpressAdView>
and in the same file i.e activity_main.xml add below lines of code in the header part
xmlns:ads="http://schemas.android.com/apk/res-auto"
⟩⟩ Now open MainActivity.java and add below lines of code in public class
private static String LOG_TAG = "EXAMPLE";
NativeExpressAdView mAdView;
VideoController mVideoController;
⟩⟩ Then under MainActivity.java and add below lines of code in onCreate() method.
// Locate the NativeExpressAdView.
mAdView = (NativeExpressAdView) findViewById(R.id.adView);
// Set its video options.
mAdView.setVideoOptions(new VideoOptions.Builder()
.setStartMuted(true)
.build());
// The VideoController can be used to get lifecycle events and info about an ad's video
// asset. One will always be returned by getVideoController, even if the ad has no video
// asset.
mVideoController = mAdView.getVideoController();
mVideoController.setVideoLifecycleCallbacks(new VideoController.VideoLifecycleCallbacks() {
#Override
public void onVideoEnd() {
Log.d(LOG_TAG, "Video playback is finished.");
super.onVideoEnd();
}
});
// Set an AdListener for the AdView, so the Activity can take action when an ad has finished
// loading.
mAdView.setAdListener(new AdListener() {
#Override
public void onAdLoaded() {
if (mVideoController.hasVideoContent()) {
Log.d(LOG_TAG, "Received an ad that contains a video asset.");
} else {
Log.d(LOG_TAG, "Received an ad that does not contain a video asset.");
}
}
});
mAdView.loadAd(new AdRequest.Builder().build());
⟩⟩ Now open string.xml file in values folder and paste the below line of code.
<string name="ad_unit_id">ca-app-pub-39402560999xxxxx/21772xxxxx</string>
⟩⟩ Then open the Manifest file and add Internet permission to it.
<uses-permission android:name="android.permission.INTERNET" />
THE RESOURCE : How to insert AdMob Native Ad in your Android App
It is restricted to selected publishers at the moment. You'll need to contact a Google account manager in your region for implementation.
Yes it is possible you can use below code in xml file
<com.google.android.gms.ads.NativeExpressAdView
android:id="#+id/adView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_alignParentBottom="true"
ads:adSize="320x300"
ads:adUnitId="#string/ad_unit_id">
mAdView.setVideoOptions(new VideoOptions.Builder()
.setStartMuted(true)
.build());
mVideoController = mAdView.getVideoController();
mVideoController.setVideoLifecycleCallbacks(new VideoController.VideoLifecycleCallbacks() {
#Override
public void onVideoEnd() {
Log.d(LOG_TAG, "Video playback is finished.");
super.onVideoEnd();
}
});
mAdView.setAdListener(new AdListener() {
#Override
public void onAdLoaded() {
if (mVideoController.hasVideoContent()) {
Log.d(LOG_TAG, "Received an ad that contains a video asset.");
} else {
Log.d(LOG_TAG, "Received an ad that does not contain a video asset.");
}
}
});
mAdView.loadAd(new AdRequest.Builder().build());
To add Native Templates in our app we have to follow some basic steps:
1-First, we have to download the Native Templates, so got to developers.google
2-Then click on the Download Native Templates, now you will be directed to Github
3-Then download the zip file from GitHub and extract the zip file to any folder and remember the location of the folder, we will use it later
4-Now go inside Android studio and click on File->New->Import Module, Now you will see a new window (Import-Module from Source) Now click on the Browse icon and select the nativetemplates folder and click on finish and wait for Gradle build to finish.
5-Now open the Gradle Scripts->build.gradle (Module: app) section and import the nativetemplates project and click the "sync Now" show at the top as shown below:
//adding native templates
implementation project(':nativetemplates')
https://www.studytonight.com/post/how-to-add-admob-native-ad-in-android-app#

Revmob fullscreen ad preload on Unity?

Is there any way to preload fullscreen ad on Unity? Right now when we call it using
revmob.ShowFullscreen();
when we create end game screen. But most of the time it loads after 5/10 secs later which is in-game most probably if you press restart, so it shows a full screen ad during gameplay.
I've found some ways to preload it on native android and tried same function to see if they exists in Unity but no luck.
Thanks.
Yes! You can use the following code:
private RevMobFullscreen fullscreen;
fullscreen = revmob.CreateFullscreen();
fullscreen.show();
If you need more information, you can access RevMob mobile ad network website: https://www.revmobmobileadnetwork.com
It will be better to add this code to the Create statement:
private RevMobFullscreen fullscreen;
fullscreen = revmob.CreateFullscreen();
...and then also this code to the listener:
RevMobAdsListener revmobListener = new RevMobAdsListener() {
// Required
#Override
public void onRevMobSessionIsStarted() {
fullscreen.show();
}
(...)
}
This will show the fullscreen ad.
You can do like this to preload revmob videos in unity. But there are memory leaks in revmob unity videos and they might fix that in 9.2.x...
REVMOB_APP_IDS = new Dictionary<string, string>() {
{ "Android", androidMediaId},
{ "IOS", iosMediaId }
};
revmob = RevMob.Start (REVMOB_APP_IDS, gameObject.name);
public void SessionIsStarted ()
{
CacheVideoInterstitial("Bootup");
}
public void CacheVideoInterstitial(string location) {
DestroyVideo();
StartCoroutine(CacheAfterEndofFrame(location));
}
IEnumerator CacheAfterEndofFrame(string location) {
yield return null;
fullscreenVideo = revmob.CreateVideo(location);
}
void DestroyVideo() {
if( fullscreenVideo != null ) {
fullscreenVideo.Hide();
//fullscreenVideo.Release();
//fullscreenVideo = null;
}
}
// revmob ad closing delegate
public void UserClosedTheAd (string revMobAdType)
{
DestroyVideo();
CacheVideoInterstitial(this.location);
}

Custom Events in Admob Mediation adding unsupported Ad Networks

I have read everything about how to implement the Custom Events in the Admob Mediation.
I have added the full packaged class name and everything is set in the Admob portal.
This is the class implementation done
public class CustomEvent implements CustomEventBanner, AdListener{
private CustomEventBannerListener bannerListener;
private AdView adView;
#Override
public void requestBannerAd(final CustomEventBannerListener listener,
final Activity activity,
String label,
String serverParameter,
AdSize adSize,
MediationAdRequest mediationAdRequest) {
// Keep the custom event listener for use later.
this.bannerListener = listener;
// Determine the best ad format to use given the adSize. If the adSize
// isn't appropriate for any format, an ad will not fill.
AdSize bestAdSize = adSize = adSize.findBestSize(
AdSize.BANNER,
AdSize.IAB_BANNER,
AdSize.IAB_LEADERBOARD,
AdSize.IAB_MRECT,
AdSize.IAB_WIDE_SKYSCRAPER);
if (bestAdSize == null) {
listener.onFailedToReceiveAd();
return;
}
// Initialize an AdView with the bestAdSize and the publisher ID.
// The publisher ID is the server parameter that you gave when creating
// the custom event.
this.adView = new AdView(activity, bestAdSize, serverParameter);
// Set the listener to register for events.
this.adView.setAdListener(this);
// Generate an ad request using custom targeting values provided in the
// MediationAdRequest.
AdRequest adRequest = new AdRequest()
.setBirthday(mediationAdRequest.getBirthday())
.setGender(mediationAdRequest.getGender())
.setKeywords(mediationAdRequest.getKeywords())
.setLocation(mediationAdRequest.getLocation());
if (mediationAdRequest.isTesting()) {
adRequest.addTestDevice(AdRequest.TEST_EMULATOR);
}
// Load the ad with the ad request.
this.adView.loadAd(adRequest);
}
#Override
public void onReceiveAd(Ad ad) {
this.bannerListener.onReceivedAd(this.adView);
}
#Override
public void onFailedToReceiveAd(Ad ad, ErrorCode errorCode) {
this.bannerListener.onFailedToReceiveAd();
}
#Override
public void onPresentScreen(Ad ad) {
this.bannerListener.onClick();
this.bannerListener.onPresentScreen();
}
#Override
public void onDismissScreen(Ad ad) {
this.bannerListener.onDismissScreen();
}
#Override
public void onLeaveApplication(Ad ad) {
this.bannerListener.onLeaveApplication();
}
}
The problem is that I really dont know how to add use my layout.add(adview) in the onReceivedAd(),
Any inputs would be helpful.
Custom Events are a little different compared to normal AdMob implementation. In requestBannerAd you create your ad network's adview, and request an ad. Once an ad is received (in this the onReceiveAd callback), you invoke:
this.bannerListener.onReceivedAd(this.adView);
You're already doing this in your code. When invoking this, you're telling the AdMob Mediation layer "Hey, I successfully loaded an ad, and here is my view for you to show." The Mediation layer takes in your adview and essentially calls layout.addView(adView) on your behalf (it adds it as a child of the main AdView you defined in your app).
So in your case, this code should just work.

Categories

Resources