How to integrate Admob Native Ads in Android Application
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.learnoset.googlenativeads">
<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.GoogleNativeAds">
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<!---Always use Test App Id while testing your application-->
<meta-data
android:name="com.google.android.gms.ads.APPLICATION_ID"
android:value="ca-app-pub-3940256099942544~3347511713" />
</application>
</manifest>
gradle.build (app)
dependencies {
// Implement Google Admob dependency
implementation project(':nativetemplates')
}
templates
1. Download Native Ad Templates from https://github.com/googleads/googleads-mobile-android-native-templates/archive/refs/heads/master.zip
2. Import the downloaded modules in the Android Studio
Note :- If you don't know how to download and import templates then you can watch our video tutorials
available on the Learnoset YouTube channel.
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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"
android:gravity="center"
android:orientation="vertical"
tools:context=".MainActivity">
<!---Showing Native ads in small Template-->
<com.google.android.ads.nativetemplates.TemplateView
android:id="@+id/my_template"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:gnt_template_type="@layout/gnt_small_template_view" />
<!---Showing Native ads in medium Template-->
<com.google.android.ads.nativetemplates.TemplateView
android:id="@+id/my_template2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="20dp"
app:gnt_template_type="@layout/gnt_medium_template_view" />
</LinearLayout>
MainActivity.java
package com.learnoset.googlenativeads;
import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.os.Bundle;
import android.widget.Toast;
import androidx.appcompat.app.AppCompatActivity;
import com.google.android.ads.nativetemplates.NativeTemplateStyle;
import com.google.android.ads.nativetemplates.TemplateView;
import com.google.android.gms.ads.AdListener;
import com.google.android.gms.ads.AdLoader;
import com.google.android.gms.ads.AdRequest;
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.nativead.NativeAd;
import com.google.android.gms.ads.nativead.NativeAdOptions;
import java.util.List;
public class MainActivity extends AppCompatActivity {
private AdLoader adLoader;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// Add below code in your launcher activity like SplashScreen etc.
MobileAds.initialize(this, new OnInitializationCompleteListener() {
@Override
public void onInitializationComplete(InitializationStatus initializationStatus) {
}
});
// load native ads
adLoader = new AdLoader.Builder(MainActivity.this, "ca-app-pub-3940256099942544/2247696110")
.forNativeAd(new NativeAd.OnNativeAdLoadedListener() {
@Override
public void onNativeAdLoaded(NativeAd NativeAd) {
// check if ad has loaded. If you are loading multiple native ads then you must check wheather all the ads loaded
if (!adLoader.isLoading()) {
Toast.makeText(MainActivity.this, "Native ads loaded successfully", Toast.LENGTH_SHORT).show();
}
// check if activity is destroyed. If activity is destroyed then also destroy the Native Ad.
// Otherwise it may cause null pointer exception or Memory leaks
if (isDestroyed()) {
NativeAd.destroy();
}
// we are using Native Ad built - in templates you can also create your own layout to show native ads.
NativeTemplateStyle styles = new
NativeTemplateStyle.Builder().withMainBackgroundColor(new ColorDrawable(Color.WHITE)).build(); // setting native ad background color
// small template
TemplateView template = findViewById(R.id.my_template);
// medium template
TemplateView template2 = findViewById(R.id.my_template2);
template.setStyles(styles);
template.setNativeAd(NativeAd);
template2.setStyles(styles);
template2.setNativeAd(NativeAd);
// OR you can also show native ad in your own Custom Layout
final String getHeadline = NativeAd.getHeadline(); // getting headline from Native Ad
final com.google.android.gms.ads.nativead.NativeAd.Image getIcon = NativeAd.getIcon();
final String getDetails = NativeAd.getBody();
final String getAdvertierName = NativeAd.getAdvertiser();
final String getPrice = NativeAd.getPrice();
final double getRating = NativeAd.getStarRating();
final List<com.google.android.gms.ads.nativead.NativeAd.Image> images = NativeAd.getImages();
}
})
.withAdListener(new AdListener() {
@Override
public void onAdFailedToLoad(LoadAdError adError) {
Toast.makeText(MainActivity.this, "Failed to load native ads", Toast.LENGTH_SHORT).show();
}
})
.withNativeAdOptions(new NativeAdOptions.Builder()
// Methods in the NativeAdOptions.Builder class can be
// used here to specify individual options settings.
.build())
.build();
// making ad request to google's servers
adLoader.loadAd(new AdRequest.Builder().build()); // single ad
// OR
//adLoader.loadAds(new AdRequest.Builder().build(), 3); // multiple ad maximum 5
}
}
Projects with Source Code + Video Tutorials
You can download our Java and Android Studio Projects with Source Code and Video Tutorials.

Simple Music Player App (Version 2) for Android (Offline Version)

Modern Login and Register screen UI design

HD Wallpaper App in Android Studio
If you have any Questions or Queries
You can mail us at info.learnoset@gmail.com
Follow us to learn Coding and get in touch with new Technologies.