Socialize

Implementation of banner ads in Android Studio

 

Implementation of banner ads in Android Studio 2023

Hello friends, in this post we will see information about Implementation of banner ads in android studio 2022. After creating the project in Android Studio, we will see in it the information about how to display banner ads in Activity

Implementation of banner ads step 1

First of all, open your project in Android Studio. Open the build.gradle and add the following dependency. Then click on sync now at the top and wait for the project to sync.

implementation 'com.google.android.gms:play-services-ads:21.0.0'

Then open manifests in your project. After opening manifests, you want to add internet permissions in it. For this you need to add the following code in manifests below package name.

Implementation of banner ads step 2

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

Then we want to add metadata in manifests. You want to copy the code and add it to your project. Here you are given a sample ID for the test. Before you publish the app, you want to change the ID and paste your app’s ID here.

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

Implementation of banner ads step 3

Then open the xml file of the activity on which you want to display the ad. In that xml file you want to paste the following code. This code gives test ID. You need to change this ID before publishing your app and enter your banner ads ID here.

 <com.google.android.gms.ads.AdView
xmlns:ads="http://schemas.android.com/apk/res-auto"
android:id="@+id/adView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_alignParentBottom="true"
ads:adSize="BANNER"
ads:adUnitId="ca-app-pub-3940256099942544/6300978111">
</com.google.android.gms.ads.AdView>

Implementation of banner ads step 4

Then open the java file of the activity on which you want to display the ad. You want to paste the following code in that java file.

    MobileAds.initialize(this, new OnInitializationCompleteListener() {
            @Override
            public void onInitializationComplete(InitializationStatus initializationStatus) {
            }
        });

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

After doing all this, run your app and see, you can see test ads in your app. After testing, change your Banner App ID.

In this way we can put banner ads in Android studio

Post a Comment

0 Comments