This is the Advance part for implementing google AdMob native ads advanced (Unified) in android apps.
Note: If you have completed Part 1 basic then only proceed
Click here to complete a basic setting for Admob native ads. Using the ScrollView component for displaying google AdMob native ads advanced in android apps.
Also read: AdMob live ads are Not Showing in real Device 2019
Google Admob Native Ads Advanced Implementation
Example codes and native ads template (unified) For Displaying AdMob native ads advanced.
Step 1: Open your project where you want to use native ad Step 2: Inside your app Package
Step 3: Select Resource (name as Res) > Then Layout >
And then open your Main Activity layout where you want to display native ad And paste this code.
This code is responsible for creating ad space in your Activity Native ad will display
<ScrollView
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".Activity_main">
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:orientation="vertical">
<FrameLayout android:id="@+id/fl_adplaceholder"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center" />
</LinearLayout>
</ScrollView>
Also Read: Tutorial – Admob native Ads Advanced Integration using Android Studio
Creating New Resource file for Native Ads Advanced Components
Select Layout > new XML Named as ad_unified
And then remove the default code and paste the following code. The following code is used to design the format of native advertising that will match the form and function of the App upon which it will appear you can customize it according to your need.
<com.google.android.gms.ads.formats.UnifiedNativeAdView xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="wrap_content">
<LinearLayout
android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_gravity="center" android:background="#FFFFFF"
android:minHeight="50dp"
android:orientation="vertical">
<TextView style="@style/AppTheme.AdAttribution"/> <LinearLayout
android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="vertical"
android:paddingLeft="20dp"
android:paddingRight="20dp"
android:paddingTop="3dp">
<LinearLayout
android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="horizontal">
<ImageView
android:id="@+id/ad_app_icon"
android:layout_width="40dp"
android:layout_height="40dp" android:adjustViewBounds="true" android:paddingBottom="5dp"
android:paddingEnd="5dp"
android:paddingRight="5dp"/>
<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="#0000FF"
android:textSize="16sp"
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="14sp"
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_marginRight="20dp" android:layout_marginEnd="20dp"
android:textSize="12sp" />
<com.google.android.gms.ads.formats.MediaView android:id="@+id/ad_media" android:layout_gravity="center_horizontal" android:layout_width="250dp"
android:layout_height="175dp" android:layout_marginTop="5dp" />
<LinearLayout
android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="center" android:orientation="horizontal" android:paddingBottom="10dp"
android:paddingTop="10dp">
<TextView
android:id="@+id/ad_price" android:layout_width="wrap_content" android:layout_height="wrap_content" android:paddingLeft="5dp"
android:paddingStart="5dp"
android:paddingRight="5dp"
android:paddingEnd="5dp"
android:textSize="12sp" />
<TextView
android:id="@+id/ad_store" android:layout_width="wrap_content" android:layout_height="wrap_content" android:paddingLeft="5dp"
android:paddingStart="5dp"
android:paddingRight="5dp"
android:paddingEnd="5dp"
android:textSize="12sp" />
<Button
android:id="@+id/ad_call_to_action" android:layout_width="wrap_content" android:layout_height="wrap_content" android:gravity="center"
android:textSize="12sp" />
</LinearLayout>
</LinearLayout>
</LinearLayout>
</LinearLayout>
</com.google.android.gms.ads.formats.UnifiedNativeAdView>
Adding Native Ads Advanced Theme
Go to Res > values > open style.xml and paste the following code which will be used to creating Ad theme and design.
<resources>
<!-- Base application theme. -->
<style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
<!-- Customize your theme here. -->
</style>
<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>
</resources>
Previously Done on PART 1 Native Ads Advanced Unified
Note: just Add nativeAdmedia(); – highlighted in above image.
private void populateUnifiedNativeAdView(UnifiedNativeAd nativeAd, UnifiedNativeAdView adView) {
// Set the media view.
Media content will be automatically populated in the media view once
// adView.setNativeAd() is called.
MediaView mediaView = adView.findViewById(R.id.ad_media); adView.setMediaView(mediaView);
// 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 is guaranteed to be in every UnifiedNativeAd.
((TextView) adView.getHeadlineView()).setText(nativeAd.getHeadline()); // 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. The SDK will populate the adView's MediaView
// with the media content from this native ad.
adView.setNativeAd(nativeAd);
}
private void nativeAdsmedia()
{
AdLoader.Builder builder = new AdLoader.Builder(this, ADMOB_AD_UNIT_ID); builder.forUnifiedNativeAd(new UnifiedNativeAd.OnUnifiedNativeAdLoadedListener()
{
// OnUnifiedNativeAdLoadedListener implementation.
@Override public void onUnifiedNativeAdLoaded(UnifiedNativeAd unifiedNativeAd) {
// You must call destroy on old ads when you are done with them,
// otherwise you will have a memory leak. if (nativeAd != null) {
nativeAd.destroy(); } nativeAd = unifiedNativeAd; FrameLayout frameLayout = findViewById(R.id.fl_adplaceholder);
UnifiedNativeAdView adView = (UnifiedNativeAdView) getLayoutInflater() .inflate(R.layout.ad_unified, null); populateUnifiedNativeAdView(unifiedNativeAd, adView); frameLayout.removeAllViews(); frameLayout.addView(adView);
}
});
AdLoader adLoader = builder.withAdListener(new AdListener()
{
@Override public void onAdFailedToLoad(int errorCode)
{
Toast.makeText(splash_diwali.this, "Failed to load native ad: " + errorCode, Toast.LENGTH_SHORT).show();
}
}).build();
adLoader.loadAd(new AdRequest.Builder().build());
}
@Overrideprotected void onDestroy()
{
if (nativeAd != null) { nativeAd.destroy();
} super.onDestroy();
}
}
Nice, you are all set to display Google AdMob native advanced ads in your android apps. comment down your doubts if any.
you can simply switch scrollview into cardview
this code show only one add on scroll view i want multiple add ,, how can add multiple ad in scroll and card view
Thanks alot brother
ek dum sahi he bhai nice work
i like it