AdMob banner's visibility stuck to View.GONE - android

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.

Related

Admob interstitial ad latency issues when loading ad

I am trying to implement interstitial ad in a fragment, when button is clicked the fragment doesn't open other activity , but loads data in same fragment so i can't use the they way people say interstisial ad should be used when navigating between activities. instead i am using the ad for only once when button is clicked .it works but takes sometime .how to deal with it?
it only works if user pressed the button after few seconds of launching the app.
public View onCreateView(#NonNull LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
root = inflater.inflate(R.layout.fragment_home, container, false);
Button btn=root.findViewById(R.id.btn);
initAds();
btn.setOnClickListener(this);
return root;
}
#Override
public void onClick(View view){
initAdsCallBack();
}
private void initAds(){
MobileAds.initialize(requireActivity(), new OnInitializationCompleteListener() {
#Override
public void onInitializationComplete(#NonNull InitializationStatus initializationStatus) {
createNonPersonalisedAd();
}
});
}
}
private void createNonPersonalisedAd() {
Bundle networkExtrasBundle = new Bundle();
networkExtrasBundle.putInt("rdp", 1);
AdManagerAdRequest adRequest = (AdManagerAdRequest) new AdManagerAdRequest.Builder()
.addNetworkExtrasBundle(AdMobAdapter.class, networkExtrasBundle)
.build();
createInterstitialAd(adRequest);
}
private void createInterstitialAd(AdRequest adRequest){
AdManagerInterstitialAd.load(requireActivity(),getResources().getString(R.string.str_iterstitial), (AdManagerAdRequest) adRequest,
new AdManagerInterstitialAdLoadCallback() {
#Override
public void onAdLoaded(#NonNull AdManagerInterstitialAd interstitialAd) {
// The mAdManagerInterstitialAd reference will be null until
// an ad is loaded.
mAdManagerInterstitialAd = interstitialAd;
Log.i(TAG, "onAdLoaded");
///// best place to callback is here coz its successfully loaded here
mAdManagerInterstitialAd.setFullScreenContentCallback(new FullScreenContentCallback(){
#Override
public void onAdDismissedFullScreenContent() {
// Called when fullscreen content is dismissed.
Log.d("TAG", "The ad was dismissed.");
createInterstitialAd(adRequest);
}
#Override
public void onAdFailedToShowFullScreenContent(AdError adError) {
// Called when fullscreen content failed to show.
Log.d("TAG", "The ad failed to show.");
}
#Override
public void onAdShowedFullScreenContent() {
// Called when fullscreen content is shown.
// Make sure to set your reference to null so you don't
// show it a second time.
mAdManagerInterstitialAd = null;
Log.d("TAG", "The ad was shown.");
}
});
}
#Override
public void onAdFailedToLoad(#NonNull LoadAdError loadAdError) {
// Handle the error
Log.i(TAG, loadAdError.getMessage());
mAdManagerInterstitialAd = null;
}
});
}
private void initAdsCallBack(){
if (mAdManagerInterstitialAd != null) {
mAdManagerInterstitialAd.show(requireActivity());
debugToast("ad shown");
} else {
Log.e("Tad didn't show");
}
}
Consider pre-loading your interstitial ads to reduce latency when displaying them to your users. For more information about pre-loading your interstitial ads refer to the AdMob Interstitial Ad developer guidelines for apps developed for Android

How to refresh native ads in specific time using Admob?

I'm working in my Android application and i integrated native ads from Google Admob and it works well as shown in the picture below. But my problem is i want it refresh itself automatically. And this is my code used to refresh the ad. Any ideas? Thanks.
private void refreshAd() {
AdLoader.Builder builder = new AdLoader.Builder(this, ADMOB_AD_UNIT_ID);
builder.forUnifiedNativeAd(new UnifiedNativeAd.OnUnifiedNativeAdLoadedListener() {
// OnUnifiedNativeAdLoadedListener implementation.
#Override
public void onUnifiedNativeAdLoaded(UnifiedNativeAd unifiedNativeAd) {
// You must call destroy on old ads when you are done with them,
// otherwise you will have a memory leak.
if (nativeAd != null) {
nativeAd.destroy();
}
nativeAd = unifiedNativeAd;
FrameLayout frameLayout =
findViewById(R.id.fl_adplaceholder);
UnifiedNativeAdView adView = (UnifiedNativeAdView) getLayoutInflater()
.inflate(R.layout.activity_ads, null);
populateUnifiedNativeAdView(unifiedNativeAd, adView);
frameLayout.removeAllViews();
frameLayout.addView(adView);
}
});
NativeAdOptions adOptions = new NativeAdOptions.Builder()
.build();
builder.withNativeAdOptions(adOptions);
AdLoader adLoader = builder.withAdListener(new AdListener() {
#Override
public void onAdFailedToLoad(int errorCode) {
Toast.makeText(MainActivity.this, "Failed to load native ad: "
+ errorCode, Toast.LENGTH_SHORT).show();
}
}).build();
adLoader.loadAd(new AdRequest.Builder().build());
}

