How to add Adsmob - Android
Finally got ads on my apps, I thought it's going to be hard but the documentation is so easy to understand, just go to documentation first if stuck go to my notes. I think I had a trouble, it says that my campaign is still running on adsword so I had to fix that by removing the campaign then it would let me register for admob. After registering it takes 2 days to be verified.
Dashboard - https://apps.admob.com/
update :
Little bit of tutorial just the main point, tried banner ads, rewarded ads but didn't get to try interstitial ads (on my future task)
BANNER ADS
#build.gradle
implementation 'com.google.android.gms:play-services-ads:19.1.0'
#manifest
<!-- 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-xxxxxxxxxxxxxxxx~yyyyyyyyyy"/>
# value string
<string name="ad_unit_id">ca-app-pub-3940256/6300</string>
Initialize after on create
MobileAds.initialize(this, new OnInitializationCompleteListener() {
@Override
public void onInitializationComplete(InitializationStatus initializationStatus) {
}
});
create the view in here I use the responsive ads
<FrameLayout
android:id="@+id/ad_view_container"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:layout_alignParentBottom="true" />
In on create after initialize
adContainerView = findViewById(R.id.ad_view_container);
// Step 1 - Create an AdView and set the ad unit ID on it.
adView = new AdView(this);
adView.setAdUnitId(getString(R.string.ad_unit_id));
adContainerView.addView(adView);
loadBanner();
then make private function in the activity
private void loadBanner() {
// Create an ad request. Check your logcat output for the hashed device ID
// to get test ads on a physical device, e.g.,
// "Use AdRequest.Builder.addTestDevice("ABCDE0123") to get test ads on this
// device."
AdRequest adRequest =
new AdRequest.Builder().addTestDevice(AdRequest.DEVICE_ID_EMULATOR)
.build();
AdSize adSize = getAdSize();
// Step 4 - Set the adaptive ad size on the ad view.
adView.setAdSize(adSize);
// Step 5 - Start loading the ad in the background.
adView.loadAd(adRequest);
}
private AdSize getAdSize() {
// Step 2 - Determine the screen width (less decorations) to use for the ad width.
Display display = getWindowManager().getDefaultDisplay();
DisplayMetrics outMetrics = new DisplayMetrics();
display.getMetrics(outMetrics);
float widthPixels = outMetrics.widthPixels;
float density = outMetrics.density;
int adWidth = (int) (widthPixels / density);
// Step 3 - Get adaptive ad size and return for setting on the ad view.
return AdSize.getCurrentOrientationAnchoredAdaptiveBannerAdSize(this, adWidth);
}
REWARDED ADS
Also I tried the rewarded ads, this is when you want the user willingly open your ads in exchange of maybe reward.
Activity
rewardedAd = new RewardedAd(this,
"ca-app-pub-3940256099942544/5224354917");
RewardedAdLoadCallback adLoadCallback = new RewardedAdLoadCallback() {
@Override
public void onRewardedAdLoaded() {
// Ad successfully loaded.
}
@Override
public void onRewardedAdFailedToLoad(int errorCode) {
// Ad failed to load.
}
};
rewardedAd.loadAd(new AdRequest.Builder().build(), adLoadCallback);
button_support.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (rewardedAd.isLoaded()) {
Activity activityContext = DetailActivity.this;
RewardedAdCallback adCallback = new RewardedAdCallback() {
@Override
public void onRewardedAdOpened() {
// Ad opened.
}
@Override
public void onRewardedAdClosed() {
// Ad closed.
}
@Override
public void onUserEarnedReward(@NonNull RewardItem reward) {
// User earned reward.
}
@Override
public void onRewardedAdFailedToShow(int errorCode) {
// Ad failed to display.
}
};
rewardedAd.show(activityContext, adCallback);
} else {
Log.d("TAG", "The rewarded ad wasn't loaded yet.");
}
}
});
View
<Button
android:id="@+id/supportUs"
android:layout_width="wrap_content"
android:layout_gravity="center"
android:text="Support Us"
android:layout_height="wrap_content" />