Admob ads not showing in Android recycler view

你说的曾经没有我的故事 提交于 2019-12-11 07:43:48

问题


Hi there my app is not showing ads in Android recycler view I want to show the ads at the bottom of my app but it is not displaying them I don't want to show native ads I only want to show a single banner at the bottom. It is a dictionary app which uses Sqlite database to load data native ads will not suit it. Can u please help here is my code. Main activity.java

mAdView = (AdView) findViewById(R.id.adView);
mAdView.setAdListener(new AdListener() {
    @Override
    public void onAdLoaded() {
        Toast.makeText(getApplicationContext(), "onAdFailedToLoad", Toast.LENGTH_LONG).show();
            if (mAdView.getVisibility() == View.GONE) {
                mAdView.setVisibility(View.VISIBLE);
            }
        }
        @Override
        public void onAdFailedToLoad(int errorCode) {
            Toast.makeText(getApplicationContext(), "onAdFailedToLoad", Toast.LENGTH_SHORT).show();
        }
        @Override
        public void onAdOpened() {
            Toast.makeText(getApplicationContext(), "onAdOpened", Toast.LENGTH_SHORT).show();
        }
        @Override
        public void onAdLeftApplication() {
            Toast.makeText(getApplicationContext(), "onAdLeftApplication", Toast.LENGTH_SHORT).show();
        }
        @Override
        public void onAdClosed() {
            Toast.makeText(getApplicationContext(), "onAdClosed", Toast.LENGTH_SHORT).show();
        }
    });


    AdRequest adRequest = new AdRequest.Builder().build();
    mAdView.loadAd(adRequest);

And here is my content main.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="match_parent"
    android:background="#e2e2e2"
    android:paddingBottom="10dp"
    android:paddingLeft="10dp"
    android:paddingRight="10dp"
    android:paddingTop="10dp">

    <android.support.v7.widget.RecyclerView
        android:id="@+id/my_recycler_view"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_below="@id/adView"
        android:descendantFocusability="blocksDescendants"
        android:scrollbars="vertical"/>

    <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_centerHorizontal="true"
        ads:adSize="SMART_BANNER"
        ads:adUnitId="ca-app-pub-3940256099942544/6300978111"/>
</RelativeLayout>

回答1:


Add on OnCreate

MobileAds.initialize(this,"Your ad unit id from admob");
mAdView = (AdView) findViewById(R.id.adView);
AdRequest adRequest = new AdRequest.Builder().addTestDevice("Add Your Device id").build();
mAdView.loadAd(adRequest);

Override Methods

@Override
public void onPause() {
    if (mAdView != null) {
         mAdView.pause();
    }
    super.onPause();
}

@Override
public void onResume() {
    super.onResume();
    if (mAdView != null) {
         mAdView.resume();
    }
}

@Override
public void onDestroy() {
    if (mAdView != null) {
         mAdView.destroy();
    }
    super.onDestroy();
}

Your XML File

<com.google.android.gms.ads.AdView
        android:id="@+id/adView"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginLeft="10dp"
        android:layout_alignParentBottom="true"
        android:layout_marginBottom="10dp"
        android:layout_marginRight="10dp"
        ads:adSize="BANNER"
        ads:adUnitId="@string/banner_home_footer" />


来源:https://stackoverflow.com/questions/47427673/admob-ads-not-showing-in-android-recycler-view

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!