Revmob in Libgdx

Hello I am trying to integrated Banner Ad using RevMob in Libgdx. But it is not displaying for some reason.
I am using the following code.
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// setContentView(R.layout.activity_game_new);
AndroidApplicationConfiguration cfg = new AndroidApplicationConfiguration();
// cfg.useGL20 = false;
final RelativeLayout gameLayout = new RelativeLayout(this);
RevMobIntegration revmob = new RevMobIntegration(this);
RelativeLayout bannerLayout = new RelativeLayout(this);
RelativeLayout.LayoutParams adParams = new RelativeLayout.LayoutParams(
RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT);
adParams.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM);
adParams.addRule(RelativeLayout.CENTER_VERTICAL);
bannerLayout.setLayoutParams(adParams);
game = new MyGdxGame(GameActivity.this, revmob);
game.setRedirectionListener(this);
View gameView = initializeForView(game, cfg);
requestWindowFeature(android.view.Window.FEATURE_NO_TITLE);
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
WindowManager.LayoutParams.FLAG_FULLSCREEN);
getWindow().clearFlags(WindowManager.LayoutParams.FLAG_FORCE_NOT_FULLSCREEN);
// Add the libgdx view
gameLayout.addView(gameView);
// gameLayout.addView(bannerLayout);
Log.d("RevMob", "Checking BannerAd");
if (revmob.getBannerAd() != null) {
Log.d("RevMob", "Displaying Called");
gameLayout.addView(revmob.getBannerAd());
}
// Hook it all up
setContentView(gameLayout);
this.onPause();
this.onResume();
gameLayout.refreshDrawableState();
initChartboost();
// startRevMobSession();
}
Here is the RevMobIntegration class :
public class RevMobIntegration implements RevmobAdInterface {
private static final String APPLICATION_ID = "YourAdmobAppIDHere";
// Set this to false when creating the version for the store.
private static final boolean DEBUG = true;
private RevMobAdsListener listener;
private RevMobFullscreen fullscreenAd;
private RevMobBanner bannerAd;
private Activity application;
private RevMob revmob;
public RevMobIntegration(Activity _application) {
this.application = _application;
startRevMobSession();
}
public void startRevMobSession() {
//RevMob's Start Session method:
revmob = RevMob.startWithListener(application, new RevMobAdsListener() {
#Override
public void onRevMobSessionStarted() {
loadBanner(); // Cache the banner once the session is started
Log.i("RevMob", "Session Started");
}
#Override
public void onRevMobSessionNotStarted(String message) {
//If the session Fails to start, no ads can be displayed.
Log.i("RevMob", "Session Failed to Start");
}
}, application.getString(R.string.rev_mob_app_id));
}
//RevMob
public void loadBanner() {
bannerAd = revmob.preLoadBanner(application, new RevMobAdsListener() {
#Override
public void onRevMobAdReceived() {
showBannerAd(true);
Log.i("RevMob", "Banner Ready to be Displayed"); //At this point,
the banner is ready to be displayed.
}
#Override
public void onRevMobAdNotReceived(String message) {
Log.i("RevMob", "Banner Not Failed to Load");
}
#Override
public void onRevMobAdDisplayed() {
Log.i("RevMob", "Banner Displayed");
}
});
}
#Override
public void showBannerAd(boolean show) {
if(show) {
Log.i("RevMob", "Showing");
if(bannerAd == null) {
startRevMobSession();
} else {
Log.i("RevMob", "Banner Displayed");
bannerAd.show();
}
} else {
bannerAd.hide();
}
}
public RevMobBanner getBannerAd() { return bannerAd; }
}
I have integrated the RevMob in my Activities and it is working fine. But for the Game Screen the ad is initializing but not displaying.
Any suggestions?
It seems revmob.getBannerAd() return null because bannerAd object created when loadBanner(); called. RevMob take some time to start it's session.
if (revmob.getBannerAd() != null) {
Log.d("RevMob", "Displaying Called");
gameLayout.addView(revmob.getBannerAd());
}
You can check this repo for clarification also you can take a look of this class.

