Admob Banner Ad Java and XML Code For Android Studio

Admob Banner Ad Java and XML Code For Android App


Table of Contents


1. First of All, Open build.gradle(Module:App) File

2. Copy Code and Paste Before buildTypes{ }

 
buildscript {
        repositories {
            google()
            mavenCentral()
        }
    }







3. Copy and Paste inside Dependency (use latest version)


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




4. Write Below Code After Dependency


allprojects {
    repositories {
        google()
        mavenCentral()
    }
}




4. Copy Below XML Code and Paste inside Your XML Code



<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_alignParentBottom="true"
android:layout_centerHorizontal="true"
android:layout_marginTop="8dp"
ads:adSize="BANNER"
app:adUnitId="Your Banner Ad ID"
       app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@id/web_view"
app:layout_constraintVertical_bias="0">
</com.google.android.gms.ads.AdView>





5. Declare Adview in Java File


private AdView mAdView;




6. Copy Below Java Code and Paste in Your 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);
    }
});





7. Open AndroidManifeast File and Paste Code 



<meta-data
android:name="com.google.android.gms.ads.APPLICATION_ID"
android:value="admob app id"/>










All Done Now Test App










Post a Comment

If You Have Any Doubts, Please Let Me Know and Share Your Opinion.

أحدث أقدم