Admob Banner ad добавь в андроид студию - PullRequest
0 голосов
/ 30 ноября 2018

Этот код работает хорошо, без его части.Я не могу добавить admob в макет Android-студии.Как добавить AdMob в макет, пожалуйста, посмотрите мой код.Помогите мне добавить в admob.Code без ошибок, только объявление не отображается в макете.пожалуйста, кто-нибудь подскажет, какую часть нужно отредактировать.спасибо

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="horizontal">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_marginBottom="70dp"
        android:gravity="center"
        android:orientation="horizontal">

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_marginLeft="@dimen/difficulty_margin"
            android:layout_marginTop="30dp"
            android:layout_marginRight="@dimen/difficulty_margin"
            android:layout_marginBottom="30dp"
            android:layout_weight="1"
            android:gravity="center"
            android:orientation="vertical">

            <com.nayemuzzaman.kidsmemorize.ui.DifficultyView
                android:id="@+id/select_difficulty_1"
                android:layout_width="match_parent"
                android:layout_height="30dp"
                android:layout_weight="1" />

            <TextView
                android:id="@+id/time_difficulty_1"
                android:layout_width="match_parent"
                android:layout_height="35dp"
                android:layout_marginTop="-40dp"
                android:layout_marginBottom="18dp"
                android:background="@drawable/time_border"
                android:textSize="17sp" />

            <com.nayemuzzaman.kidsmemorize.ui.DifficultyView
                android:id="@+id/select_difficulty_4"
                android:layout_width="match_parent"
                android:layout_height="30dp"
                android:layout_weight="1" />

            <TextView
                android:id="@+id/time_difficulty_4"
                android:layout_width="match_parent"
                android:layout_height="35dp"
                android:layout_marginTop="-40dp"
                android:background="@drawable/time_border"
                android:textSize="17sp" />


        </LinearLayout>

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_marginLeft="@dimen/difficulty_margin"
            android:layout_marginTop="30dp"
            android:layout_marginRight="@dimen/difficulty_margin"
            android:layout_marginBottom="30dp"
            android:layout_weight="1"
            android:gravity="center"
            android:orientation="vertical">

            <com.nayemuzzaman.kidsmemorize.ui.DifficultyView
                android:id="@+id/select_difficulty_2"
                android:layout_width="match_parent"
                android:layout_height="30dp"
                android:layout_weight="1" />

            <TextView
                android:id="@+id/time_difficulty_2"
                android:layout_width="match_parent"
                android:layout_height="35dp"
                android:layout_marginTop="-40dp"
                android:layout_marginBottom="18dp"
                android:background="@drawable/time_border"
                android:textSize="17sp" />

            <com.nayemuzzaman.kidsmemorize.ui.DifficultyView
                android:id="@+id/select_difficulty_5"
                android:layout_width="match_parent"
                android:layout_height="30dp"
                android:layout_weight="1" />

            <TextView
                android:id="@+id/time_difficulty_5"
                android:layout_width="match_parent"
                android:layout_height="35dp"
                android:layout_marginTop="-40dp"
                android:background="@drawable/time_border"
                android:textSize="17sp" />


        </LinearLayout>

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_marginLeft="@dimen/difficulty_margin"
            android:layout_marginTop="30dp"
            android:layout_marginRight="@dimen/difficulty_margin"
            android:layout_marginBottom="30dp"
            android:layout_weight="1"
            android:gravity="center"
            android:orientation="vertical">

            <com.nayemuzzaman.kidsmemorize.ui.DifficultyView
                android:id="@+id/select_difficulty_3"
                android:layout_width="match_parent"
                android:layout_height="30dp"
                android:layout_weight="1" />

            <TextView
                android:id="@+id/time_difficulty_3"
                android:layout_width="match_parent"
                android:layout_height="35dp"
                android:layout_marginTop="-40dp"
                android:layout_marginBottom="18dp"
                android:background="@drawable/time_border"
                android:textSize="17sp" />

            <com.nayemuzzaman.kidsmemorize.ui.DifficultyView
                android:id="@+id/select_difficulty_6"
                android:layout_width="match_parent"
                android:layout_height="30dp"
                android:layout_weight="1" />

            <TextView
                android:id="@+id/time_difficulty_6"
                android:layout_width="match_parent"
                android:layout_height="35dp"
                android:layout_marginTop="-40dp"
                android:background="@drawable/time_border"
                android:textSize="17sp" />


        </LinearLayout>

    </LinearLayout>

    <com.google.android.gms.ads.AdView xmlns:ads="http://schemas.android.com/apk/res-auto"
        android:id="@+id/adView"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_alignParentBottom="true"
        android:layout_marginTop="@dimen/margintoptendp"
        ads:adSize="BANNER"
        ads:adUnitId="@string/banner_adUnitId" />

</LinearLayout> 

DifficultySelectFragment.java

  @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View view = LayoutInflater.from(Shared.context).inflate(R.layout.difficulty_select_fragment, container, false);
        Theme theme = Shared.engine.getSelectedTheme();

        DifficultyView difficulty1 = view.findViewById(R.id.select_difficulty_1);
        difficulty1.setDifficulty(1, Memory.getHighStars(theme.id, 1));
        setOnClick(difficulty1, 1);
       /* FIrstFragment fIrstFragment = new FIrstFragment();
        moveToFragment(fIrstFragment);*/


        DifficultyView difficulty2 =  view.findViewById(R.id.select_difficulty_2);
        difficulty2.setDifficulty(2, Memory.getHighStars(theme.id, 2));
        setOnClick(difficulty2, 2);

        DifficultyView difficulty3 =  view.findViewById(R.id.select_difficulty_3);
        difficulty3.setDifficulty(3, Memory.getHighStars(theme.id, 3));
        setOnClick(difficulty3, 3);

        DifficultyView difficulty4 =  view.findViewById(R.id.select_difficulty_4);
        difficulty4.setDifficulty(4, Memory.getHighStars(theme.id, 4));
        setOnClick(difficulty4, 4);

        DifficultyView difficulty5 =  view.findViewById(R.id.select_difficulty_5);
        difficulty5.setDifficulty(5, Memory.getHighStars(theme.id, 5));
        setOnClick(difficulty5, 5);

        DifficultyView difficulty6 =  view.findViewById(R.id.select_difficulty_6);
        difficulty6.setDifficulty(6, Memory.getHighStars(theme.id, 6));
        setOnClick(difficulty6, 6);

        animate(difficulty1, difficulty2, difficulty3, difficulty4, difficulty5, difficulty6);

        Typeface type = Typeface.createFromAsset(Shared.context.getAssets(), "fonts/grobold.ttf");


        TextView text1 =  view.findViewById(R.id.time_difficulty_1);
        text1.setGravity(Gravity.CENTER_HORIZONTAL | Gravity.CENTER_VERTICAL);
        text1.setTypeface(type);
        text1.setText(getBestTimeForStage(theme.id, 1));

        TextView text2 =  view.findViewById(R.id.time_difficulty_2);
        text2.setGravity(Gravity.CENTER_HORIZONTAL | Gravity.CENTER_VERTICAL);
        text2.setTypeface(type);
        text2.setText(getBestTimeForStage(theme.id, 2));

        TextView text3 =  view.findViewById(R.id.time_difficulty_3);
        text3.setGravity(Gravity.CENTER_HORIZONTAL | Gravity.CENTER_VERTICAL);
        text3.setTypeface(type);
        text3.setText(getBestTimeForStage(theme.id, 3));

        TextView text4 =  view.findViewById(R.id.time_difficulty_4);
        text4.setGravity(Gravity.CENTER_HORIZONTAL | Gravity.CENTER_VERTICAL);
        text4.setTypeface(type);
        text4.setText(getBestTimeForStage(theme.id, 4));

        TextView text5 =  view.findViewById(R.id.time_difficulty_5);
        text5.setGravity(Gravity.CENTER_HORIZONTAL | Gravity.CENTER_VERTICAL);
        text5.setTypeface(type);
        text5.setText(getBestTimeForStage(theme.id, 5));

        TextView text6 = view.findViewById(R.id.time_difficulty_6);
        text6.setGravity(Gravity.CENTER_HORIZONTAL | Gravity.CENTER_VERTICAL);
        text6.setTypeface(type);
        text6.setText(getBestTimeForStage(theme.id, 6));

        mAdview =  view.findViewById( R.id.adView );
        AdRequest adRequest = new AdRequest.Builder().tagForChildDirectedTreatment(true).build();
        mAdview.loadAd( adRequest );

        return view;

    }

Ответы [ 4 ]

0 голосов
/ 01 декабря 2018

Заменить деталь в фрагменте.Я тестирую в InterstitialAd AdMob с этим кодом.

//For add of Admob app ID
        MobileAds.initialize(getContext(),
                "ca-app-pub-3940256099942544~3347511713");

        mInterstitialAd = new InterstitialAd(getContext());
        mInterstitialAd.setAdUnitId("ca-app-pub-3940256099942544/1033173712");
        final AdRequest request = new AdRequest.Builder().build();

        mInterstitialAd.setAdListener(new AdListener() {
            @Override
            public void onAdLoaded() {
                super.onAdLoaded();
                mInterstitialAd.show();
            }
            @Override
            public void onAdClosed() {
                super.onAdClosed();
                // mInterstitialAd.loadAd(request.build());
            }
        });

        mInterstitialAd.loadAd( request );
0 голосов
/ 30 ноября 2018

Добавьте слушателя Adview, чтобы найти в чем ошибка.

mAdView.setAdListener(new AdListener() {
    @Override
    public void onAdLoaded() {
        // Code to be executed when an ad finishes loading.
          Toast.makeText(this, "ad loaded", Toast.LENGTH_SHORT).show();


    }

    @Override
    public void onAdFailedToLoad(int errorCode) {
        // Code to be executed when an ad request fails.
          Toast.makeText(this, errorCode.toString, Toast.LENGTH_SHORT).show();

    }

    @Override
    public void onAdOpened() {
        // Code to be executed when an ad opens an overlay that
        // covers the screen.
          Toast.makeText(this, "Ad opened", Toast.LENGTH_SHORT).show();

    }

    @Override
    public void onAdLeftApplication() {
        // Code to be executed when the user has left the app.
            Toast.makeText(this, "Ad left application", Toast.LENGTH_SHORT).show();

    }

    @Override
    public void onAdClosed() {
        // Code to be executed when when the user is about to return
        // to the app after tapping on an ad.
           Toast.makeText(this, "ad closed", Toast.LENGTH_SHORT).show();

    }
});
0 голосов
/ 30 ноября 2018
<?xml version="1.0" encoding="utf-8"?>
<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:orientation="horizontal">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_marginBottom="70dp"
        android:gravity="center"
        android:orientation="horizontal">

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_marginLeft="@dimen/difficulty_margin"
            android:layout_marginTop="30dp"
            android:layout_marginRight="@dimen/difficulty_margin"
            android:layout_marginBottom="30dp"
            android:layout_weight="1"
            android:gravity="center"
            android:orientation="vertical">

            <com.nayemuzzaman.kidsmemorize.ui.DifficultyView
                android:id="@+id/select_difficulty_1"
                android:layout_width="match_parent"
                android:layout_height="30dp"
                android:layout_weight="1" />

            <TextView
                android:id="@+id/time_difficulty_1"
                android:layout_width="match_parent"
                android:layout_height="35dp"
                android:layout_marginTop="-40dp"
                android:layout_marginBottom="18dp"
                android:background="@drawable/time_border"
                android:textSize="17sp" />

            <com.nayemuzzaman.kidsmemorize.ui.DifficultyView
                android:id="@+id/select_difficulty_4"
                android:layout_width="match_parent"
                android:layout_height="30dp"
                android:layout_weight="1" />

            <TextView
                android:id="@+id/time_difficulty_4"
                android:layout_width="match_parent"
                android:layout_height="35dp"
                android:layout_marginTop="-40dp"
                android:background="@drawable/time_border"
                android:textSize="17sp" />


        </LinearLayout>

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_marginLeft="@dimen/difficulty_margin"
            android:layout_marginTop="30dp"
            android:layout_marginRight="@dimen/difficulty_margin"
            android:layout_marginBottom="30dp"
            android:layout_weight="1"
            android:gravity="center"
            android:orientation="vertical">

            <com.nayemuzzaman.kidsmemorize.ui.DifficultyView
                android:id="@+id/select_difficulty_2"
                android:layout_width="match_parent"
                android:layout_height="30dp"
                android:layout_weight="1" />

            <TextView
                android:id="@+id/time_difficulty_2"
                android:layout_width="match_parent"
                android:layout_height="35dp"
                android:layout_marginTop="-40dp"
                android:layout_marginBottom="18dp"
                android:background="@drawable/time_border"
                android:textSize="17sp" />

            <com.nayemuzzaman.kidsmemorize.ui.DifficultyView
                android:id="@+id/select_difficulty_5"
                android:layout_width="match_parent"
                android:layout_height="30dp"
                android:layout_weight="1" />

            <TextView
                android:id="@+id/time_difficulty_5"
                android:layout_width="match_parent"
                android:layout_height="35dp"
                android:layout_marginTop="-40dp"
                android:background="@drawable/time_border"
                android:textSize="17sp" />


        </LinearLayout>

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_marginLeft="@dimen/difficulty_margin"
            android:layout_marginTop="30dp"
            android:layout_marginRight="@dimen/difficulty_margin"
            android:layout_marginBottom="30dp"
            android:layout_weight="1"
            android:gravity="center"
            android:orientation="vertical">

            <com.nayemuzzaman.kidsmemorize.ui.DifficultyView
                android:id="@+id/select_difficulty_3"
                android:layout_width="match_parent"
                android:layout_height="30dp"
                android:layout_weight="1" />

            <TextView
                android:id="@+id/time_difficulty_3"
                android:layout_width="match_parent"
                android:layout_height="35dp"
                android:layout_marginTop="-40dp"
                android:layout_marginBottom="18dp"
                android:background="@drawable/time_border"
                android:textSize="17sp" />

            <com.nayemuzzaman.kidsmemorize.ui.DifficultyView
                android:id="@+id/select_difficulty_6"
                android:layout_width="match_parent"
                android:layout_height="30dp"
                android:layout_weight="1" />

            <TextView
                android:id="@+id/time_difficulty_6"
                android:layout_width="match_parent"
                android:layout_height="35dp"
                android:layout_marginTop="-40dp"
                android:background="@drawable/time_border"
                android:textSize="17sp" />


        </LinearLayout>

    </LinearLayout>

    <com.google.android.gms.ads.AdView xmlns:ads="http://schemas.android.com/apk/res-auto"
        android:id="@+id/adView"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_alignParentBottom="true"
        android:layout_marginTop="@dimen/margintoptendp"
        ads:adSize="BANNER"
        ads:adUnitId="@string/banner_adUnitId" />

</RelativeLayout>     

Добавить xmlns:ads="http://schemas.android.com/apk/res-auto" к корневому макету


In Fragment:

private AdView mBannerAd;

mBannerAd = view.findViewById(R.id.adView);

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

Добавить зависимости

implementation 'com.google.android.gms:play-services-ads:11.6.0'
0 голосов
/ 30 ноября 2018

Чтобы ответить на этот вопрос

только реклама не отображается в макете

Возможны 2 причины, по которым объявления не показываются

1) Если вы только что создали свой идентификатор рекламного моба, есть большая вероятность, что вы не получите никакого объявления.Потому что, когда вы создаете новый идентификатор рекламного моба, иногда требуется предоставить какое-то объявление для вашего идентификатора

2) Если первый случай недействителен, вы сделали идентификатор 4-5 часов назад, тогда убедитесь, что ваш идентификатор правильный,По ошибке вы не используете неверный идентификатор или опечатку.

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...