Yesterday I created an interstitial ad using Admob in my Android app.
The test is showing fine, but if I put the real AdUnitId , the ad is not loading.
This is my code
MobileAds.initialize(this, "ca-app-pub-***");
mInterstitialAd = new InterstitialAd(getActivity());
mInterstitialAd.setAdUnitId("ca-app-pub-**");
mInterstitialAd.loadAd(new AdRequest.Builder().build());
mInterstitialAd.setAdListener(new AdListener() {
#Override
public void onAdLoaded() {
// Code to be executed when an ad finishes loading.
Log.i("Ads", "onAdLoaded");
mInterstitialAd.show();
}
});
Related
I am testing Ads in an Android app.
I want a button to be shown when the ad is loaded, and then click on the button to show the ad.
My issue is that the ad is never loaded.
This is my code so far:
MobileAds.initialize(getActivity(), "ca-app-pub-***");
mInterstitialAd = new InterstitialAd(getActivity());
mInterstitialAd.setAdUnitId("ca-app-pub-ca-app-pub-***");
mInterstitialAd.loadAd(new AdRequest.Builder().build());
btnOfertar.setVisibility(View.GONE);
mInterstitialAd.setAdListener(new AdListener() {
#Override
public void onAdLoaded() {
// Code to be executed when an ad finishes loading.
Log.i("Ads", "onAdLoaded");
btnOfertar.setVisibility(View.VISIBLE);
}
});
You should try the following:
mInterstitialAd.setAdListener(new AdListener() {
#Override
public void onAdLoaded() {
// Add this
mInterstitialAd.show()
Log.i("Ads", "onAdLoaded");
btnOfertar.setVisibility(View.VISIBLE);
}
});
Also, try using the dedicated test ad id that Google provides in the documentation: ca-app-pub-3940256099942544/1033173712. If this loads properly your code should be okay.
Your actual ad might take a while to show up depending on how many requests you're getting to show the ad. Also make sure that your AdMob's payment is set up correctly.
I try to add an Interstitial ads, the problem is that i have 2 code to change.
The 2 codes to change in my MainActivity:
But in my admob account, I can only find the ca-app-pub-xxxxx....xx code and I can't find the ID_INTERSTITIAL_UNIT_ID.
Can any body please show me where to find the second one?
I tried to put the same links in the two code but I am afraid of being banned from admob.
You will need only <string name="interstitial_ad_unit_id">ca-app-pub-3940256099942544/1033173712</string>
and do this on you onCreate() method
//region Interstitial
String interstitialAdId=getResources().getString(R.string.interstitial_ad_unit_id);
mInterstitialAd = new InterstitialAd(this);
mInterstitialAd.setAdUnitId(interstitialAdId);
mInterstitialAd.setAdListener(new AdListener() {
#Override
public void onAdClosed()
{
}
});
AdRequest adRequest = new AdRequest.Builder().build();
mInterstitialAd.loadAd(adRequest);
//endregion
then add
if (mInterstitialAd.isLoaded())
mInterstitialAd.show();
where you want to start the Interstitial
What's the behavior of the admob interstitial after an error occurs? It automatically retries to reload the data or not?
In other words I have to write something like:
mInterstitialAd.setAdListener(new AdListener(){
//ERROR MANAGEMENT
#Override
public void onAdFailedToLoad(int error) {
mInterstitialAd.loadAd(adRequest);//Retry
}
});
Or admob auto-retry to load ad?
in case of error loading interstitial ad, try this code in your adListener's onAdFailedToLoad method :
AdRequest adRequest = new AdRequest.Builder().build();
mInterstitialAd.loadAd(adRequest);
this will create a new request for ad.
I am working with frame animations and i implement banner and interstitial ads in my activity, but when i do that it slows my application.
Here is my code:
private InterstitialAd interstitial;
interstitial = new InterstitialAd(MainActivity.this);
// Insert the Ad Unit ID
interstitial.setAdUnitId("UNIT ID");
//Locate the Banner Ad in activity_main.xml
AdView adView = (AdView) this.findViewById(R.id.adView);
// Request for Ads
AdRequest adRequest = new AdRequest.Builder()
// Add a test device to show Test Ads
.addTestDevice(AdRequest.DEVICE_ID_EMULATOR)
.addTestDevice("CC5F2C72DF2B356BBF0DA198")
.build();
// Load ads into Banner Ads
adView.loadAd(adRequest);
// Load ads into Interstitial Ads
interstitial.loadAd(adRequest);
// Prepare an Interstitial Ad Listener
interstitial.setAdListener(new AdListener() {
public void onAdLoaded() {
// Call displayInterstitial() function
displayInterstitial();
}
});
}
Any solutions on how to efficiently integrate admob in my class.
Answere to your comment
can i put all my admob code in someother class and initialize it in my
Main Activity?
Yes, For example: AdMob.java class:
import android.content.Context;
import com.google.android.gms.ads.AdListener;
import com.google.android.gms.ads.AdRequest;
import com.google.android.gms.ads.InterstitialAd;
public class AdMob {
private InterstitialAd interstitial;
public void displayInterstitial() {
// If Ads are loaded, show Interstitial else show nothing.
if (interstitial.isLoaded()) {
interstitial.show();
}
}
void LoadInterstitialAd(Context ctx){
// Prepare the Interstitial Ad
interstitial = new InterstitialAd(ctx);
// Insert the Ad Unit ID
interstitial.setAdUnitId("unit_id");
// Request for Ads
AdRequest adRequest = new AdRequest.Builder()
// Add a test device to show Test Ads
//.addTestDevice(AdRequest.DEVICE_ID_EMULATOR)
//.addTestDevice("CC5F2C72DF2B356BBF0DA198")
.build();
// Load ads into Interstitial Ads
interstitial.loadAd(adRequest);
// Prepare an Interstitial Ad Listener
interstitial.setAdListener(new AdListener() {
public void onAdLoaded() {
// Call displayInterstitial() function
displayInterstitial();
}
});
}
}
You can use it anywhere by doing this:
AdMob adMob = new AdMob();
adMob.LoadInterstitialAd(getApplicationContext());
I have only showed for interstitial ad, but it can be done for other ads too..
i want to have an admob interstitial ad show up when the user first opens the app or when the user navigates to another app (like browser) and then returns to my app. This is my current code for interstital ad, this code is entirely contained inside the OnCreate method.
// Create the interstitial.
interstitial = new InterstitialAd(this);
interstitial.setAdUnitId("ca-app-pub-XXXXXXXXXXXXXXXX");
// Create ad request.
AdRequest adRequest2 = new AdRequest.Builder().build();
// Begin loading your interstitial.
interstitial.loadAd(adRequest2);
interstitial.setAdListener(new AdListener() {
#Override
public void onAdLoaded() {
if (interstitial.isLoaded()) {
interstitial.show();
}
}
});
This seems to work for most situations, but on certain devices this will create a loop of interstitial showing 2-3 sec after they are dismissed by the user. one of those device that has the loop is a Galaxy Tab3 . i cant seem to figure out a proper way to setup my code so that this behavior does not happen on any device.
Use the following code to display the interstitial ad when appstarts.
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
interstitial = new InterstitialAd(this);
interstitial.setAdUnitId("***********");
AdRequest adRequest = new AdRequest.Builder().build();
interstitial.loadAd(adRequest);
interstitial.setAdListener(new AdListener() {
public void onAdLoaded() {
displayInterstitial();
}
});
}
public void displayInterstitial() {
if (interstitial.isLoaded()) {
interstitial.show();
}
}
Create a boolean var to assist with it..
boolean ad_shown = false;
When you do .show() turn the variable to true.
Don't forget to put a guard on the if
if (interstitial.isLoaded() && !ad_shown) {