Android, Codes and Tips

Just in

12/19/2019

How to add and integrate Admob on Android Studio App in 2020


Follow these steps to integrate the latest Admob codes to to your latest Android Studio App

Before you build your android app add you admob codes in necessary place for optimal performance.

Anything about inversion changes and android too, in order to meet the latest, you must upgrade too.

Create Admob Ads account from here.
Add internet permission an AndroidManifest file

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

1. We have to add Admob and Google Play Service codes to our project SDK

a) Go to Gradle Scripts in your App Project and select 1 see image below

build.gradle (Project AppName App) check if the codes in it look like the one below otherwise and  google() or jcenter() to the build.gradle see image



allprojects {

repositories {

google()

jcenter()

}
}




b)  Go to
Gradle Scripts and select build.gradle (Module App) 
under dependencies paste this code 

implementation 'com.google.android.gms:play-services-ads:18.3.0'
see example below

dependencies {

implementation fileTree(dir: 'libs', include: ['*.jar'])

implementation 'androidx.appcompat:appcompat:1.0.2'

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

}


We have updated our Android Studio app with the latest Admob integration gradle.

2. Lets now add and tell our Android App project about Admob ads codes.

a) Open Android Manifest file and add the following code just before closing </application>

android:name="com.google.android.gms.ads.APPLICATION_ID"

android:value="ca-app-pub-xxxxxxxxxxxxxxxx~yyyyyyyyyy"/>

replace 
ca-app-pub-xxxxxxxxxxxxxxxx~yyyyyyyyyy with your admob code or Admob test code below.



b). Go to ManiActivity or any Java Class you want Admob to appear and add below code immediately after setContent view or similar thing. see image

MobileAds.initialize(this, new OnInitializationCompleteListener() {

@Override

public void onInitializationComplete(InitializationStatus initializationStatus) {

     }

});




c) import below items or click Alt + Enter to enable Android Studio import it automatically

import com.google.android.gms.ads.MobileAds;

import com.google.android.gms.ads.initialization.InitializationStatus;

import com.google.android.gms.ads.initialization.OnInitializationCompleteListener;


We are done telling our Android App about our Admob so next thing is to add banner and Interstitial Ads. I use Banner Admob ads mostly for good looking android app.


3. In this part we are going to add Banner Admob ads 

a). Go to any XML UI file you want your Admob to show and add below widget
ads banner add in mainActivity

<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-XXXXXXXXXXXXXXXXX/XXXXXX ">

</com.google.android.gms.ads.AdView>

You can edit the below code to suit your User interface. Add other constraint and also chang the attribute but be careful you are dealing with Google.

Replace ca-app-pub-XXXXXXXXXXXXXXXXX/XXXXXX with your Admob banner code

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:layout_centerHorizontal="true"

android:layout_alignParentBottom="true"


b). Go to Activity Java and under added code in No 2b) add below codes

mAdView = findViewById(R.id.adView);

AdRequest adRequest = new AdRequest.Builder().build();

mAdView.loadAd(adRequest);

}

c). import/add below items or click Alt + Enter to enable Android Studio import it automatically

import com.google.android.gms.ads.AdRequest;

import com.google.android.gms.ads.AdView;



d). Remember to populate the global view for banner where you declare public class see image example below. Add below red code.



public class MainActivity extends AppCompatActivity {

private AdView mAdView;

Ensure you do this anywhere you want you Admob Banner ads to display.


4. Here we are going to add InterstitialAD Admob code just as we did on Banner.

a). Go to Activity Java eg Mainactivity and add under No 3b)  below codes (if you already added Banner Admob Codes to your project).

mInterstitialAd = new InterstitialAd(this);
mInterstitialAd.setAdUnitId("ca-app-pub-3940256099942544/1033173712");

b). Go to Activity Java eg Mainactivity and add under No 6b) below codes

public class MainActivity extends AppCompatActivity {

private InterstitialAd mInterstitialAd;



c). Import/add below items or click Alt + Enter to enable Android Studio import it automatically

import com.google.android.gms.ads.AdRequest;
import com.google.android.gms.ads.InterstitialAd;


For further customization of Interstitiall Admob you can add this code below any onClickListener.

mMyButton.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View v) {
        if (mInterstitialAd.isLoaded()) {
            mInterstitialAd.show();
        } else {
            Log.d("TAG", "The interstitial wasn't loaded yet.");
        }
    }
});


Note. Remember to use Test ads first before you replace you own code Go to Admob Page get:- 

Admob Test ID
ca-app-pub-3940256099942544~3347511713

Admob Test Banner ID
ca-app-pub-3940256099942544/6300978111

Admob Test InterstitialAd
ca-app-pub-3940256099942544/1033173712.

No comments:

Post a Comment