Following this code (https://developers.google.com/mobile-ads-sdk/docs/admob/advanced):
import com.google.ads.*;
public class BannerExample extends Activity implements AdListener {
private InterstitialAd interstitial;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
// Create the interstitial
interstitial = new InterstitialAd(this, MY_INTERSTITIAL_UNIT_ID);
// Create ad request
AdRequest adRequest = new AdRequest();
// Begin loading your interstitial
interstitial.loadAd(adRequest);
// Set Ad Listener to use the callbacks below
interstitial.setAdListener(this);
}
#Override
public void onReceiveAd(Ad ad) {
Log.d("OK", "Received ad");
if (ad == interstitial) {
interstitial.show();
}
}
}
I have an image (my advertise) smaller than my phone screen.
I want to know if it's possible to resize the view created into the java code. (in onCreate() or onReceiveAd()) for adapting the size of the advertise.
LayoutParam, in CenterCrop for exemple, or simple resizing of the view !
If you want to serve an interstitial from DFP to a phone, you can set up an image in DFP, and override the size to be 320x480. Then that creative can serve, and be smaller if you want.
Or, you may be looking for a banner instead of an interstitial overlay that takes over the entire screen. In that case, you can specify a custom size for your AdView, that should match the size you set up in DFP.
Related
I am trying to put an interstitial ad after every 5 games the user plays. What I've done is created an empty object with the following script:
using UnityEngine;
using GoogleMobileAds.Api;
public class AdBetweenGames : MonoBehaviour {
InterstitialAd interstitial;
void Start()
{
if(PlayerPrefs.GetInt("games-played", -1) % 5 == 0)
{
this.ShowInterstitial();
}
}
private void RequestInterstitial()
{
string adUnitId = "UNITID";
// Initialize an InterstitialAd.
interstitial = new InterstitialAd(adUnitId);
// Create an empty ad request.
AdRequest request = new AdRequest.Builder().Build();
// Load the interstitial with the request.
interstitial.LoadAd(request);
}
public void ShowInterstitial()
{
RequestInterstitial();
Debug.Log("Interstatial ad called.");
if (interstitial.IsLoaded())
{
interstitial.Show();
}
}
}
I am sure the code here is called properly based on the debug log. I've also tried doing banner ads and they show without a problem after 1-2 seconds. I am making an android game.
Also I was thinking, could it be that the ad needs some extra time to load and I am clicking try again too early. But then what would a solution be?
The interstitial ad of ad mob need time to load try it in update and wait for at least 4 sec and call ShowInterstitial function inside a bool set its value true in start and false it after calling the function to show ad once.
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"
Some user intentionally try to click banner ads many times.Due to this we face problem of account suspension or termination. Does anyone know how to stop the ad being loading if it cross some limit(for example 3).
AdView adView = (AdView) findViewById(R.id.adView);
AdRequest adRequest = new AdRequest.Builder()
.setRequestAgent("android_studio:ad_template").build();
adView.loadAd(adRequest);
if(currentbannerclick>3)
{
// some code to not load the ad.
}
LinearLayout id=container
AdView id=adView
if(currentbannerclick>3)
container.removeView(adView);
Thank you everyone for your answer.
You can identify if Ad is clicked using Activity life-cycle callbacks. you can then find out how much time user clicked your Ad and call adView.loadAd(adRequest); only if the user clicked your Ad less than your threshold.
You can attach an AdListener to your AdView and increase your click counter in the onAdLoaded or onAdOpened methods. More info here: https://developers.google.com/android/reference/com/google/android/gms/ads/AdListener#public-methods
This should work:
private void loadAd() {
// This is a one element array because it needs to be declared final
// TODO: you should probably load the default value from somewhere because of activity restarts
final int[] currentBannerClick = {0};
final AdView adView = (AdView) findViewById(R.id.adView);
adView.setAdListener(new AdListener() {
#Override
public void onAdOpened() {
super.onAdOpened();
currentBannerClick[0]++;
if (currentBannerClick[0] > 3) {
adView.setVisibility(View.INVISIBLE);
adView.destroy();
}
// TODO: save currentBannerClick[0] somewhere, see previous TODO comment
}
});
if (currentBannerClick[0] <= 3) {
AdRequest adRequest = new AdRequest.Builder().addTestDevice(YOUR_DEVICE_ID).build();
adView.setVisibility(View.VISIBLE);
adView.loadAd(adRequest);
} else {
adView.setVisibility(View.INVISIBLE);
}
}
You can also limit number of Ads displayed for user in AdMob System. You can set limit of 3 ads per user per minut, hour or day.
I have an Android app and I'm trying to add AdMob interstitials into it. I create them this way
interstitial = new InterstitialAd(this);
interstitial.setAdUnitId(getResources().getString(R.string.ad));
interstitial.loadAd(AdUtils.createRequest());
and when the moment is appropriate, I show them like this:
if(interstitial.isLoaded()) {
interstitial.show();
}
When I test this code on the emulator, everything is fine. But when I run it on the real device, it often (about a half of all shows) displays only a black screen with the close button and no advertisement image at all. Is there any solution to this issue?
You can try as below.
Declare this as a variable.
private InterstitialAd interstitial;
then use this part in your onCreate method
interstitial = new InterstitialAd(this, "your id");
// Create ad request
AdRequest adRequest = new AdRequest();
// Begin loading your interstitial
interstitial.loadAd(adRequest);
// Set Ad Listener to use the callbacks below
interstitial.setAdListener(this);
and at last set this outside your onCreate method so basically set below code in your activity..
#Override
public void onReceiveAd(Ad ad) {
// TODO Auto-generated method stub
if (ad == interstitial) {
interstitial.show();
}
}
Hope this will help you.
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.