Here is my Activity class
public class MainActivity extends Activity {
private InterstitialAd interstitial;
#Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// Prepare the Interstitial Ad
interstitial = new InterstitialAd(MainActivity.this);
// Insert the Ad Unit ID
interstitial.setAdUnitId("Ad-ID");
//Locate the Banner Ad in activity_main.xml
AdView adView = (AdView)findViewById(R.id.adView);
// Request for Ads
AdRequest.Builder adRequestBuilder=new AdRequest.Builder();
// Add a test device to show Test Ads
adRequestBuilder .addTestDevice(AdRequest.DEVICE_ID_EMULATOR);
adRequestBuilder.addTestDevice("abcd").build();
// Load ads into Banner Ads
//adView.setAdUnitId("Ad-ID");
adView.loadAd(adRequestBuilder.build());
interstitial.setAdListener(new AdListener() {
public void onAdLoaded() {
// Call displayInterstitial() function
displayInterstitial();
}
});
}//end of onCreate
protected void displayInterstitial() {
// TODO Auto-generated method stub
if (interstitial.isLoaded()) {
interstitial.show();
}
}
This is my simple XML Layout
<com.google.android.gms.ads.AdView
android:id="#+id/adView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
ads:adSize="BANNER"
ads:adUnitId="Ad-ID" />
I tried running the application there is no error shown and I am not able to seen any sample ad's in my layout when I run it.
Please let me know where was I mistaken.
You need to replace your own Ad Unit Id in your code and xml file.
adRequestBuilder.addTestDevice("abcd").build(); Here abcd is not a vaild device id. If you are running your app on a real device then in Logcat you will find its device id.
You will have to put that id here in spite of abcd.
In Logcat you will get a line saying,
To get test ads on this device, call adRequest.addTestDevice("SOME_ID_HERE");
You will have to put this id shown here in your code.
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
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();
I am developing and android application that needs to show Facebook Banner Ads, for that i have successfully generated Placement id and implemented the code suggested by Facebook developers website. But the problem is that ads are not showing. On debugging i find out this No Fill Error. I have searched this error on google but could not able to solve it. Any help will be appreciated. Here is my code :
AdView adView;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
// Instantiate an AdView view
adView = new AdView(this, PLACEMENT_ID, AdSize.BANNER_HEIGHT_50);
// Find the main layout of your activity
LinearLayout layout = (LinearLayout)findViewById(R.id.activityLayout);
// Add the ad view to your activity layout
layout.addView(adView);
adView.setAdListener(new AdListener() {
#Override
public void onError(Ad ad, AdError error) {
// Ad failed to load.
// Add code to hide the ad's view
}
#Override
public void onAdLoaded(Ad ad) {
// Ad was loaded
// Add code to show the ad's view
}
#Override
public void onAdClicked(Ad ad) {
// Use this function to detect when an ad was clicked.
}
});
// Request to load an ad
adView.loadAd();
}
After lots of searching i find out the solution why ads are not showing, actually my application is not live(in google play store) so try to login on your application with that Facebook account through which Placement id is generated. It will work.
Please find the code below adView was giving nullpointerexception.
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
// Prepare the Interstitial Ad
interstitial = new InterstitialAd(MainActivity.this);
// Insert the Ad Unit ID
interstitial.setAdUnitId("ca-app-pub-6117491189465387/7628536955");
//Locate the Banner Ad in activity_main.xml
AdView adView = (AdView)findViewById(R.id.adView);
// Request for Ads
AdRequest.Builder adRequestBuilder=new AdRequest.Builder();
// Add a test device to show Test Ads
adRequestBuilder .addTestDevice(AdRequest.DEVICE_ID_EMULATOR);
adRequestBuilder.addTestDevice("abcd").build();
// Load ads into Banner Ads
adView.setAdSize(AdSize.BANNER);
adView.setAdUnitId("ca-app-pub-6117491189465387/7628536955");
adView.loadAd(adRequestBuilder.build());
interstitial.setAdListener(new AdListener() {
public void onAdLoaded() {
// Call displayInterstitial() function
displayInterstitial();
}
});
I was struck since 2 day's please let me know where I was mistaken
I don't see your setContentView() method(). adview could be null because you either don't set the layout to use or you call setContentView() after initializing the adview. You should have this:
setContentView(R.layout.activity_main);
AdView adView = (AdView)findViewById(R.id.adView);
Please post the full content of the onCreate() method, your logcat stacktrace and layout file, if I'm wrong