I am implementing admob ads in android but Interstitial ads are not showing and only the banner shows.
is it required for the Banner Ads and the Interstitial Ads to have different Ad Unit ID in Admob Android?
Any suggestions
mInterstitialAd = new InterstitialAd(this);
mInterstitialAd.setAdUnitId("myAdUnitId");
// Create an ad request.
AdRequest.Builder adRequestBuilder = new AdRequest.Builder();
// Optionally populate the ad request builder.
adRequestBuilder.addTestDevice(AdRequest.DEVICE_ID_EMULATOR);
// Set an AdListener.
mInterstitialAd.setAdListener(new AdListener() {
#Override
public void onAdLoaded() {
Toast.makeText(MyActivity.this,
"The interstitial is loaded", Toast.LENGTH_SHORT).show();
}
#Override
public void onAdClosed() {
// Proceed to the next level.
goToNextLevel();
}
});
// Start loading the ad now so that it is ready by the time the user is ready to go to
// the next level.
mInterstitialAd.loadAd(adRequestBuilder.build());
// Create the button to go to the next level.
mNextLevelButton = new Button(this);
mNextLevelButton.setText("Next Level");
mNextLevelButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
// Show the interstitial if it is ready. Otherwise, proceed to the next level
// without ever showing it.
if (mInterstitialAd.isLoaded()) {
mInterstitialAd.show();
} else {
// Proceed to the next level.
goToNextLevel();
}
}
});
// Add the next level button to the layout.
LinearLayout layout = new LinearLayout(this);
layout.setOrientation(LinearLayout.VERTICAL);
layout.addView(mNextLevelButton);
// Create a TextView to display the current level.
mTextView = new TextView(this);
mTextView.setText("Level 1");
layout.addView(mTextView);
setContentView(layout);
}
public void goToNextLevel() {
// Show the next level (and disable the next level button since there are no more levels.
mNextLevelButton.setEnabled(false);
mTextView.setText("Level 2");
}
}
Here is a nice example on developers.google.com
Check your AD Type id.Very it both in your admob account and source code that the AD Unit Id belong to interstitial Ad.
If thats correct use this code
public partial class MainPage : PhoneApplicationPage
{
private InterstitialAd interstitialAd;
// Constructor
public MainPage()
{
InitializeComponent();
interstitialAd = new InterstitialAd("your id");
AdRequest adReques = new AdRequest();
interstitialAd.ReceivedAd += OnAdReceived;
interstitialAd.LoadAd(adReques);
}
private void OnAdReceived(object sender, AdEventArgs e)
{
System.Diagnostics.Debug.WriteLine("Ad received successfully");
interstitialAd.ShowAd();
}
Related
I am working on an android app and I want to display interstitial ad every time a button in clicked. I have searched for it on stackoverflow and used the given suggestion but still I am not able to do it.
First I have declared interstitial ad as
private InterstitialAd mInterstitialAd;
private AdRequest adRequest;
I have used this:
mInterstitialAd = new InterstitialAd(this);
mInterstitialAd.setAdUnitId("ca-app-pub-3940256099942544/1033173712");
adRequest = new AdRequest.Builder()
.build();
mInterstitialAd.loadAd(adRequest);
btn = (Button) findViewById(R.id.Chm);
btn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
if (mInterstitialAd.isLoaded()) {
mInterstitialAd.show();
}
mInterstitialAd.loadAd(adRequest);
Intent i = new Intent(MainActivity.this, Btn.class);
startActivity(i);
}
}
);
But when I run the app, I click the above button I do not get any ad but when I click the back button after clicking the above button, I get the ad but that too only first time, not always. Please tell me my mistake.
Thanks.
As per my knowledge you can fix it by including setAdListner
mInterstital.setAdListener(new AdListener() {
#Override
public void onAdClosed() {
mInterstital.loadAd(new AdRequest.Builder().build());
}
});
How do you people use admob banner? Can you please take a look at my code and show what is wrong with it. I have a game that has a Pause screen and it has much room to place a banner.
However when I load it, banner takes too long to show up in slow connection. So i made to load it up but keep it INVISIBLE, then make it visible when I need it. But it's not work. The banner won't visible! Please give me any advice...
Here is the code:
//Admob request Banner
bannerAdmob = new AdView(this);
bannerAdmob.setAdSize(AdSize.WIDE_SKYSCRAPER);
bannerAdmob.setAdUnitId(Setting.admobBannerId);
requestAdmobBanner();
I use handler to choose adunit. Interstitial is work nice, but neither banner.
protected Handler handlerAdmob = new Handler() {
#Override
public void handleMessage(Message msg) {
switch (msg.what) {
case 1: //If is interstitial
if (interstitialAdmob.isLoaded()) {
interstitialAdmob.show();
} else {
requestAdmobInterstitial();
}
break;
case 2: //If is banner
bannerAdmob.setVisibility(View.VISIBLE);
break;
case 6: //Hide banner
bannerAdmob.destroy();
requestAdmobBanner(); //Request new banner
}
}
};
Then the method to load banner
private void requestAdmobBanner() {
AdRequest bannerRequest = new AdRequest.Builder()
// Add a test device to show Test Ads
.addTestDevice(AdRequest.DEVICE_ID_EMULATOR)
.addTestDevice(Setting.Device_ID)
.build();
// Load the banner ad.
bannerAdmob.loadAd(bannerRequest);
// Now we add ads listener for Admob banner so we can SHOW and HIDE it
bannerAdmob.setAdListener(new AdListener() {
#Override
public void onAdFailedToLoad(int errorCode) { // On admob interstitial failed to load, request new ad
bannerAdmob.setVisibility(View.GONE);
}
#Override
public void onAdLoaded() {
bannerAdmob.setVisibility(View.INVISIBLE);
System.out.println("Banner Admob is load, but still INVISIBLE");
}
});
}
You are setting the AdView to be INVISIBLE, when you should be setting it to VISIBLE. See code below:
// Now we add ads listener for Admob banner so we can SHOW and HIDE it
bannerAdmob.setAdListener(new AdListener() {
#Override
public void onAdFailedToLoad(int errorCode) { // On admob interstitial failed to load, request new ad
bannerAdmob.setVisibility(View.GONE);
}
#Override
public void onAdLoaded() {
bannerAdmob.setVisibility(View.VISIBLE);
System.out.println("#onAdLoaded - making AdView visible");
}
});
I want to show an Interstitial Ad to a user who has downloaded the app for the first time only. This will show only once.
I have written code to determine if the user is a first time user. - Which is working fine.
ISSUE / ERORR
My displayInterstitial() method is returning AD IS NOT LOADED? - interstitial.isLoaded() is false hence interstitial.show() does not get called.
MY UNDERSTANDING
I have loadAd() in the oncreate, so the displayInterstitial() should work in this case. But it doesn't.
CODE
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.inbox_list);
createAdmobBanner();
// Create the interstitial.
interstitial = new InterstitialAd(this);
interstitial.setAdUnitId("ca-app-pub-xxxx/xxx");
// Create ad request.
AdRequest adRequestIN = new AdRequest.Builder().build();
// Begin loading your interstitial.
interstitial.loadAd(adRequestIN);
//CHECK IF USER DOWNLOADED APP FOR FIRST TIME.
SharedPreferences settings = getSharedPreferences(PREFS_NAME, 0);
if (settings.getBoolean("my_first_time", true)) {
//the app is being launched for first time, do something
Log.d("Comments", "First time");
//CALL LARGE SCREEN ADD
displayInterstitial();
// record the fact that the app has been started at least once
settings.edit().putBoolean("my_first_time", false).commit();
}
// Set an AdListener.
interstitial.setAdListener(new AdListener() {
#Override
public void onAdLoaded() {
}
#Override
public void onAdClosed() {
// Proceed to the next level.
// Create ad request.
AdRequest adRequestIN = new AdRequest.Builder().build();
// Begin loading your interstitial.
interstitial.loadAd(adRequestIN);
}
});
//SHOW ADD
public void displayInterstitial()
{
if (interstitial.isLoaded())
{
interstitial.show();
Log.d("response", "AD IS LOADED: ");
}
else
{
Log.d("response", "AD IS NOT LOADED: " );
}
}
You are calling displayInterstitial() before an ad has been loaded.
Interstitial ads take some time to load.
You are trying to display it too early.
Iam trying to display ad using Admob,but Iam not getting any call at onAdloaded.Can someone help...
Iam displaying an inerstitial ad.Iam setting and then Iam trying to load it...
#Override
protected void onCreate(Bundle arg0) {
super.onCreate(arg0);
CalligraphyConfig.initDefault(getString(R.string.font_alegreya_sans_regular));
setInterstitialAd();
setContentView(R.layout.splashscreen_activity);
}
private void setInterstitialAd() {
mInterstitial = new InterstitialAd(this);
mInterstitial.setAdUnitId(INTERSTITIAL_AD_ID);
// Create ad request.
AdRequest adRequest = new AdRequest.Builder().build();
// Begin loading your interstitial.
mInterstitial.loadAd(adRequest);
mInterstitial.setAdListener(new AdListener() {
#Override
public void onAdLoaded() {
displayInterstitial();
}
#Override
public void onAdFailedToLoad(int errorCode) {
}
});
}
private void initAdView() {
AdView mAdView = (AdView) findViewById(R.id.ad_container_layout);
AdRequest adRequest = new AdRequest.Builder().build();
mAdView.loadAd(adRequest);
}
You should'nt have displayInterstitial() inside onAdLoaded(). This is causing the outcome of Ad not being shown. Either show an Ad after some level or user made action like after five level, one is displaying an interstitial, it comes in the pause menu. you keep it loaded from before and display it on the fifth level. or you can have it in that pause menu after some amount of time using a timer ( in which displayInterstitial() ) is included.
Suraj, add some log into both callback methods. One of them will be called.
I suspect the ad request is failing for reason, so onFailedToLoad() will be getting called instead.
Also DON'T call displayInterstitial() from onAdLoaded(). This causes really poor user experience and is likely to get your Admob account banned.
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) {