Today we will learn in this article how we will implement interstitial ad in navigation item.Sometimes we have to go from navigation item to another activity and we want to load interstitial ad at the same time, for this we have some problem how we will implement it.
First of all, in the project in which we want to put an ad, we will do it for a dependancy.
whose code is given below...
implementation 'com.google.android.gms:play-services-ads:20.4.0'
First of all we will create a variable of type InterstitialAd in this MainActivity. Before this,if you are facing problem in implementing navigation drawer then click
here.
After that we have to initialize MobileAds in onCreate
loadAdd(); We will create the method whose code is given below.
import androidx.annotation.NonNull;
import androidx.appcompat.app.ActionBarDrawerToggle;
import androidx.appcompat.app.AppCompatActivity;
import androidx.appcompat.widget.Toolbar;
import androidx.core.view.GravityCompat;
import androidx.drawerlayout.widget.DrawerLayout;
import android.content.Intent;
import android.os.Bundle;
import android.view.MenuItem;
import android.widget.Toast;
import com.google.android.gms.ads.AdRequest;
import com.google.android.gms.ads.FullScreenContentCallback;
import com.google.android.gms.ads.LoadAdError;
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;
import com.google.android.material.navigation.NavigationView;
public class MainActivity extends AppCompatActivity implements NavigationView.OnNavigationItemSelectedListener {
private Toolbar toolbar;
private NavigationView navigationView;
DrawerLayout drawerLayout;
private InterstitialAd mInterstitialAd;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
MobileAds.initialize(this, new OnInitializationCompleteListener() {
@Override
public void onInitializationComplete(InitializationStatus initializationStatus) {
}
});
loadAd();
toolbar = findViewById(R.id.toolbar);
navigationView = findViewById(R.id.navigation);
drawerLayout = findViewById(R.id.drawer);
setSupportActionBar(toolbar);
ActionBarDrawerToggle toogle = new ActionBarDrawerToggle(this, drawerLayout, toolbar, R.string.open, R.string.close);
drawerLayout.addDrawerListener(toogle);
toogle.syncState();
navigationView.setNavigationItemSelectedListener(this);
}
private void loadAd() {
AdRequest adRequest = new AdRequest.Builder().build();
InterstitialAd.load(this, "ca-app-pub-3940256099942544/1033173712", adRequest, new InterstitialAdLoadCallback() {
@Override
public void onAdLoaded(@NonNull InterstitialAd interstitialAd) {
mInterstitialAd = interstitialAd;
}
@Override
public void onAdFailedToLoad(@NonNull LoadAdError loadAdError) {
mInterstitialAd = null;
}
});
}
@Override
protected void onResume() {
super.onResume();
}
@Override
public void onBackPressed() {
DrawerLayout drawer = findViewById(R.id.drawer);
if (drawer.isDrawerOpen(GravityCompat.START)) {
drawer.closeDrawer(GravityCompat.START);
}
}
@Override
public boolean onNavigationItemSelected(@NonNull MenuItem item) {
int getitem = item.getItemId();
if (getitem == R.id.flower) {
if (mInterstitialAd != null) {
mInterstitialAd.show(MainActivity.this);
mInterstitialAd.setFullScreenContentCallback(new FullScreenContentCallback() {
@Override
public void onAdDismissedFullScreenContent() {
super.onAdDismissedFullScreenContent();
startActivity(new Intent(MainActivity.this, FlowerActivity.class));
mInterstitialAd = null;
loadAd();
}
});
} else {
startActivity(new Intent(MainActivity.this, FlowerActivity.class));
// loadAd();
}
} else if (getitem == R.id.share) {
Toast.makeText(this, "Share", Toast.LENGTH_SHORT).show();
} else if (getitem == R.id.message) {
Toast.makeText(this, "Message", Toast.LENGTH_SHORT).show();
}
drawerLayout = findViewById(R.id.drawer);
drawerLayout.closeDrawer(GravityCompat.START);
return true;
}
}
We are seeing that a variable named adRequest has been created in the loadAd method and as the parameter InterstitialAd.load, paste the context, interstial ad unit id, in which if you have created a test project, then only the test unit id should do it, otherwise your admob account is disabled or Can be suspended and use only the original ad unit in the main project.
Now we have to use this code on the item on which we want to show interstitial ad on click in onNavigationItemSelected.
@Override
public boolean onNavigationItemSelected(@NonNull MenuItem item) {
int getitem = item.getItemId();
if (getitem == R.id.flower) {
if (mInterstitialAd != null) {
mInterstitialAd.show(MainActivity.this);
mInterstitialAd.setFullScreenContentCallback(new FullScreenContentCallback() {
@Override
public void onAdDismissedFullScreenContent() {
super.onAdDismissedFullScreenContent();
startActivity(new Intent(MainActivity.this, FlowerActivity.class));
mInterstitialAd = null;
loadAd();
}
});
} else {
startActivity(new Intent(MainActivity.this, FlowerActivity.class));
// loadAd();
}
} else if (getitem == R.id.share) {
Toast.makeText(this, "Share", Toast.LENGTH_SHORT).show();
} else if (getitem == R.id.message) {
Toast.makeText(this, "Message", Toast.LENGTH_SHORT).show();
}
drawerLayout = findViewById(R.id.drawer);
drawerLayout.closeDrawer(GravityCompat.START);
return true;
}
In this code we are seeing public void onAdDismissedFullScreenContent() it means when we close it after InterstitialAd show then the activity which we want to show will be show but once it is show then it will not show again.
To make the show again, we can use mInterstitialAd = null
and then we will call loadAd() method which will show the internal ad again when we click on that item. If there is no internet connection then interstitial ad will not show and most important we have to give app id and internet permission in manifest. Whose code is given below.
Manifest<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.navigationads">
<uses-permission android:name="android.permission.INTERNET"/>
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/Theme.NavigationAds">
<!-- Sample AdMob App ID: ca-app-pub-3940256099942544~3347511713 -->
<!-- Sample AdMob App ID: ca-app-pub-3940256099942544~3347511713 -->
<meta-data
android:name="com.google.android.gms.ads.APPLICATION_ID"
android:value="ca-app-pub-3940256099942544~3347511713" />
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity android:name=".FlowerActivity" />
</application>
</manifest>
If you face any problem in implementing this interstitial ad, then you can ask by commenting.
Below is the screenshot how this app will work
Thank you
0 Comments
Please do not enter any spam link in the comment box
Emoji