0

I'm trying to show AdMob banner. The problem is that onAdLoaded() method called every time when I launch activity that contains my banner. Even in cases when device width is smaller then banner width. I have only Not enough space to show ad in my Logcat. Here is my code (partly):

private class BannerHolder extends RecyclerView.ViewHolder {

    private PublisherAdView adView;
    private ImageView closeAdd;
    private View divider;
    private RelativeLayout adLayout;
    private PublisherAdRequest adRequest = new PublisherAdRequest.Builder()
            .build();

    BannerHolder(View itemView) {
        super(itemView);
        adLayout = (RelativeLayout) itemView.findViewById(R.id.banner_place);
        divider = itemView.findViewById(R.id.divider);
        adView = new PublisherAdView(itemView.getContext());
        closeAdd = (ImageView) itemView.findViewById(R.id.close_add);

        adView.setAdListener(new AdListener() {
            @Override
            public void onAdClosed() {
                super.onAdClosed();
            }

            @Override
            public void onAdFailedToLoad(int i) {
                super.onAdFailedToLoad(i);
                adLayout.setVisibility(View.GONE);
                divider.setVisibility(View.GONE);
                adView.setVisibility(View.GONE);
                closeAdd.setVisibility(View.GONE);
            }

            @Override
            public void onAdLeftApplication() {
                super.onAdLeftApplication();
            }

            @Override
            public void onAdOpened() {
                super.onAdOpened();
            }

            @Override
            public void onAdLoaded() {
                super.onAdLoaded();
                adLayout.setVisibility(View.VISIBLE);
                divider.setVisibility(View.VISIBLE);
                adView.setVisibility(View.VISIBLE);
                closeAdd.setVisibility(View.VISIBLE);
            }
        });

        closeAdd.setOnClickListener(v -> {
            adLayout.setVisibility(View.GONE);
            divider.setVisibility(View.GONE);
            adView.setVisibility(View.GONE);
            closeAdd.setVisibility(View.GONE);

            isAddClosedByUser = true;

            itemView.getContext().getSharedPreferences("BANNER", Context.MODE_PRIVATE).edit().putBoolean("MAIN_SCREEN_BANNER_CLOSED", true).apply();
        });

        if (!isAddClosedByUser) {
            //Параметры View для баннера
            if (Build.VERSION.SDK_INT >= 19) {
                RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.MATCH_PARENT,
                        RelativeLayout.LayoutParams.WRAP_CONTENT));
                params.setMargins(0, (int) itemView.getContext().getResources().getDimension(R.dimen.banner_body_part_banner_padding_top),
                        0, (int) itemView.getContext().getResources().getDimension(R.dimen.banner_body_part_banner_padding_bottom));
                params.addRule(RelativeLayout.CENTER_IN_PARENT, RelativeLayout.TRUE);
                adView.setLayoutParams(params);
                adView.setVisibility(View.GONE);
            } else {
                ViewGroup.MarginLayoutParams params = new ViewGroup.MarginLayoutParams(new ViewGroup.LayoutParams(RelativeLayout.LayoutParams.MATCH_PARENT,
                        RelativeLayout.LayoutParams.WRAP_CONTENT));
                params.setMargins(0, (int) itemView.getContext().getResources().getDimension(R.dimen.banner_body_part_banner_padding_top),
                        0, (int) itemView.getContext().getResources().getDimension(R.dimen.banner_body_part_banner_padding_bottom));
                adView.setLayoutParams(params);
                adView.setVisibility(View.GONE);
            }

            //Задание параметров баннера
            if (adView.getAdUnitId() == null) {
                adView.setAdUnitId(tabBannerId);
            }
            if (adView.getAdSizes() == null) {
                adView.setAdSizes(AdSize.MEDIUM_RECTANGLE, new AdSize((int) tabBannerExtraWidth, (int) tabBannerExtraHeight), AdSize.SMART_BANNER);
            }

            if (!isAddClosedByUser) {
                adView.loadAd(adRequest);
                adLayout.removeView(adView);
                adLayout.addView(adView);

            }
        }

        if (!isAddClosedByUser) {
            adView.loadAd(adRequest);
        }
    }

    void update() {
        adView.loadAd(adRequest);
    }
}

My BannerHolder xml:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:ads="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="wrap_content">

<View
    android:id="@+id/divider"
    android:layout_width="match_parent"
    android:layout_height="1dp"
    android:background="#E5E5E5"
    android:visibility="gone" />

