anuncio admob no Fragment android studio

Asked

Viewed 116 times

0

I am trying to add banner ad in my app by Fragment, but I could not, nothing appears. What can I do to solve?

fragment_inicio.xml

  <androidx.constraintlayout.widget.ConstraintLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent">


        <com.google.android.gms.ads.AdView xmlns:ads="http://schemas.android.com/apk/res-auto"
            android:id="@+id/adView"
            android:layout_width="324dp"
            android:layout_height="56dp"
            android:layout_alignParentBottom="true"
            android:layout_centerHorizontal="true"
            android:visibility="visible"
            ads:adSize="BANNER"
            ads:adUnitId="ca-app-pub-xxxxxxxxxxxx~xxxxxxxxxxxx"
            ads:layout_constraintTop_toTopOf="parent"
            ads:layout_constraintRight_toRightOf="parent"
            ads:layout_constraintLeft_toLeftOf="parent"
            ads:layout_constraintBottom_toBottomOf="parent"
            ads:layout_constraintVertical_bias="1.0">
        </com.google.android.gms.ads.AdView>


    </androidx.constraintlayout.widget.ConstraintLayout>

Homefragment.java

        private AdView mAdView;

public InicioFragment() {
    // Required empty public constructor
}


    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
        Bundle savedInstanceState) {

    // Inflate the layout for this fragment
    View view = inflater.inflate(R.layout.fragment_inicio, container, false);


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

androidManifest

 <meta-data
        android:name="com.google.android.gms.ads.APPLICATION_ID"
        android:value="ca-app-pub-xxxxxxxxxxxx~xxxxxxxxxxxx"/>


 <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />

<uses-permission android:name="android.permission.INTERNET" />

1 answer

3


For admob to appear in the app, it first has to be posted in the play store for a few days.

The main problem I identified is that you are using the same ID in the manifest as in the banner in Fragment.

create ads strings in res/value/strings.xml:

<string name="adAPPId">ca-app-pub-xxxxxxxxxxx~xxxxxxxx</string>
<string name="adUnitId">ca-app-pub-xxxxxxxxxxx/xxxxxxxx</string>

Note that Appid has "~" and adUnitId has the "/".

Now in the meta-data of your manifest leave the value field as follows:

 <meta-data
        android:name="com.google.android.gms.ads.APPLICATION_ID"
        android:value="@string/adAPPId" />

And in the xml of your Fragment change:

ads:adUnitId="ca-app-pub-xxxxxxxxxxxx~xxxxxxxxxxxx"

for:

ads:adUnitId="@string/adUnitId"

For testing it is possible to make it appear by changing:

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

for:

        AdRequest.Builder builder = new AdRequest.Builder();
        if (BuildConfig.DEBUG) {
            builder.addTestDevice(AdRequest.DEVICE_ID_EMULATOR);
        }

        AdRequest adRequest = builder.build();
        mAdView.loadAd(adRequest);

Note: Maybe the AdRequest.DEVICE_ID_EMULATOR does not work, in this case, while running the program by android studio, go to the bottom tab called "Logcat" and search for Ads, you will have some message like

Use Adrequest.Builder.addTestDevice("Something") to get test ads on this device.

Then copy this value it provides and put in place of the AdRequest.DEVICE_ID_EMULATOR

I hope I helped. Hug

Browser other questions tagged

You are not signed in. Login or sign up in order to post.