- Get link
- X
- Other Apps
- Get link
- X
- Other Apps
In this blog, I will tell you about how to implement an Google Admob Interstitial Ads in Android Studio App. You will need to follow these steps.👇
- Create Google ADMOB Account and get it approved.
- Add your App *just app name is enough at this time* and enable statistics.
- Create Ad unit and get API keys; App ID , Ad Unit ID.
- Use actual App_ID and Test_ID for interstitial ads in the program. (Don't use the actual Ad Unit ID
Launch the Android Studio. The code snippet for MainActivity.Java is:
package com.codingtutorials.admobinterstitial;
import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.view.TextureView;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import com.google.android.gms.ads.AdRequest;
import com.google.android.gms.ads.MobileAds;
import com.google.android.gms.ads.initialization.InitializationStatus;
import com.google.android.gms.ads.initialization.OnInitializationCompleteListener;
import com.google.android.gms.ads.interstitial.InterstitialAd;
import com.google.android.gms.ads.interstitial.InterstitialAdLoadCallback;
public class MainActivity extends AppCompatActivity {
private static final String AD_UNIT_ID = " "; % Your Ad Unit ID here
private Button load, show;
private TextView text;
private InterstitialAd interstitialAd;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
load = findViewById(R.id.LoadAdBtn);
show = findViewById(R.id.ShowAdBtn);
text = findViewById(R.id.text);
MobileAds.initialize(this, new OnInitializationCompleteListener() {
@Override
public void onInitializationComplete(@NonNull InitializationStatus initializationStatus) {
} });
load.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
LoadAd();
}});
show.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
ShowAd();
} });
}
private void LoadAd() {
AdRequest adRequest = new AdRequest.Builder().build();
InterstitialAd.load(
this, AD_UNIT_ID, adRequest, new InterstitialAdLoadCallback() {
@Override
public void onAdLoaded(@NonNull InterstitialAd interstitialAd) {
MainActivity.this.interstitialAd = interstitialAd;
text.setText("Ad is ready! Click 'Show'");
} } );
}
private void ShowAd()
{
if(interstitialAd!=null)
{
interstitialAd.show(MainActivity.this);
interstitialAd = null;
text.setText("Ad is shown. Click 'Load' to relaod Ad");
}
else {
text.setText("Ad is not loaded. Click 'Load' first");
} }
}
Code snippet for activity_main.XML is:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity"
android:padding="10dp">
<Button
android:id="@+id/LoadAdBtn"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Load"
android:textSize="20sp"/>
<Button
android:id="@+id/ShowAdBtn"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@id/LoadAdBtn"
android:text="Show"
android:textSize="20sp"/>
<TextView
android:id="@+id/text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@id/ShowAdBtn"
android:layout_centerInParent="true"
android:text="Click 'Load' to start"
android:layout_marginTop="10dp"
android:textSize="24sp"/>
</RelativeLayout>
Code snippet for Manifest (AndroidManifest.XML) is:
(Because we need to add Internet 🌐 permission, and also MetaData)
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools">
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>
<uses-permission android:name="android.permission.INTERNET"/>
<application
android:allowBackup="true"
android:dataExtractionRules="@xml/data_extraction_rules"
android:fullBackupContent="@xml/backup_rules"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/Theme.AdmobInterstitial"
tools:targetApi="31">
<activity
android:name=".MainActivity"
android:exported="true">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<meta-data
android:name="com.google.android.gms.ads.APPLICATION_ID"
android:value="Your App ID here" />
</application>
</manifest>
Build.Gradle (App Level): Add dependency: "com.google.android.gms:play-services-ads-lite:22.6.0".
dependencies {
implementation("androidx.appcompat:appcompat:1.6.1")
implementation("com.google.android.material:material:1.11.0")
implementation("androidx.constraintlayout:constraintlayout:2.1.4")
implementation("com.google.android.gms:play-services-ads-lite:22.6.0")
testImplementation("junit:junit:4.13.2")
androidTestImplementation("androidx.test.ext:junit:1.1.5")
androidTestImplementation("androidx.test.espresso:espresso-core:3.5.1")
}
Your App will look like this 👇👇👇👇👇
Once uploaded in the Play Store, you can use the actual Ad Unit ID and enjoy the real Ads.
That' all folks. 👦 👨
https://www.youtube.com/watch?v=zlnRM3oKYas
https://www.youtube.com/@codingtutorials1857
- Get link
- X
- Other Apps
Comments
Post a Comment