<RelativeLayout
    android:id="@+id/banner_place"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_alignParentBottom="true"
    android:layout_centerHorizontal="true"
    android:background="@color/banner_main_screen_background"
    android:visibility="gone">

    <ImageView
        android:id="@+id/close_add"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentRight="true"
        android:padding="@dimen/banner_body_close_add_padding"
        android:src="@drawable/cancel"
        android:visibility="gone" />
</RelativeLayout>
</RelativeLayout>

Is there any way to catch situation when banner is bigger then container prepared for it?

Anton Prokopov
  • 639
  • 6
  • 27
  • Post your xml file (for the holder), maybe the problem is in there. – Alejandro Cumpa Dec 19 '17 at 14:18
  • @Aiapaec I posted my xml. But I add adView to the layot programmatically, I dont think the problem is there – Anton Prokopov Dec 19 '17 at 14:28
  • Possible duplicate of [Not enough space to show ad (AdMob)](https://stackoverflow.com/questions/21848532/not-enough-space-to-show-ad-admob) – mavrosxristoforos Dec 19 '17 at 14:29
  • It really doesn't matter that you add it programmatically. If it says there is no space, you should probably look at this: https://stackoverflow.com/questions/21848532/not-enough-space-to-show-ad-admob – mavrosxristoforos Dec 19 '17 at 14:30
  • @mavrosxristoforos Thanks, but I've already seen that answer. There is no problem that there is no space to show banner. At portrait orientation there is 100% chances that we have no enough space. The problem is that onAdLoaded() called even in cases when there is no space to show banner. – Anton Prokopov Dec 19 '17 at 14:38
  • @AntonProkopov I am not sure what you mean. What exactly are you trying to achieve? Do you _not_ want the `onAdLoaded()` to be called? – mavrosxristoforos Dec 19 '17 at 17:17
  • @mavrosxristoforos I want to cath situation when threre is no space to show banner. I found my case in discussion at google groups (https://groups.google.com/forum/#!topic/google-admob-ads-sdk/jmT5oK3MGTo) but there is no solution – Anton Prokopov Dec 20 '17 at 07:12
  • @mavrosxristoforos Now I researched the issue deep enuogh. There is really no way to catch "Not enough space to show ad" - case by standard AdMob methods. We need check is there enough / not enough space on the screen before request banner. Otherwise (if banner does exist in ad system) banner will be loaded and AdListener will call onAdLoaded(). Real pain( Thank you for your help man! – Anton Prokopov Dec 20 '17 at 07:56
  • @AntonProkopov I understand what you meant now. It was not really clear at first. I wish I could have helped more. – mavrosxristoforos Dec 20 '17 at 20:13

1 Answers1

0

I understand that you're trying to load the ad in first instance, so isAddClosedByUser starts in false, so here:

if (Build.VERSION.SDK_INT >= 19) {
            RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.MATCH_PARENT,
                    RelativeLayout.LayoutParams.WRAP_CONTENT));
            params.setMargins(0, (int) itemView.getContext().getResources().getDimension(R.dimen.banner_body_part_banner_padding_top),
                    0, (int) itemView.getContext().getResources().getDimension(R.dimen.banner_body_part_banner_padding_bottom));
            params.addRule(RelativeLayout.CENTER_IN_PARENT, RelativeLayout.TRUE);
            adView.setLayoutParams(params);
            adView.setVisibility(View.GONE); //<----- YOUR ADVIEW IS NEVER GONNA SHOW
        } else {
            ViewGroup.MarginLayoutParams params = new ViewGroup.MarginLayoutParams(new ViewGroup.LayoutParams(RelativeLayout.LayoutParams.MATCH_PARENT,
                    RelativeLayout.LayoutParams.WRAP_CONTENT));
            params.setMargins(0, (int) itemView.getContext().getResources().getDimension(R.dimen.banner_body_part_banner_padding_top),
                    0, (int) itemView.getContext().getResources().getDimension(R.dimen.banner_body_part_banner_padding_bottom));
            adView.setLayoutParams(params);
            adView.setVisibility(View.GONE); //<----- YOUR ADVIEW IS NEVER GONNA SHOW
        }

Making it visible should do the trick.

Alejandro Cumpa
  • 2,118
  • 1
  • 24
  • 45
  • That adView.setVisibility(View.GONE) is just a rudundant condition. Visibitity of adView is overriden inside onAdFailedToLoad(int i) and onAdLoaded(). I tryed to do the way you suggested. But unfortunatly it does not work – Anton Prokopov Dec 19 '17 at 14:54
  • Maybe, try to add the view before loading the ad. – Alejandro Cumpa Dec 19 '17 at 15:15