Admob android cannot change from banner INVISIBLE to VISIBLE - android

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");
}
});

Related

Ad not loaded after request

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.

Banner Ads and Interstitial Ad Unit id Issue

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();
}

Can you show both banner ads and interstitial ads?

Can you show both banner ads and interstitial ads on the same activity? Is it against AdMob policy?
For example, when you open the app, interstitial ad shows up, and when you close it, banner ad shows up, or when banner ad is already showed you display interstitial ad.
Yes, you can.
Have a look at the documentation for more info
https://developers.google.com/mobile-ads-sdk/docs/admob/fundamentals
Also you might find using an "AdListener" very useful for your purposes!
adView.setAdListener(new AdListener() {
#Override
public void onAdClosed() {
//Called when the user is about to return to the application after
super.onAdClosed();
}
#Override
public void onAdFailedToLoad(int errorCode) {
//Called when an ad request failed
super.onAdFailedToLoad(errorCode);
}
#Override
public void onAdLeftApplication() {
//Called when an ad leaves the application (e.g., to go to the browser)
super.onAdLeftApplication();
}
#Override
public void onAdOpened() {
//Called when an ad is received
super.onAdOpened();
}
#Override
public void onAdLoaded() {
//Called when an ad opens an overlay that covers the screen
super.onAdLoaded();
}
});
You can fill each method with your own code :)
Ofcourse you can show both the banner ad and interstitial ad in you application. Here you go.
MainActivity.java add below lines of code in onCreate metho

Android Publisher Interstitial Ad displaying after close

My application opens a Publisher InterstitialAd whenever you start but when a user closes it reopens. This process happens constantly and then you can't use application, can anyone help me?
public void getIntertitalAds(boolean isPortraitMode)
{
interstitial = new PublisherInterstitialAd(context);
if(isPortraitMode)
interstitial.setAdUnitId(tags.getAdUnitInterstitial());
else
interstitial.setAdUnitId(tags.getAdUnitInterstitialTablet());
AdListener adListener = new AdListener() {
#Override
public void onAdLoaded() {
super.onAdLoaded();
if(interstitial!=null)
interstitial.show();
}
#Override
public void onAdClosed() {
super.onAdClosed();
interstitial = null;
}
};
// Create ad request.
PublisherAdRequest adRequest = new PublisherAdRequest.Builder()
.build();
// Begin loading your interstitial.
interstitial.setAdListener(adListener);
interstitial.loadAd(adRequest);
}
NEVER call interstitial.show() from AdListener#onAdLoaded(). You have no control over when it will be called and it presents a really poor user experience. Instead call interstitial.show() at a natural break point in your app.
There is no need to have separate AdUnitIds for portrait and landscape. Interstitials are the same regardless.
All the code in your getIntertitalAds() (sic) method should be Activity#onCreate
I strongly suspect your problems stem from a combination of 1 and 3.

Admob interstitial on app start

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) {

Categories

Resources