Dynamically Create Admob Banner and Interstitial Ads
21-01-2019activity_main.xml file
<?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" xmlns:app="http://schemas.android.com/apk/res-auto" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent"> <FrameLayout xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/fragment_container" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_above="@+id/navigation" android:layout_alignParentTop="true" android:background="@color/colorMain" /> <android.support.design.widget.BottomNavigationView android:id="@+id/navigation" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_above="@+id/linearAdView" android:layout_marginStart="0dp" android:layout_marginEnd="0dp" android:background="?android:attr/windowBackground" app:menu="@menu/navigation" /> <LinearLayout android:id="@+id/linearAdView" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_alignParentBottom="true" android:layout_centerHorizontal="true" android:orientation="vertical"> </LinearLayout> </RelativeLayout>
MainActivity Class
public class MainActivity extends AppCompatActivity { private DBHelper mDbHelper; private TextView mTextMessage; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); AdmobHelper.showAdmob(this); AdmobHelper.showInterstitialAds(getApplicationContext()); mTextMessage = (TextView) findViewById(R.id.message); BottomNavigationView navigation = (BottomNavigationView) findViewById(R.id.navigation); navigation.setOnNavigationItemSelectedListener(mOnNavigationItemSelectedListener); } }
AdmobHelper Class
package com.codesenior.radyom.helper; import android.content.Context; import android.support.v7.app.AppCompatActivity; import android.view.View; import android.widget.LinearLayout; import com.codesenior.radyom.R; import com.codesenior.radyom.models.Settings; import com.google.android.gms.ads.AdListener; 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; public class AdmobHelper { public static void showInterstitialAds(final Context context) { SettingsHelper.getSettings(new SettingsHelperResponse() { @Override public void response(Settings settings) { if (!settings.getInterstitalAd().equals("true")) return; final InterstitialAd mInterstitial = new InterstitialAd(context); mInterstitial.setAdUnitId(settings.getInterstitalAdId()); mInterstitial.loadAd(new AdRequest.Builder().build()); mInterstitial.setAdListener(new AdListener() { @Override public void onAdLoaded() { super.onAdLoaded(); mInterstitial.show(); } }); } @Override public void failure(String message) { } }); } public static void showAdmob(final AppCompatActivity activity) { SettingsHelper.getSettings(new SettingsHelperResponse() { @Override public void response(Settings settings) { if (!settings.getBannerAd().equals("true")) return; final AdView adView = new AdView(activity); adView.setAdUnitId(settings.getBannerAdId()); adView.setAdSize(AdSize.SMART_BANNER); AdRequest adRequest = new AdRequest.Builder().build(); adView.loadAd(adRequest); adView.setVisibility(View.GONE); adView.setAdListener(new AdListener() { @Override public void onAdLoaded() { super.onAdLoaded(); adView.setVisibility(View.VISIBLE); } @Override public void onAdFailedToLoad(int i) { super.onAdFailedToLoad(i); } }); LinearLayout linearLayout = activity.findViewById(R.id.linearAdView); LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.MATCH_PARENT); linearLayout.addView(adView, params); } @Override public void failure(String message) { } }); } }
SettingsHelper and SettingsHelperResponse Class
package com.codesenior.radyom.helper; import com.codesenior.radyom.Config; import com.codesenior.radyom.models.Settings; import com.codesenior.radyom.restapis.ApiClient; import com.codesenior.radyom.restapis.SettingsService; import com.codesenior.radyom.restapis.SingleResponse; import retrofit2.Call; import retrofit2.Callback; import retrofit2.Response; import retrofit2.Retrofit; public class SettingsHelper { public static void getSettings(final SettingsHelperResponse settingsHelperResponse) { Retrofit retrofit = ApiClient.getClient(Config.REST_API); SettingsService service = retrofit.create(SettingsService.class); service.all().enqueue(new Callback<SingleResponse<Settings>>() { @Override public void onResponse(Call<SingleResponse<Settings>> call, Response<SingleResponse<Settings>> response) { Settings settings = response.body().getData(); settingsHelperResponse.response(settings); } @Override public void onFailure(Call<SingleResponse<Settings>> call, Throwable t) { settingsHelperResponse.failure(t.getMessage()); } }); } } import com.codesenior.radyom.models.Settings; public interface SettingsHelperResponse { public void response(Settings settings); public void failure(String message); }
Settings Class
public class Settings { private int id; private String appName; private String onesignalAppId; private String googleProjectNumber; private String interstitalAd; private String interstitalAdId; private String bannerAd; private String bannerAdId; public int getId() { return id; } public void setId(int id) { this.id = id; } public String getAppName() { return appName; } public void setAppName(String appName) { this.appName = appName; } public String getOnesignalAppId() { return onesignalAppId; } public void setOnesignalAppId(String onesignalAppId) { this.onesignalAppId = onesignalAppId; } public String getGoogleProjectNumber() { return googleProjectNumber; } public void setGoogleProjectNumber(String googleProjectNumber) { this.googleProjectNumber = googleProjectNumber; } public String getInterstitalAd() { return interstitalAd; } public void setInterstitalAd(String interstitalAd) { this.interstitalAd = interstitalAd; } public String getInterstitalAdId() { return interstitalAdId; } public void setInterstitalAdId(String interstitalAdId) { this.interstitalAdId = interstitalAdId; } public String getBannerAd() { return bannerAd; } public void setBannerAd(String bannerAd) { this.bannerAd = bannerAd; } public String getBannerAdId() { return bannerAdId; } public void setBannerAdId(String bannerAdId) { this.bannerAdId = bannerAdId; } }