I have the following problem with AdMob.
My app is published on Google Play, Opera Mobile Store and GetJar. It has 30 downloads from Opera Store, 120 from GetJar but AdMob is showing and counting only the impressions and clicks made from Google Play downloads. The problem is only with the other app stores 150 downloads for the last 1 week and not a single impression counted.
Any idea is something wrong with my code or it's something else?
I have removed the addTestDevice method calls.
private AdView adView;
#Override
protected void onCreate(Bundle savedInstanceState) {
...
adView = (AdView) this.findViewById(R.id.adView);
AdRequest adRequest = new AdRequest.Builder().build();
adView.loadAd(adRequest);
...
}
#Override
protected void onResume() {
...
if (adView != null) {
adView.resume();
}
super.onResume();
}
#Override
protected void onPause() {
if (adView != null) {
adView.pause();
}
super.onPause();
}
#Override
protected void onDestroy() {
super.onDestroy();
if (adView != null) {
adView.destroy();
}
}
<com.google.android.gms.ads.AdView
android:id="#id/adView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_alignParentBottom="true"
ads:adUnitId="ca-app-pub-*******/******"
ads:adSize="BANNER"/>
Related
I've been having this problem for weeks now. I finally narrowed it down to this : the AdView's visibility seems stuck to View.GONE, no matter how hard I try to set it to View.VISIBLE.
Here is my code :
public class AndroidLauncher extends AndroidApplication {
AdView bannerView;
RelativeLayout layout;
#Override
protected void onCreate (Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
AppLovinSdk.initializeSdk(this);
AppLovinPrivacySettings.setHasUserConsent(false, this);
MobileAds.initialize(this, "ca-app-pub-3940256099942544~3347511713"); // test APP ID
AndroidApplicationConfiguration config = new AndroidApplicationConfiguration();
game = new GameClass(this);
// // Create Layout that will call both Libgdx View and AdMod View
layout = new RelativeLayout(this);
// InitializeCommonRessources Libgdx View
requestWindowFeature(Window.FEATURE_NO_TITLE);
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
WindowManager.LayoutParams.FLAG_FULLSCREEN);
getWindow().clearFlags(WindowManager.LayoutParams.FLAG_FORCE_NOT_FULLSCREEN);
//
View gameView = initializeForView(game, config);
layout.addView(gameView);
// Tell Android to use this Layout
setContentView(layout);
setupBanner();
}
#Override
public void setupBanner() {
SetupAdMobBanner();
}
public void SetupAdMobBanner() {
System.out.println("Setup AdMob BANNER");
bannerView = new AdView(this);
bannerView.setVisibility(View.VISIBLE); // These are my desperate attempts to make the banner visible
System.out.println("visibility == " + bannerView.getVisibility()); // Always prints "visibility == 8" which is the value for View.GONE (View.VISIBLE is 0)
bannerView.setAdListener(new AdListener() {
#Override public void onAdOpened() {
System.out.println("BANNER AD OPENED");
Ressources.incrementBannerClicks();
}
#Override public void onAdClosed() {
System.out.println("BANNER AD CLOSED");
}
#Override public void onAdLeftApplication() {
System.out.println("BANNER LEFT APP");
}
#Override public void onAdFailedToLoad(int var1) {
System.out.println("BANNER FAILED TO LOAD : " + var1);
destroyAdMobBanner();
SetupAdMobBanner();
}
#Override public void onAdLoaded() {
System.out.println("BANNER LOADED");
bannerView.setVisibility(View.VISIBLE);
System.out.println("visibility == " + bannerView.getVisibility());
}
#Override public void onAdImpression() {
System.out.println("BANNER IMPRESSION");
}
});
bannerView.setVisibility(View.VISIBLE);
System.out.println("visibility == " + bannerView.getVisibility());
bannerView.setAdSize(AdSize.SMART_BANNER);
bannerView.setVisibility(View.VISIBLE);
System.out.println("visibility == " + bannerView.getVisibility());
bannerView.setAdUnitId("ca-app-pub-3940256099942544/6300978111"); // Test banners
bannerView.setVisibility(View.VISIBLE);
System.out.println("visibility == " + bannerView.getVisibility());
bannerView.setVisibility(View.VISIBLE);
System.out.println("visibility == " + bannerView.getVisibility());
RelativeLayout.LayoutParams adParams = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT);
adParams.addRule(RelativeLayout.ALIGN_TOP);
adParams.addRule(RelativeLayout.CENTER_HORIZONTAL);
layout.addView(bannerView, adParams);
bannerView.setVisibility(View.VISIBLE);
System.out.println("visibility == " + bannerView.getVisibility());
loadAdMobBanner();
bannerView.setVisibility(View.VISIBLE);
System.out.println("visibility == " + bannerView.getVisibility());
}
public void loadAdMobBanner() {
System.out.println("BANNER loadAd(adRequest)");
AdRequest adRequest = new AdRequest.Builder().addTestDevice("36429449DBD95B918CFD96E610995AE6").build(); // Test ads for my Honor 7x
bannerView.loadAd(adRequest);
}
public void destroyAdMobBanner() {
if (bannerView == null) return;
System.out.println("Destroying BANNER");
layout.removeView(bannerView);
bannerView.destroy();
bannerView = null;
}
}
As you can see, I am desperately trying to set the visibility to VISIBLE, but it always prints visibility == 8 right after (8 being the value for GONE, VISIBLE's being 0).
The weirdest thing is that sometimes, the banner will actually show up.
It might work for a day or two, and suddenly not work for a day or two, the banner won't show even though the AdListener says it's been successfully loaded, without me doing a damn thing.
The problem occurs on a physical device, a Honor 7X, with both live ads and test ads.
You have to add test device to adView. You will find test device id in logcat. If you don't add test device while development and click on ads for many times ads will stop working for that ad id.
Add only this code not set visibility
try this code
private AdView mAdView;
mAdView = (AdView) findViewById(R.id.adView);
AdRequest adRequest = new AdRequest.Builder()
.addTestDevice(AdRequest.DEVICE_ID_EMULATOR)
// Check the LogCat to get your test device ID
.addTestDevice("36429449DBD95B918CFD96E610995AE6")
.build();
mAdView.loadAd(adRequest);
#Override
protected void onResume() {
super.onResume();
if (mAdView != null) {
mAdView.resume();
}
}
#Override
public void onDestroy() {
if (mAdView != null) {
mAdView.destroy();
}
super.onDestroy();
}
#Override
protected void onPause() {
if (mAdView != null) {
mAdView.pause();
}
super.onPause();
}
I hope that can help you!
Thank You.
I follow the official documentation:
https://developers.google.com/admob/android/banner
But I can't find a mistake in my code. I tried to change
AdRequest adRequest = new AdRequest.Builder().build();
For the test AdRequest but it's still not working.
This is my source code:
https://github.com/sssolammm/HelloWordTestBanner
public class MainActivity extends AppCompatActivity {
private AdView mAdView;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
MobileAds.initialize(this, "ca-app-pub-5676983055182772~7282283135");
mAdView = (AdView) findViewById(R.id.ad_View);
AdRequest adRequest = new AdRequest.Builder().build();
mAdView.loadAd(adRequest);
mAdView.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.
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");
}
});
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.my, menu);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
/** Called when leaving the activity */
#Override
public void onPause() {
if (mAdView != null) {
mAdView.pause();
}
super.onPause();
}
/** Called when returning to the activity */
#Override
public void onResume() {
super.onResume();
if (mAdView != null) {
mAdView.resume();
}
}
/** Called before the activity is destroyed */
#Override
public void onDestroy() {
if (mAdView != null) {
mAdView.destroy();
}
super.onDestroy();
}
}
Xml layout
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
xmlns:ads="http://schemas.android.com/apk/res-auto"
android:id="#+id/container"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginTop="#dimen/activity_vertical_margin"
tools:context=".MainActivity"
tools:ignore="MergeRootFrame">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="#dimen/activity_horizontal_margin"
android:layout_marginStart="#dimen/activity_horizontal_margin"
android:text="#string/hello_world" />
<com.google.android.gms.ads.AdView
android:id="#+id/ad_View"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_alignParentBottom="true"
ads:adSize="BANNER"
ads:adUnitId="#string/banner_ad_unit_id">
</com.google.android.gms.ads.AdView>
</RelativeLayout>
You should add your test device id to builder.
If it works with test id, it will be working on production.
AdRequest request = new AdRequest.Builder()
.addTestDevice(AdRequest.DEVICE_ID_EMULATOR) // All emulators
.addTestDevice("your id") // Put here your test device id
.build();
For test device id, go in Logcat after run app, put in Verbose put in the search field AdRequest, so the id device shows down.
Well, sometimes ad is not shown...then i uninstall my application and change build variants from debug to release.
After then i install my app again...banner ad will be shown successfully.
even if banner ad is not showing..then i check the log info...and get the test device id..add this test device id to AdRequest object...and i am sure banner ad will be shown.
adRequest.addTestDevice("your id")
I integrate google admob ads but it's displaying an error as,
W/Ads: There was a problem getting an ad response. ErrorCode: 0
08-09 16:05:43.933 24002-24002/com.locationtracking W/Ads: Failed to load ad: 0
I created a new ad id and copied the interstital ad unit id and app id but it's not working.
But when I used testing interstial id, it's displaying an ad. But when i copy and use the interstital id created, it's not displaying the ads. For past few days, I am struggling in this. I created new account and checked. Still same error.
MobileAds.initialize(this, "ca-app-pub-9172823052987868~3234462570");
mInterstitialAd = new InterstitialAd(this);
mInterstitialAd.setAdUnitId("ca-app-pub-9172823052987868/8576265071");
AdRequest adRequestInterstitial = new AdRequest.Builder().addTestDevice("deviceid").build();
mInterstitialAd.loadAd(adRequestInterstitial);
mInterstitialAd.setAdListener(new AdListener() {
#Override
public void onAdClosed() {
}
#Override
public void onAdLoaded() {
mAdIsLoading = false;
showInterstitial();
}
#Override
public void onAdFailedToLoad(int errorCode) {
mAdIsLoading = false;
}
});
}
private void showInterstitial() {
if (mInterstitialAd.isLoaded()) {
mInterstitialAd.show();
AdRequest adRequest = new AdRequest.Builder().addTestDevice("deviceid").build();
mInterstitialAd.loadAd(adRequest);
}
}
dependencies {
compile fileTree(dir: 'libs', include: ['*.jar'])
compile 'com.google.android.gms:play-services-ads:9.2.1'
compile 'com.google.android.gms:play-services-analytics:9.2.1'
}
Common Error! There is no fault in your code. It's all good.
You just need to wait a little. Your ad id is newly created so it will take some time to fetch ads from google servers. You can verify this by adding banner/interstitial ad id you creating for earlier applications and you'll see that they work. So give it some time and it will work soon. At least for me, it happens all the time.
W/Ads: There was a problem getting an ad response. ErrorCode: 0 08-09 16:05:43.933 24002-24002/com.locationtracking W/Ads: Failed to load ad: 0
remove addTestDevice("deviceid"); from your code display real ads
AdRequest adRequestInterstitial = new AdRequest.Builder().addTestDevice("deviceid").build();
to
AdRequest adRequestInterstitial = new AdRequest.Builder().build();
try bellow code its running well:
public class MainActivity extends AppCompatActivity {
InterstitialAd mInterstitialAd;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
////////////////////////// banner/////////////////////////
AdView mAdView = (AdView) findViewById(R.id.adView);
//AdRequest adRequest = new AdRequest.Builder().addTestDevice("7339C0A9091FA2D6AA9A2BF29077B5EA").build();
AdRequest adRequest = new AdRequest.Builder().build();
mAdView.loadAd(adRequest);
///////////////////////////////////////////////////////////
mInterstitialAd = new InterstitialAd(this);
mInterstitialAd.setAdUnitId(getResources().getString(R.string.interstitial_unit_id));
mInterstitialAd.setAdListener(new AdListener() {
#Override
public void onAdClosed() {
loadAdd();
}
});
loadAdd();
}
#Override
protected void onStart() {
super.onStart();
showInterstitial();
}
#Override
protected void onStop() {
super.onStop();
showInterstitial();
}
//////////////////////////////////add Interstitial section//////////////////////////////////////////////
private void showInterstitial() {
// Show the ad if it's ready. Otherwise toast and restart the game.
if (mInterstitialAd != null && mInterstitialAd.isLoaded()) {
mInterstitialAd.show();
} else {
//Toast.makeText(this, "Ad did not load", Toast.LENGTH_SHORT).show();
loadAdd();
}
}
private void loadAdd() {
// Request a new ad if one isn't already loaded, hide the button, and kick off the timer.
if (!mInterstitialAd.isLoading() && !mInterstitialAd.isLoaded()) {
//AdRequest adRequest = new AdRequest.Builder().addTestDevice("7339C0A9091FA2D6AA9A2BF29077B5EA").build();
AdRequest adRequest = new AdRequest.Builder().build();
mInterstitialAd.loadAd(adRequest);
}
}
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
}
compile 'com.google.android.gms:play-services-ads:8.3.0'
You can not display real ad in the test device, so you should remove the test device code when you upload your apk=> change your code as this
AdRequest adRequestInterstitial = new AdRequest.Builder().build();
I new in AdMob. At first I'm trying to create a test app with different types of ad.
I use real ad_unit_id and nexus 4 for testing.
I use 1 layout xml for all activity:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:ads="http://schemas.android.com/apk/lib/com.google.ads"
android:id="#+id/empty_layout"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
</LinearLayout>
When i create simple activity with banner ad it's work fine. Activity code:
public class AdBannerActivity extends Activity {
private AdView adView;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.ad_empty);
adView = new AdView(this, AdSize.SMART_BANNER, getString(R.string.adUnitId));
adView.setAdListener(this);
LinearLayout layout = (LinearLayout) findViewById(R.id.empty_layout);
layout.addView(adView);
adView.loadAd(new AdRequest());
}
#Override
public void onDestroy() {
if (adView != null) {
adView.destroy();
}
super.onDestroy();
}
}
When i try to create activity with full screen ad, i have error message:
Ad request successful, but no ad returned due to lack of ad inventory.
Activity code:
public class AdInterstitialActivity extends Activity implements AdListener {
private InterstitialAd interstitial;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.ad_empty);
interstitial = new InterstitialAd(this, getString(R.string.adUnitId));
interstitial.setAdListener(this);
AdRequest adRequest = new AdRequest();
interstitial.loadAd(adRequest);
}
#Override
public void onReceiveAd(Ad ad) {
if (ad == interstitial) {
interstitial.show();
}
}
#Override
public void onDismissScreen(Ad ad) {
}
#Override
public void onFailedToReceiveAd(Ad ad, ErrorCode error) {
Log.e("AdTest", "Error code: " + error);
}
#Override
public void onLeaveApplication(Ad ad) {
}
#Override
public void onPresentScreen(Ad ad) {
}
}
What am I doing wrong?
How to solve this problem?
Solution: Create new ad_unit_id for interstitial ads in AdMob account.
If your have that error, then your code is correct. Admob just doesn't have any ads to display for your app (your country, your type of ads are determinants).
If you want to test the behavior of your ads in test mode, you should take a look at this link :
https://developers.google.com/mobile-ads-sdk/docs/admob/additional-controls#testmode
EDIT : If you have just created your Admob account, it may take some times and some requests to deliver the first ads.
I saw somewhere that people had this error too by using custom size for banner, so be sure to check :
https://developers.google.com/mobile-ads-sdk/docs/admob/intermediate#play
I have read few articles on trying to stop adView when the app is hidden/minimised but this crashes my app.
This is my code, adView.LoadAd... and adView.stopLoading both crashes the app on startup.
public class MainActivity extends Activity implements OnItemSelectedListener {
#Override
protected void onResume() {
super.onResume();
if (AdViewStarted = true) {
adView.loadAd(new AdRequest());
}
}
#Override
protected void onPause() {
super.onPause();
if (AdViewStarted = true) {
adView.destroy();
}
}
[...]
public class AdMob extends Activity {
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
adView = new AdView(this, AdSize.BANNER,"12345678901234567890");
LinearLayout layout = (LinearLayout) findViewById(R.id.adView);
layout.addView(adView);
adView.loadAd(new AdRequest());
AdViewStarted = true;
}
#Override
public void onDestroy() {
if (adView != null) {
adView.destroy();
}
super.onDestroy();
}
}
}
Thanks in advance
Replace the if statements, you have to use two equals instead of one. Correct would be
if (AdViewStarted == true) {
adView.destroy();
}
or better
if (AdViewStarted) {
adView.destroy();
}
By the win, variable names are starting with a lowercase char.
Also, what are you trying in your onCreate?
This is correct (I think, if not, show me the layout xml file and LogCat):
LinearLayout adView = (LinearLayout) findViewById(R.id.adView);
adView.loadAd(new AdRequest());
AdViewStarted = true;