how to add admob in android app :
AdMob offers a number of different ad formats, so you can choose the one that best fits your app's user experience.:
1-Banner Add-
Banner graphics give you the freedom to portray your product in a way that connects to the customers. It is very important to inform your customers about your product to make it a brand name that people can recognize. For this, they need to be informed about it time and again, until they know it well
2-Interstitial
Full-screen ads that cover the interface of an app until closed by the user. They're best used at natural pauses in the flow of an app's execution, such as between levels of a game or just after a task is completed.
3- Native add:
Customizable ads that match the look and feel of your app. You decide how and where they're placed, so the layout is more consistent with your app's design.
1-how to add banner ads in android studio:
Step-1: Activity Main.java code :
MobileAds.initialize(BlogAuthorPostActivity.this, new OnInitializationCompleteListener() {
@Override
public void onInitializationComplete(InitializationStatus initializationStatus) {
}
});
AdView adView=(AdView) findViewById(R.id.adView2);
AdView adView2=(AdView) findViewById(R.id.adView);
//adRequest=new AdRequest.Builder().AddTestDeveice().build();
AdRequest adRequest=new AdRequest.Builder().build();
adView.loadAd(adRequest);
AdRequest adRequest2=new AdRequest.Builder().build();
adView2.loadAd(adRequest2);
adView.setAdListener(new AdListener() {
@Override
public void onAdClosed() {
super.onAdClosed();
Log.d("ads-test","Banner ad closed");
}
@Override
public void onAdFailedToLoad(@NonNull LoadAdError error) {
super.onAdFailedToLoad(error);
Log.d("ads-test","Banner ad FailedToLoad");
}
@Override
public void onAdOpened() {
super.onAdOpened();
Log.d("ads-test","Banner ad AdOpened");
}
@Override
public void onAdLoaded() {
super.onAdLoaded();
Log.d("ads-test","Banner ad AdLoaded");
}
@Override
public void onAdClicked() {
// Code to be executed when the user clicks on an ad.
}
});
adView2.setAdListener(new AdListener() {
@Override
public void onAdClosed() {
super.onAdClosed();
Log.d("ads-test","Banner ad closed");
}
@Override
public void onAdFailedToLoad(@NonNull LoadAdError error) {
super.onAdFailedToLoad(error);
Log.d("ads-test","Banner ad FailedToLoad");
}
@Override
public void onAdOpened() {
super.onAdOpened();
Log.d("ads-test","Banner ad AdOpened");
}
@Override
public void onAdLoaded() {
super.onAdLoaded();
Log.d("ads-test","Banner ad AdLoaded");
}
@Override
public void onAdClicked() {
// Code to be executed when the user clicks on an ad.
}
});
Step-2: Activity Main.XML code :
<com.google.android.gms.ads.AdView
android:id="@+id/adView2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@+id/lllr"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
ads:adSize="BANNER"
ads:adUnitId="@string/banner_ad_unit_id" />
<com.google.android.gms.ads.AdView
android:id="@+id/adView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@+id/lllr"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
ads:adSize="BANNER"
ads:adUnitId="@string/banner_ad_unit_id" />
xmlns:ads="http://schemas.android.com/apk/res-auto" add That code in first stage of xml code :
2-how to implement native ads in android studio :
Step-1:Activity Main ,java code :
AdLoader.Builder builder1 = new AdLoader.Builder(context,
currentActivity.getString(R.string.native_ad_unit_id))
.forNativeAd(new NativeAd.OnNativeAdLoadedListener() {
@Override
public void onNativeAdLoaded(@NonNull NativeAd nativeAd) {
NativeAdView nativeAdView = (NativeAdView) currentActivity
.getLayoutInflater().inflate(R.layout.layout_native_ad, null);
populateNativeADView(nativeAd, nativeAdView);
//context.adLayout.removeAllViews();
//binding.adLayout.addView(nativeAdView);
frameLayout1.removeAllViews();
frameLayout1.addView(nativeAdView);
}
});
AdLoader adLoader1 = builder1.withAdListener(new AdListener() {
@Override
public void onAdFailedToLoad(@NonNull LoadAdError loadAdError) {
super.onAdFailedToLoad(loadAdError);
//Toast.makeText(currentActivity, loadAdError.getMessage(), Toast.LENGTH_SHORT).show();
}
}).build();
adLoader1.loadAd(new AdRequest.Builder().build());
//NativeAd 1@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
//NativeAdAd 2####################################################################
AdLoader.Builder builder2 = new AdLoader.Builder(context, currentActivity
.getString(R.string.native_ad_unit_id))
.forNativeAd(new NativeAd.OnNativeAdLoadedListener() {
@Override
public void onNativeAdLoaded(@NonNull NativeAd nativeAd) {
NativeAdView nativeAdView = (NativeAdView) currentActivity
.getLayoutInflater().inflate(R.layout.layout_native_ad, null);
populateNativeADView(nativeAd, nativeAdView);
//context.adLayout.removeAllViews();
//binding.adLayout.addView(nativeAdView);
frameLayout2.removeAllViews();
frameLayout2.addView(nativeAdView);
}
});
builder2.withAdListener(new AdListener() {
@Override
public void onAdFailedToLoad(@NonNull LoadAdError loadAdError) {
super.onAdFailedToLoad(loadAdError);
//Toast.makeText(currentActivity, loadAdError.getMessage(), Toast.LENGTH_SHORT).show();
}
});
AdLoader adLoader2 = builder2.build();
adLoader2.loadAd(new AdRequest.Builder().build());
//NativeAdAd 2@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
}
private void populateNativeADView(NativeAd nativeAd, NativeAdView adView) {
// Set the media view.
adView.setMediaView(adView.findViewById(R.id.ad_media));
// Set other ad assets.
adView.setHeadlineView(adView.findViewById(R.id.ad_headline));
adView.setBodyView(adView.findViewById(R.id.ad_body));
adView.setCallToActionView(adView.findViewById(R.id.ad_call_to_action));
adView.setIconView(adView.findViewById(R.id.ad_app_icon));
adView.setPriceView(adView.findViewById(R.id.ad_price));
adView.setStarRatingView(adView.findViewById(R.id.ad_stars));
adView.setStoreView(adView.findViewById(R.id.ad_store));
adView.setAdvertiserView(adView.findViewById(R.id.ad_advertiser));
// The headline and mediaContent are guaranteed to be in every UnifiedNativeAd.
((TextView) adView.getHeadlineView()).setText(nativeAd.getHeadline());
adView.getMediaView().setMediaContent(nativeAd.getMediaContent());
// These assets aren't guaranteed to be in every UnifiedNativeAd, so it's important to
// check before trying to display them.
if (nativeAd.getBody() == null) {
adView.getBodyView().setVisibility(View.INVISIBLE);
} else {
adView.getBodyView().setVisibility(View.VISIBLE);
((TextView) adView.getBodyView()).setText(nativeAd.getBody());
}
if (nativeAd.getCallToAction() == null) {
adView.getCallToActionView().setVisibility(View.INVISIBLE);
} else {
adView.getCallToActionView().setVisibility(View.VISIBLE);
((Button) adView.getCallToActionView()).setText(nativeAd.getCallToAction());
}
if (nativeAd.getIcon() == null) {
adView.getIconView().setVisibility(View.GONE);
} else {
((ImageView) adView.getIconView()).setImageDrawable(
nativeAd.getIcon().getDrawable());
adView.getIconView().setVisibility(View.VISIBLE);
}
if (nativeAd.getPrice() == null) {
adView.getPriceView().setVisibility(View.INVISIBLE);
} else {
adView.getPriceView().setVisibility(View.VISIBLE);
((TextView) adView.getPriceView()).setText(nativeAd.getPrice());
}
if (nativeAd.getStore() == null) {
adView.getStoreView().setVisibility(View.INVISIBLE);
} else {
adView.getStoreView().setVisibility(View.VISIBLE);
((TextView) adView.getStoreView()).setText(nativeAd.getStore());
}
if (nativeAd.getStarRating() == null) {
adView.getStarRatingView().setVisibility(View.INVISIBLE);
} else {
((RatingBar) adView.getStarRatingView()).setRating(nativeAd.getStarRating().floatValue());
adView.getStarRatingView().setVisibility(View.VISIBLE);
}
if (nativeAd.getAdvertiser() == null) {
adView.getAdvertiserView().setVisibility(View.INVISIBLE);
} else {
((TextView) adView.getAdvertiserView()).setText(nativeAd.getAdvertiser());
adView.getAdvertiserView().setVisibility(View.VISIBLE);
}
// This method tells the Google Mobile Ads SDK that you have finished populating your
// native ad view with this native ad.
adView.setNativeAd(nativeAd);
}
Step-2: Activity Main.XML code :
<FrameLayout
android:id="@+id/adLayout2"
android:layout_width="match_parent"
android:layout_height="match_parent" /><FrameLayout
android:id="@+id/adLayout4"
android:layout_width="match_parent"
android:layout_height="match_parent" />
Step-3: Add meta tag on Manifest.xml file :
<meta-data
android:name="com.google.android.gms.ads.APPLICATION_ID"
android:value="@string/admob_app_id"/>
step-4: Add , add unit id on String .xml :
<string name="banner_ad_unit_id">put id here</string>
<string name="native_ad_unit_id">put id here</string>
<string name="admob_app_id">put id here</string> <string name="ad_attribution">AD</string>
step-4: put dependency on build Gradle:
implementation 'com.google.android.gms:play-services-ads:20.4.0'
Step-5 : put style on style.xml:
<style name="AppTheme.AdAttribution">
<item name="android:layout_width">wrap_content</item>
<item name="android:layout_height">wrap_content</item>
<item name="android:layout_gravity">left</item>
<item name="android:textColor">#FFFFFF</item>
<item name="android:textSize">12sp</item>
<item name="android:text">@string/ad_attribution</item>
<item name="android:background">#FFCC66</item>
<item name="android:width">15dp</item>
<item name="android:height">15dp</item>
</style>
Step 6- Create Layout for native Add ; it is visible to user as a Add :
<com.google.android.gms.ads.nativead.NativeAdView
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools">
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:background="@color/white"
android:minHeight="50dp"
android:orientation="vertical">
<View
android:id="@+id/divider5"
android:layout_width="match_parent"
android:layout_height="@dimen/fivedp"
android:background="?android:attr/listDivider"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/adcard"
tools:ignore="MissingConstraints" />
<TextView style="@style/AppTheme.AdAttribution" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:paddingLeft="@dimen/padding_above_ten"
android:paddingTop="@dimen/spacing_control_half3"
android:paddingRight="@dimen/padding_above_ten">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<ImageView
android:id="@+id/ad_app_icon"
android:layout_width="@dimen/dimens_40dp"
android:layout_height="@dimen/dimens_40dp"
android:adjustViewBounds="true"
android:paddingEnd="@dimen/fivedp"
android:paddingRight="@dimen/fivedp"
android:paddingBottom="@dimen/fivedp" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<TextView
android:id="@+id/ad_headline"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textColor="@color/linkColor"
android:textSize="@dimen/font_large"
android:textStyle="bold" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<TextView
android:id="@+id/ad_advertiser"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:gravity="bottom"
android:textSize="@dimen/font_medium"
android:textStyle="bold" />
<RatingBar
android:id="@+id/ad_stars"
style="?android:attr/ratingBarStyleSmall"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:isIndicator="true"
android:numStars="5"
android:stepSize="0.5" />
</LinearLayout>
</LinearLayout>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<TextView
android:id="@+id/ad_body"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginEnd="@dimen/padding_above_ten"
android:layout_marginRight="@dimen/padding_above_ten"
android:textSize="@dimen/font_small" />
<com.google.android.gms.ads.nativead.MediaView
android:id="@+id/ad_media"
android:layout_width="match_parent"
android:layout_height="@dimen/dimens_175dp"
android:layout_gravity="center_horizontal"
android:layout_marginTop="@dimen/fivedp" />
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="end"
android:orientation="horizontal"
android:paddingTop="@dimen/padding_notify"
android:paddingBottom="@dimen/padding_notify">
<TextView
android:id="@+id/ad_price"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:paddingStart="@dimen/fivedp"
android:paddingLeft="@dimen/fivedp"
android:paddingEnd="@dimen/fivedp"
android:paddingRight="@dimen/fivedp"
android:textSize="@dimen/font_small" />
<TextView
android:id="@+id/ad_store"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:paddingStart="@dimen/fivedp"
android:paddingLeft="@dimen/fivedp"
android:paddingEnd="@dimen/fivedp"
android:paddingRight="@dimen/fivedp"
android:textSize="@dimen/font_small" />
<Button
android:id="@+id/ad_call_to_action"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center"
android:textSize="@dimen/font_small" />
</LinearLayout>
</LinearLayout>
</LinearLayout>
</LinearLayout>
</com.google.android.gms.ads.nativead.NativeAdView>
✅ Tags
Android Coding