How To Implement Ads With Firebase In Android Studio

 Java code

import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.RelativeLayout;
import androidx.appcompat.app.AppCompatActivity;
import com.firebase.client.DataSnapshot;
import com.firebase.client.Firebase;
import com.firebase.client.FirebaseError;
import com.firebase.client.ValueEventListener;
import com.google.android.gms.ads.AdRequest;
import com.google.android.gms.ads.AdSize;
import com.google.android.gms.ads.AdView;
import com.google.android.gms.ads.InterstitialAd;
import com.google.android.gms.ads.MobileAds;
import com.google.android.gms.ads.initialization.InitializationStatus;
import com.google.android.gms.ads.initialization.OnInitializationCompleteListener;
public class MainActivity extends AppCompatActivity {
    RelativeLayout banner;
    String data;
    String intVar;
    InterstitialAd interstitialAd;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

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

        fireInt("https://testads-.firebaseio.com/inter");
        interstitialAd = new InterstitialAd(this);

        Button button = findViewById(R.id.showAds);
        button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (interstitialAd.isLoaded()){
                    interstitialAd.show();
                }


            }
        });

        banner = findViewById(R.id.banner_container);
        fireAds("https://testads-.firebaseio.com/banner");

    }

    public void fireAds(String adsUrl) {
        Firebase.setAndroidContext(this);
        Firebase firebase = new Firebase(adsUrl);
        firebase.addValueEventListener(new ValueEventListener() {
            @Override
            public void onDataChange(DataSnapshot dataSnapshot) {
                data = dataSnapshot.getValue(String.class);
                AdView mAdView = new AdView(MainActivity.this);
                mAdView.setAdUnitId(data);
                banner.addView(mAdView);
                mAdView.setAdSize(AdSize.SMART_BANNER);
                AdRequest adRequest = new AdRequest.Builder().build();
                mAdView.loadAd(adRequest);
            }
            @Override
            public void onCancelled(FirebaseError firebaseError) {
            }
        });

    }



    public void fireInt(String intUrl) {
        Firebase.setAndroidContext(this);
        Firebase firebase = new Firebase(intUrl);
        firebase.addValueEventListener(new ValueEventListener() {
            @Override
            public void onDataChange(DataSnapshot dataSnapshot) {
                intVar = dataSnapshot.getValue(String.class);
                interstitialAd.setAdUnitId(intVar);
                interstitialAd.loadAd(new AdRequest.Builder().build());

            }
            @Override
            public void onCancelled(FirebaseError firebaseError) {
            }
        }); 
}}

XML Code



<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity">


    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/showAds"
        android:text="Show"
        android:layout_centerHorizontal="true"
        android:layout_centerVertical="true"
        />


    <RelativeLayout
        android:id="@+id/banner_container"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true" />


</RelativeLayout>

Build.gradle (App)

implementation 'com.google.firebase:firebase-core:18.0.0'
implementation 'com.google.firebase:firebase-database:19.6.0'
implementation 'com.google.firebase:firebase-ads:19.6.0'
implementation 'com.firebase:firebase-client-android:2.5.1'