Created
November 15, 2022 05:13
-
-
Save harsh1772/2b2bedba8653189f13b7ce2744df5867 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Copyright 2021 Google LLC | |
// | |
// Licensed under the Apache License, Version 2.0 (the "License"); | |
// you may not use this file except in compliance with the License. | |
// You may obtain a copy of the License at | |
// | |
// https://www.apache.org/licenses/LICENSE-2.0 | |
// | |
// Unless required by applicable law or agreed to in writing, software | |
// distributed under the License is distributed on an "AS IS" BASIS, | |
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | |
// See the License for the specific language governing permissions and | |
// limitations under the License. | |
package com.YOUR_PACKAGE_NAME; | |
import android.view.LayoutInflater; | |
import android.view.View; | |
import android.widget.Button; | |
import android.widget.ImageView; | |
import android.widget.RatingBar; | |
import android.widget.TextView; | |
import com.google.android.gms.ads.nativead.MediaView; | |
import com.google.android.gms.ads.nativead.NativeAd; | |
import com.google.android.gms.ads.nativead.NativeAdView; | |
import io.flutter.plugins.googlemobileads.GoogleMobileAdsPlugin.NativeAdFactory; | |
import java.util.Map; | |
class NativeAdFactoryExample implements NativeAdFactory { | |
private final LayoutInflater layoutInflater; | |
NativeAdFactoryExample(LayoutInflater layoutInflater) { | |
this.layoutInflater = layoutInflater; | |
} | |
@Override | |
public NativeAdView createNativeAd(NativeAd nativeAd, Map<String, Object> customOptions) { | |
---------------------------------------------------------------->>>>>>>>>>>>>>>change name R.layout.FILE_NAME | |
final NativeAdView adView = (NativeAdView) layoutInflater.inflate(R.layout.layout_small_native_ad_new, null); | |
// Set the media view. | |
adView.setMediaView((MediaView) 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 NativeAd. | |
((TextView) adView.getHeadlineView()).setText(nativeAd.getHeadline()); | |
adView.getMediaView().setMediaContent(nativeAd.getMediaContent()); | |
// These assets aren't guaranteed to be in every NativeAd, 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 { | |
adView.getAdvertiserView().setVisibility(View.VISIBLE); | |
((TextView) adView.getAdvertiserView()).setText(nativeAd.getAdvertiser()); | |
} | |
// This method tells the Google Mobile Ads SDK that you have finished populating your | |
// native ad view with this native ad. | |
adView.setNativeAd(nativeAd); | |
return adView; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment