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.
<uses-permission android:name="android.permission.INTERNET"/>
1. We have to add Admob and Google Play Service codes to our project SDK
allprojects {
repositories {
google()
jcenter()
}
b) Go to
Gradle Scripts and select build.gradle (Module App)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.
android:value=”ca-app-pub-xxxxxxxxxxxxxxxx~yyyyyyyyyy”/>
replace
ca-app-pub-xxxxxxxxxxxxxxxx~yyyyyyyyyy with your admob code or Admob test code below.@Override
public void onInitializationComplete(InitializationStatus initializationStatus) {
}
});
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
ads banner add in mainActivity
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>
android:layout_height=”wrap_content”
android:layout_centerHorizontal=”true”
android:layout_alignParentBottom=”true”
AdRequest adRequest = new AdRequest.Builder().build();
mAdView.loadAd(adRequest);
}
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.
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;
ca-app-pub-3940256099942544/6300978111