Admob + Andengine showing ad only on second request

the issue is: the ad doesn't show on first request, but when it makes the second request, it shows right. About 2 seconds before it make the second request, the ad of the first request shows up. Any ideas?
Thanks,
EDIT: I changed the gravity to TOP, and now it shows on the first time, but is showing just the top half of the ad, bizarre, any ideas?
#Override
protected void onSetContentView() {
if(adView != null){
return;
}
final FrameLayout frameLayout = new FrameLayout(this);
final FrameLayout.LayoutParams frameLayoutLayoutParams =
new FrameLayout.LayoutParams(FrameLayout.LayoutParams.MATCH_PARENT,
FrameLayout.LayoutParams.MATCH_PARENT);
final FrameLayout.LayoutParams adViewLayoutParams =
new FrameLayout.LayoutParams(FrameLayout.LayoutParams.MATCH_PARENT,
FrameLayout.LayoutParams.WRAP_CONTENT,
Gravity.CENTER_HORIZONTAL|Gravity.BOTTOM);
adView = new AdView(this);
adView.setAdSize(AdSize.BANNER);
adView.setAdUnitId(getResources().getString(R.string.ad_unit_id));
adView.setAdListener(new ToastAdListener(this));
adView.loadAd(new AdRequest.Builder().build());
this.mRenderSurfaceView = new RenderSurfaceView(this);
mRenderSurfaceView.setRenderer(mEngine,this);
final android.widget.FrameLayout.LayoutParams surfaceViewLayoutParams =
new FrameLayout.LayoutParams(super.createSurfaceViewLayoutParams());
frameLayout.addView(this.mRenderSurfaceView, surfaceViewLayoutParams);
frameLayout.addView(adView, adViewLayoutParams);
this.setContentView(frameLayout, frameLayoutLayoutParams);
}
#Override
protected void onPause() {
if(adView!=null){
adView.pause();
}
super.onPause();
}
#Override
protected void onResume() {
super.onResume();
if(adView!=null){
adView.resume();
}
}
#Override
protected void onDestroy() {
if(adView!=null){
adView.destroy();
}
super.onDestroy();
}
The problem was solved for me by adding the line
adView.setBackgroundColor(android.graphics.Color.TRANSPARENT);
The way I have done is as follows:
In the method which changes the visibility, I first check is the ad was loaded. If it was not, I don't change the visibility
Set an AdListener for the AdView. in the onReceivedAd(), I check the condition to hide it - if it should be hidden, I hide.
Works fine this way.
I have got the same problem, I solved this way:
Advertisement class:
public class Advertisement {
private AdView adView;
private final Handler adsHandler = new Handler();
public Advertisement(final Activity activity) {
adView = (AdView)activity.findViewById(R.id.adView);
}
//show the ads.
private void showAds () {
adView.loadAd(new AdRequest.Builder().build());
adView.setVisibility(android.view.View.VISIBLE);
adView.setEnabled(true);
}
//hide ads.
private void unshowAds () {
adView.loadAd(new AdRequest.Builder().build());
adView.setVisibility(android.view.View.INVISIBLE);
adView.setEnabled(false);
}
final Runnable unshowAdsRunnable = new Runnable() {
public void run() {
unshowAds();
}
};
final Runnable showAdsRunnable = new Runnable() {
public void run() {
showAds();
}
};
public void showAdvertisement() {
adsHandler.post(showAdsRunnable);
}
public void hideAdvertisement() {
adsHandler.post(unshowAdsRunnable);
}
}
In code:
...
public static Advertisement advertisement = new Advertisement(this);
...
public static void showAd()
{
if (advertisement != null)
advertisement.showAdvertisement();
}
public static void hideAd()
{
if (gangsterAdvertisement != null)
advertisement.hideAdvertisement();
}

AdMob - Disable request onPause()

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;

Categories

Resources