After spending many hours and long nights building the app, let’s start getting it off the ground and start earning money from it. Admob can display ads in your app and partner with advertisers all over the world making your hard work really pay off.
Admob network provides different types of ad formats. I implemented Banner Ads and Interstitial Ads into 123Flip app. The steps to implement admob are:
1) Create Ad Units in Admob 2) Import Mobile Ads SDK in Android Studio 3) For banner ad, add the AdView in your layout 4) Initialize Mobile Ads in your activity
Banner Ads in activity_main.xml
A mobile banner ad is a rectangular system-initiated ad unit, which can be either static or animated. Mobile banner ads are typically displayed on the top or bottom of the screen, sticking to the screen for the duration of the user session.
Add AdView to activity_main.xml
<?xml version="1.0" encoding="utf-8"?> <androidx.coordinatorlayout.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android"xmlns:app="http://schemas.android.com/apk/res-auto" xmlns:tools="http://schemas.android.com/tools" android:id="@+id/clRoot" android:background="@color/white" android:layout_width="match_parent" android:layout_height="match_parent" tools:context=".MainActivity"> <androidx.constraintlayout.widget.ConstraintLayout android:id="@+id/constraintLayout" android:layout_width="match_parent" android:layout_height="match_parent" app:layout_anchorGravity="center"> <LinearLayout android:id="@+id/llTopBar" android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="horizontal" app:layout_constraintEnd_toEndOf="parent" app:layout_constraintHorizontal_bias="0.0" app:layout_constraintStart_toStartOf="parent" app:layout_constraintTop_toTopOf="parent"> <androidx.cardview.widget.CardView android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_margin="10dp" android:layout_weight="0.1" app:cardBackgroundColor="@android:color/transparent" app:cardElevation="0dp"> <TextView android:id="@+id/tvTimer" android:layout_width="80dp" android:layout_height="wrap_content" android:background="@color/white" android:gravity="center" android:padding="2dp" android:text="00:00" android:textAppearance="@style/TextAppearance.AppCompat.Medium" android:textColor="@color/colorPrimaryDark" android:textStyle="bold" tools:ignore="HardcodedText" /> </androidx.cardview.widget.CardView> <androidx.cardview.widget.CardView android:id="@+id/cvLogoAbout" android:layout_width="wrap_content" android:layout_height="match_parent" android:layout_weight="2" app:cardBackgroundColor="@android:color/holo_green_light" app:cardCornerRadius="15dp"> <TextView android:id="@+id/tvSponsor_Message" android:layout_width="match_parent" android:layout_height="match_parent" android:layout_gravity="center" android:layout_margin="4dp" android:text="@string/sponsor_bannerMsg_default" android:textAlignment="center" android:textAppearance="@style/TextAppearance.AppCompat.Small" android:textColor="@color/black" android:textColorLink="@color/black" android:textStyle="bold|italic" /> </androidx.cardview.widget.CardView> </LinearLayout> <LinearLayout android:id="@+id/linearLayoutMain" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_marginBottom="60dp" android:background="?attr/colorSecondaryVariant" android:orientation="horizontal" app:layout_constraintBottom_toBottomOf="parent" app:layout_constraintEnd_toEndOf="parent" app:layout_constraintHorizontal_bias="0.0" app:layout_constraintStart_toStartOf="parent"> <androidx.cardview.widget.CardView android:layout_width="match_parent" android:layout_height="match_parent" android:layout_margin="10dp" android:layout_weight="1" app:cardCornerRadius="8dp"> <TextView android:id="@+id/tvNumMoves" android:layout_width="match_parent" android:layout_height="match_parent" android:background="@color/color_green_darkest" android:gravity="center" android:padding="8dp" android:text="@string/tvMoves" android:textAppearance="@style/TextAppearance.AppCompat.Medium" android:textColor="@color/white" tools:ignore="HardcodedText" /> </androidx.cardview.widget.CardView> <androidx.cardview.widget.CardView android:layout_width="match_parent" android:layout_height="match_parent" android:layout_margin="10dp" android:layout_weight="1" app:cardCornerRadius="8dp"> <TextView android:id="@+id/tvNumPairs" android:layout_width="match_parent" android:layout_height="wrap_content" android:background="@color/color_green_darkest" android:gravity="center" android:padding="8dp" android:text="@string/btn_setting" android:textAppearance="@style/TextAppearance.AppCompat.Medium" android:textColor="@color/white" app:drawableLeftCompat="@drawable/ic_timer" tools:ignore="HardcodedText" /> </androidx.cardview.widget.CardView> </LinearLayout> <androidx.recyclerview.widget.RecyclerView android:id="@+id/rvBoard" android:layout_width="0dp" android:layout_height="0dp" android:layout_marginBottom="8dp" android:layoutAnimation="@anim/layout_animation" android:overScrollMode="never" app:layout_behavior="@string/appbar_scrolling_view_behavior" app:layout_constraintBottom_toTopOf="@+id/linearLayoutMain" app:layout_constraintEnd_toEndOf="parent" app:layout_constraintHorizontal_bias="0.0" app:layout_constraintStart_toStartOf="parent" app:layout_constraintTop_toBottomOf="@+id/llTopBar" app:layout_constraintVertical_bias="0.0" /> <com.google.android.gms.ads.AdView android:id="@+id/adView" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentBottom="true" android:layout_centerHorizontal="true" app:adSize="BANNER" app:adUnitId="ca-app-pub-3940256099942544/6300978111" app:layout_constraintBottom_toBottomOf="parent" app:layout_constraintEnd_toEndOf="parent" app:layout_constraintStart_toStartOf="parent" app:layout_constraintTop_toBottomOf="@+id/linearLayoutMain"></com.google.android.gms.ads.AdView> </androidx.constraintlayout.widget.ConstraintLayout> </androidx.coordinatorlayout.widget.CoordinatorLayout>
Initialize AdView in MainActivity: onCreate()
class MainActivity : AppCompatActivity() { companion object { private const val TAG = "MainActivity" private const val CREATE_REQUEST_CODE = 248 private const val GAME_REQUEST_CODE = 173. . . override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) setContentView(R.layout.activity_main) getPreference() auth = getInstance() MobileAds.initialize(this) {} mAdView = findViewById(R.id.adView) val adRequest = AdRequest.Builder().build() mAdView.loadAd(adRequest)
Add Interstitial Ad in GamelistActivity
Interstitial ads provide rich interactive ads for users on mobile apps. Interstitial ads are designed to be placed between content, so they are best placed at natural app transition points.Â
class GamelistActivity : AppCompatActivity() { private lateinit var mListener: onItemClickListener private var sortQuery: String = ""private var cmd : String = "" private var sponsorEmail = "" private var gameToDelete: String = "" private var passcodeToDelete: String = "" private var mInterstitialAd: InterstitialAd? = null . . . private fun loadInterAd() { var adRequest = AdRequest.Builder().build() // Test InterstitialAd.load(this,"ca-app-pub-3940256099942544/1033173712", adRequest, object : InterstitialAdLoadCallback() { override fun onAdFailedToLoad(adError: LoadAdError) { mInterstitialAd = null } override fun onAdLoaded(interstitialAd: InterstitialAd) { //Log.d(TAG, 'Ad was loaded.') mInterstitialAd = interstitialAd } }) }