I have integrated Admob native ads. I have used same adUnitId in multiple screen to load native ads. In most of the screen my code is working fine but in some screen onAdLoaded overridden method is calling but ads is not displaying.
I am using below code to native ads.
nativeExpressAdView.setAdListener(new AdListener() {
#Override
public void onAdFailedToLoad(int i) {
super.onAdFailedToLoad(i);
nativeExpressAdView.setVisibility(View.GONE);
}
#Override
public void onAdLeftApplication() {
super.onAdLeftApplication();
nativeExpressAdView.setVisibility(View.GONE);
}
#Override
public void onAdLoaded() {
super.onAdLoaded();
nativeExpressAdView.setVisibility(View.VISIBLE);
}
nativeExpressAdView.loadAd(new AdRequest.Builder().build());
nativeExpressAdView.setVisibility(View.VISIBLE);
View of Nativeexpressview
<com.google.android.gms.ads.NativeExpressAdView
android:id="#+id/nativeExpressAdView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
app:adSize="300x250"
android:layout_marginTop="#dimen/card_margin"
app:adUnitId="#string/ad_unit_id"/>
onAdLoaded() method is firing but ads is not loading. Can any one pls help me to identify the issue.
After you show an add always bring the banner to front
nativeExpressAdView.bringToFront()
I do that in onresume as well.
Related
I created an account at Admob since a month, and i added payment method since 2 days ago.
a test device showing ads successfully. but other devices don't show any thing with error code:3
Ads: Ad failed to load : 3
public class MainActivity extends AppCompatActivity implements RewardedVideoAdListener {
private AdView adView;
private Button onePlayer, twoPlayer, exit;
private InterstitialAd mInterstitialAd;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
MobileAds.initialize(this, getString(R.string.app_id));
MobileAds.initialize(this, initializationStatus -> {
});
mInterstitialAd = new InterstitialAd(this);
mInterstitialAd.setAdUnitId(getString(R.string.instruction));
mInterstitialAd.loadAd(new AdRequest.Builder().addTestDevice("my-device-id").build());
mInterstitialAd.setAdListener(InterstitialListener);
adView = findViewById(R.id.adView);
adView.loadAd(new AdRequest.Builder().addTestDevice("my-device-id").build());
adView.setAdListener(adListener);
}
}
and i created a listener for InterstitialAd
private AdListener InterstitialListener = new AdListener() {
#Override
public void onAdLoaded() {
// Code to be executed when an ad finishes loading.
mInterstitialAd.show();
}
#Override
public void onAdFailedToLoad(int errorCode) {
// Code to be executed when an ad request fails.
Toast.makeText(getApplicationContext(), "errorCode " + errorCode + "", Toast.LENGTH_LONG).show();
}
#Override
public void onAdOpened() {
// Code to be executed when the ad is displayed.
}
#Override
public void onAdClicked() {
// Code to be executed when the user clicks on an ad.
}
#Override
public void onAdLeftApplication() {
// Code to be executed when the user has left the app.
}
#Override
public void onAdClosed() {
// Code to be executed when the interstitial ad is closed.
}
};
and this is banner xml:
<com.google.android.gms.ads.AdView
xmlns:ads="http://schemas.android.com/apk/res-auto"
android:id="#+id/adView"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
ads:adSize="BANNER"
ads:adUnitId="#string/banner">
</com.google.android.gms.ads.AdView>
Note: the app doesn't uploaded to store yet!
how can i fix this problem?
This answer might help someone in future. I had the same problem. Ads were working fine in test devices. But in real device it did not display any ad.
I published my app to google play and downloaded it. In the app downloaded from google play, Ads are showing fine. I did not make any changes to the code
I am not sure why this happens, it's kind of black box.
Some answers in stack overflow says no need to publish app for showing ads. But it was not true in my case.
Your code seems fine.
Are you using this test unit ID for your banner?
ca-app-pub-3940256099942544/6300978111
Or are you using the one from your AdMob account?
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'm trying to get my Google ads to show on an Android app. I've done these things already:
Connect to Firebase
Added gradle file code
Set up admob account
Have the proper code
It seems like there's nothing more for me to do, but they're not showing up.
Here's an example of what I have in onCreate.
//AdMob initialization
MobileAds.initialize(this, "**myADmob ID**");
mAdView = findViewById(R.id.adView);
AdRequest adRequest = new AdRequest.Builder().build();
mAdView.loadAd(adRequest);
AdView adView = new AdView(this);
adView.setAdSize(AdSize.SMART_BANNER);
mAdView.setAdListener(new AdListener() {
#Override
public void onAdLoaded() {
// Code to be executed when an ad finishes loading.
}
#Override
public void onAdFailedToLoad(int errorCode) {
// Code to be executed when an ad request fails.
}
#Override
public void onAdOpened() {
// Code to be executed when an ad opens an overlay that
// covers the screen.
}
#Override
public void onAdLeftApplication() {
// Code to be executed when the user has left the app.
}
#Override
public void onAdClosed() {
// Code to be executed when when the user is about to return
// to the app after tapping on an ad.
}
});
Here's a sample from my xml.
<com.google.android.gms.ads.AdView
android:id="#+id/adView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignParentStart="true"
app:adSize="SMART_BANNER"
app:adUnitId="banner_ad_unit_id_main"
tools:layout_editor_absoluteX="0dp"
tools:layout_editor_absoluteY="457dp">
</com.google.android.gms.ads.AdView>
Try this. As per Google Documentation,
Note: If an app tries to load a banner that's too big for its layout,
the SDK won't display it. Instead, an error message will be written to
the log.
Make sure you are giving enough space for the ad to show up(which is 320x50 dp). You can try hard-code the value for testing.
Can you also check the log and check the internet permissions in AndroidManifest.xml
I published my Application few days ago in Play Store, everything works perfectly, the Ads (Banner & Interstitial) were appearing as expected. But today, i discovered that the Interstitial Ads are appearing but not Banner ads. This is my first application and I don't know what may be the problem.
Sometimes ads are not shown due to lack of ads in google's ad inventory. You can check ad request response code to be sure.
mAdView.setAdListener(new AdListener() {
#Override
public void onAdLoaded() {
// Code to be executed when an ad finishes loading.
}
#Override
public void onAdFailedToLoad(int errorCode) {
// Code to be executed when an ad request fails.
Log.d("ADMOB_ERROR_CODE", "admob error code: " + errorCode);
}
#Override
public void onAdOpened() {
// Code to be executed when an ad opens an overlay that
// covers the screen.
}
#Override
public void onAdLeftApplication() {
// Code to be executed when the user has left the app.
}
#Override
public void onAdClosed() {
// Code to be executed when when the user is about to return
// to the app after tapping on an ad.
}
});
Further Reading: https://developers.google.com/admob/android/banner
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