I run my app with android studio, but my add doesn't appear...
Every time I run my app no ads appear and I get this error message in Logcat:
"There was a problem getting an ad response. ErrorCode: 0 Failed to
load ad:0"
Here is my MainActivity.java
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
MobileAds.initialize(MainActivity.this, "ca-app-pub-4760206671218452~8890405785");
mAdview = findViewById(R.id.adView);
AdRequest adRequest = new AdRequest.Builder().addTestDevice(AdRequest.DEVICE_ID_EMULATOR).build();
mAdview.loadAd(adRequest);
mAdview.setAdListener(new AdListener() {
#Override
public void onAdLoaded() {
Toast.makeText(getApplicationContext(),"RKAd Loaded",Toast.LENGTH_SHORT).show();
// Code to be executed when an ad finishes loading.
}
#Override
public void onAdFailedToLoad(int errorCode) {
Toast.makeText(getApplicationContext(),"Ad Failed to load",Toast.LENGTH_SHORT).show();
// Code to be executed when an ad request fails.
}
#Override
public void onAdOpened() {
Toast.makeText(getApplicationContext(),"Ad Opened",Toast.LENGTH_SHORT).show();
// Code to be executed when an ad opens an overlay that
// covers the screen.
}
#Override
public void onAdLeftApplication() {
Toast.makeText(getApplicationContext(),"Ad Left Application",Toast.LENGTH_SHORT).show();
// Code to be executed when the user has left the app.
}
#Override
public void onAdClosed() {
Toast.makeText(getApplicationContext(),"RKAd Closed",Toast.LENGTH_SHORT).show();
// Code to be executed when when the user is about to return
// to the app after tapping on an ad.
}
});
Here my activity_main.xml
<com.google.android.gms.ads.AdView
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="https://schemas.android.com/tools"
xmlns:ads="http://schemas.android.com/apk/res-auto"
android:id="#+id/adView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_alignParentBottom="true"
ads:adSize="SMART_BANNER"
android:layout_gravity="bottom"
ads:adUnitId="ca-app-pub-4760206671218452/1505195192">
</com.google.android.gms.ads.AdView>
I have on the screen Ad Failed to load. I created my Admob account yesterday, but the ad doesn't display :(
1. If your account is new please wait 2-3 Hours, it will automatically start showing ads.
About this issue google say:
"It could be that you have only recently created a new Ad Unit ID and
requesting for live ads. It could take a few hours for ads to start
getting served if that is that case. If you are receiving test ads
then your implementation is fine. Just wait a few hours and see if you
are able to receive live ads then. If not, can send us your Ad Unit ID
for us to look into."
So you have to wait for a few hours.
Check Out Referance
2. After that if still does not started showing ads, probably you forgot to setup payment settings. So You have to complete that.
i use this for testing an ad
AdView adView = (AdView) findViewById(R.id.adView);
AdRequest adRequest = new AdRequest.Builder()
.setRequestAgent("android_studio:ad_template").build();
adView.loadAd(adRequest);
and in the ads xml (banner test id)
ads:adUnitId="ca-app-pub-3940256099942544/6300978111"
if you want to test your own ad you have to change the id and the tag on setRequestAgent
.setRequestAgent("myapp").build();
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'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
As we know, google requires us to use test device and test ad unit id, when we develop the app. However, I want to know that if there exists anyway that I can
see the real ad, because I am afraid that no ad will show after I change the code and ad id before release. I have successfully seen test ad, and then I changed the code and ad id, and then submit my app to beta testing, but the tester said that no ad was shown, is it normal, or I have made some mistake in my code or ad unit id.
Thank you for your help!
Below is my ad-related code, and I have changed the ad id
mAdView = (AdView) findViewById(R.id.adView);
AdRequest adRequest = new AdRequest.Builder().build();
mAdView.loadAd(adRequest);
You can use actual ad unit id even for development, but your test devices should be added to the list of TEST DEVICES when you build your ad request.
If it's a valid ad unit id that you are using, you can be sure that ads will appear when you publish your apps. Also, you can check if you are getting hits for your ad unit in Adsense Dashboard.
If you want to be dead sure, you can just try to install the apk on another device, test it, and then publish it when you see the ads.
You can use AdListener and monitor why it is not showing ads . Remember if you have created fresh ad units then it will take some time or few hours to arrange live ads for it . If test ads are showing and you have valid ad unit id and also your app has not violated any policy then you are good to go (You will be emailed if your app have a policy issue) . Ads will be shown when availabe.
AdView adView = (AdView) findViewById(R.id.adView);
AdRequest adRequest1 = new AdRequest.Builder().build();
adView.loadAd(adRequest1);
adView.setAdListener(new AdListener() {
#Override
public void onAdLoaded() {
// Code to be executed when an ad finishes loading.
Log.i("Ads", "onAdLoaded");
}
#Override
public void onAdFailedToLoad(int errorCode) {
// Code to be executed when an ad request fails.
switch (errorCode){
case AdRequest.ERROR_CODE_INTERNAL_ERROR:
Toast.makeText(PlayListsActivity.this,"onAdFailedToLoad banner ERROR_CODE_INTERNAL_ERROR",Toast.LENGTH_SHORT).show();
break;
case AdRequest.ERROR_CODE_INVALID_REQUEST:
Toast.makeText(PlayListsActivity.this,"onAdFailedToLoad banner ERROR_CODE_INVALID_REQUEST",Toast.LENGTH_SHORT).show();
break;
case AdRequest.ERROR_CODE_NETWORK_ERROR:
Toast.makeText(PlayListsActivity.this,"onAdFailedToLoad banner ERROR_CODE_NETWORK_ERROR",Toast.LENGTH_SHORT).show();
break;
case AdRequest.ERROR_CODE_NO_FILL:
Toast.makeText(PlayListsActivity.this,"onAdFailedToLoad banner ERROR_CODE_NO_FILL",Toast.LENGTH_SHORT).show();
break;
}
Log.i("Ads", "onAdFailedToLoad");
}
#Override
public void onAdOpened() {
// Code to be executed when an ad opens an overlay that
// covers the screen.
Log.i("Ads", "onAdOpened");
}
#Override
public void onAdLeftApplication() {
// Code to be executed when the user has left the app.
Log.i("Ads", "onAdLeftApplication");
}
#Override
public void onAdClosed() {
// Code to be executed when when the user is about to return
// to the app after tapping on an ad.
Log.i("Ads", "onAdClosed");
}
});
My doubt is not weather its ok to click on live ads during development.I did all the formalities to get my admob ad working . Then i saw that we should only use test add during development . so i used this code:
AdRequest request = new AdRequest.Builder()
.addTestDevice(AdRequest.DEVICE_ID_EMULATOR) // All emulators
.addTestDevice("AC98C820A50B4AD8A2106EDE96FB87D4") // An example device ID
.build();
It worked fine. But then i change i back to the live ad code fearing that i would forget to do so later:
AdView mAdView = (AdView) findViewById(R.id.adView);
AdRequest adRequest = new AdRequest.Builder().build();
mAdView.loadAd(adRequest);
But now the ad wont appear during test run. Is that normal?
will the ad work when i finally launch the app?
I dont see anything wrong with your code, but something similar happened to me when implementing admob too.
I didnt have in count that the ad must be loaded before showing it, and that the loading may take some time. If you try to show an ad that its not loaded, a black activity might be shown.
You should implement some waiting time or a listener in order to be sure that the add is loaded before showing it.
public InterstitialAd interstitialAd;
[...]
public void setNewInterstitialRequest()
{
interstitialAd = new InterstitialAd(CurrActivity.this);
interstitialAd.setAdUnitId("ca-app-pub-***************************");
AdRequest adRequest = new AdRequest.Builder()
//.addTestDevice("ZY22247DJV")
.build();
// Begin loading your interstitial.
interstitialAd.loadAd(adRequest);
//add listener so you know is fully loaded
interstitialAd.setAdListener(new AdListener() {
#Override
public void onAdLoaded() {
[...] // <- code you want to execute as soon as the ad is loaded
}
});
}
public void displayAd() {
try {
//verify is loaded before showing a black screen
if (interstitialAd.isLoaded()) {
interstitialAd.show();
}
//else
// showToast("Not loaded");
}
catch (Exception ex){}
}
So taking this in mind, if you want for the ad to be shown inmediately when something happens, you should have it loaded before that event occurs.
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.