Watch Video To Implementation Smart Admob Library
- Add it in your root
settings.gradle
at the end of repositories
maven { url 'https://jitpack.io' }
Step 2. Add the dependency in
build.gradle
implementation 'com.github.AtikulSoftware:SmartAdmob:6.0'
implementation 'com.google.android.gms:play-services-ads:23.2.0'
- Ad Some Permissions inside
AndroidManifest.xml
<!-- Ad And Internet Permission -->
<uses-permission android:name="android.permission.INTERNET"/>
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>
<uses-permission android:name="com.google.android.gms.permission.AD_ID"/>
<!-- Admob Meta Data (inside the application tag) -->
<meta-data
android:name="com.google.android.gms.ads.APPLICATION_ID"
android:value="ca-app-pub-3940256099942544~3347511713"/>
- How To Set Ad Unit ID ?
AdmobAdUnit.ADMOB_BANNER_AD = "ca-app-pub-3940256099942544/9214589741";
AdmobAdUnit.ADMOB_INTERSTITIAL_AD = "ca-app-pub-3940256099942544/1033173712";
AdmobAdUnit.ADMOB_REWARDED_AD = "ca-app-pub-3940256099942544/5224354917";
AdmobAdUnit.ADMOB_NATIVE_AD = "ca-app-pub-3940256099942544/2247696110";
How to use Admob Banner Ad ?
Add AdView to the layout
XML CODE (Layout) -
<LinearLayout
android:id="@+id/bannerAd"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:layout_alignParentBottom="true"/>
JAVA CODE -
new AdmobAd(this, new AdmobAdCallBack() {
@Override
public void onAdClicked() {
AdmobAdCallBack.super.onAdClicked();
}
}).initializeAdmobAd().loadBanner(findViewById(R.id.bannerAd));
Note
To further customize the behavior of your ad, you can hook onto a number of events in the ad's lifecycle: loading, opening, closing, and so on. You can listen for these events through the AdListener class.
To use an AdListener with `AdView`, call the `setAdListener()` method: ?
new AdmobAd(this, new AdmobAdCallBack() {
@Override
public void onAdClicked() {
}
@Override
public void onAdClosed() {
}
@Override
public void onAdFailedToLoad(LoadAdError loadAdError) {
}
@Override
public void onAdImpression() {
}
@Override
public void onAdLoaded() {
}
@Override
public void onAdOpened() {
}
}).initializeAdmobAd().loadBanner(findViewById(R.id.bannerAd));
How to use Interstitial Ad ?
new AdmobAd(this, new AdmobAdCallBack() {
}).loadAdmobInterstitialAd().showAdmobInterstitial(true);
Note
The FullScreenContentCallback handles events related to displaying your InterstitialAd. Before showing InterstitialAd, make sure to set the callback:
Set the `FullScreenContentCallback` ?
new AdmobAd(this, new AdmobAdCallBack() {
@Override
public void onAdClicked() {
}
@Override
public void onAdFailedToLoad(LoadAdError loadAdError) {
}
@Override
public void onAdFailedToShowFullScreenContent(AdError adError) {
}
@Override
public void onAdImpression() {
}
@Override
public void onAdShowedFullScreenContent() {
}
@Override
public void onAdDismissed() {
// go to another activity
}
}).loadAdmobInterstitialAd().showAdmobInterstitial(true);
How to use Rewarded Ad ?
new AdmobAd(this, new AdmobAdCallBack() {
}).loadAdmobRewardedAd().showAdmobRewardedAd(true);
Note
The FullScreenContentCallback handles events related to displaying your RewardedAd. Before you show your RewardedAd, make sure to set the callback like so:
Set the FullScreenContentCallback ?
new AdmobAd(this, new AdmobAdCallBack() {
@Override
public void onAdDismissed() {
}
@Override
public void onAdClicked() {
}
@Override
public void onAdDismissedFullScreenContent() {
}
@Override
public void onAdFailedToShowFullScreenContent(AdError adError) {
}
@Override
public void onAdImpression() {
}
@Override
public void onAdShowedFullScreenContent() {
}
@Override
public void onAdFailedToLoad(LoadAdError loadAdError) {
}
@Override
public void onUserEarnedReward(RewardItem rewardItem) {
}
}).loadAdmobRewardedAd().showAdmobRewardedAd(true);
How to use Native Ad ?
Native Templates
Download Native Templates
XML CODE -
<com.google.android.ads.nativetemplates.TemplateView
android:layout_below="@id/RewordedAd"
android:visibility="gone"
android:id="@+id/my_template"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:gnt_template_type="@layout/gnt_small_template_view" />
JAVA CODE-
TemplateView templateView = findViewById(R.id.my_template);
new AdmobAd(this, new AdmobAdCallBack() {
@Override
public void onNativeAdLoaded(NativeAd nativeAd) {
AdmobAdCallBack.super.onNativeAdLoaded(nativeAd);
templateView.setVisibility(View.VISIBLE);
}
}).initializeAdmobAd().loadAdmobNativeAd(templateView);
- Create a Java Class Name
MyApplication.java
public class MyApplication extends OpenAdManager {
@Override
public void onCreate() {
super.onCreate();
}
}
- Do some changes in
AndroidManifest.xml
inside application tag add nameandroid:name=".MyApplication"
<application
...
android:name=".MyApplication"
...
</application>
- Create a Activity name
SplashScreen
public class SplashScreen extends AppCompatActivity {
SplashManager splashManager;
TextView tvResult;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_splash_screen);
// initialize
tvResult = findViewById(R.id.tvResult);
// set ad unit id
AdmobAdUnit.ADMOB_OPEN_AD = "ca-app-pub-3940256099942544/9257395921";
//If you want you can set splash screen time (Default Time: 5 seconds)
AdmobConfig.COUNTER_TIME_MILLISECONDS = 5000;
// for open ad
splashManager = new SplashManager(this, new SplashOpenAdCallBack() {
@Override
public void startYourActivity() {
startActivity(new Intent(SplashScreen.this, MainActivity.class));
finish();
}
@Override
public void loadingTimer(long time) {
runOnUiThread(() -> tvResult.setText(String.valueOf(time)));
}
});
splashManager.initialize();
} // onCreate bundle end here ==========
} // public class end here =================
MyApplication.java | AndroidManifest.xml | SplashScreen.java |
---|---|---|
GDPR gdpr = new GDPR(this);
gdpr.setGDPR();
AdmobAdUnit.ADMOB_AD_IS_ON = false;
Distributed under the MIT License. See LICENSE for more information.
- Md Atikul Islam - Android Software Developer - Md Atikul Islam - Easy & Quick Solution