Создайте класс по имени AdUtils , который инициализирует и загружает объявление
public class AdUtils {
private Context context;
private FrameLayout frameLayout;
private int nativeAdLayout;
private String nativeAdsId;
private UnifiedNativeAd nativeAd;
private NativeAdListener nativeAdListener;
public void setNativeAdListener(NativeAdListener nativeAdListener){
this.nativeAdListener= nativeAdListener;
}
public AdUtils(Context context) {
this.context = context;
}
public void loadNativeAd(FrameLayout frameLayout, int nativeAdLayout, String nativeAdsId) {
this.nativeAdLayout = nativeAdLayout;
this.frameLayout = frameLayout;
this.nativeAdsId = nativeAdsId;
this.nativeAd = loadNativeAd();
}
private UnifiedNativeAd loadNativeAd() {
if ((nativeAdLayout == -1 && frameLayout == null)) {
return null;
}
final UnifiedNativeAd[] nativeAd = {null};
AdLoader.Builder builder;
builder = new AdLoader.Builder(context, nativeAdsId);
builder.forUnifiedNativeAd(unifiedNativeAd -> {
if (nativeAd[0] != null) {
nativeAd[0].destroy();
}
nativeAd[0] = unifiedNativeAd;
UnifiedNativeAdView adView = (UnifiedNativeAdView) ((Activity) context).getLayoutInflater()
.inflate(nativeAdLayout, null);
populateNativeAdView(unifiedNativeAd, adView);
frameLayout.removeAllViews();
frameLayout.addView(adView);
if (nativeAdListener != null) {
nativeAdListener.onNativeAdLoaded();
}
});
NativeAdOptions adOptions = new NativeAdOptions.Builder().build();
builder.withNativeAdOptions(adOptions);
AdLoader adLoader = builder.withAdListener(new AdListener() {
@Override
public void onAdFailedToLoad(int errorCode) {
if (nativeAdListener != null) {
nativeAdListener.onNativeAdError();
}
}
@Override
public void onAdOpened() {
super.onAdOpened();
}
@Override
public void onAdClicked() {
super.onAdClicked();
}
}).build();
adLoader.loadAd(new AdRequest.Builder().build());
return nativeAd[0];
}
private void populateNativeAdView(UnifiedNativeAd nativeAd, UnifiedNativeAdView adView) {
MediaView mediaView = adView.findViewById(R.id.ad_media_main);
adView.setMediaView(mediaView);
adView.setHeadlineView(adView.findViewById(R.id.ad_headline));
adView.setCallToActionView(adView.findViewById(R.id.ad_call_to_action));
adView.setIconView(adView.findViewById(R.id.ad_app_icon));
((TextView) adView.getHeadlineView()).setText(nativeAd.getHeadline());
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.INVISIBLE);
} else {
((ImageView) adView.getIconView()).setImageDrawable(
nativeAd.getIcon().getDrawable());
adView.getIconView().setVisibility(View.VISIBLE);
}
adView.setNativeAd(nativeAd);
VideoController vc = nativeAd.getVideoController();
if (vc.hasVideoContent()) {
vc.setVideoLifecycleCallbacks(new VideoController.VideoLifecycleCallbacks() {
@Override
public void onVideoEnd() {
super.onVideoEnd();
}
});
}
}
public interface NativeAdListener {
void onNativeAdLoaded();
void onNativeAdError();
}
}
Внутри файла макета класса Acitivity
, два объявления Framelayouts
<FrameLayout
android:layout_width="match_parent"
android:layout_height="120dp"
android:id="@+id/ad_container_one"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.0" />
<FrameLayout
android:layout_width="match_parent"
android:layout_height="220dp"
android:id="@+id/ad_container_two"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/ad_container_one"
app:layout_constraintVertical_bias="0.47000003" />
Теперь внутри вашей активности или фрагмента создайте два объекта класса Adutils
AdUtils adUtilOne;
AdUtils adUtilTwo;
теперь вы должны инициализировать оба объекта, передав контекст конструктору и, framelayout, adlayout и adId методу загрузки. Создайте метод initAds
в действии, который вызывает инициализацию.
public void initAds(){
String adIdOne="Your_ad_id";
String adIdTwo="Your_ad_id";
adUtilOne= new AdUtils(this).loadNativeAd(frameLayoutAdOne,R.layout.ad_one, adIdOne);
adUtilTwo= new AdUtils(this).loadNativeAd(frameLayoutAdOne,R.layout.ad_two, adIdTwo);
adUtilOne.setNativeAdListener(new AdUtils.NativeAdListener() {
@Override
public void onNativeAdLoaded() {
}
@Override
public void onNativeAdError() {
frameLayoutAdOne.setVisibility(View.GONE);
}
});
adUtilTwo.setNativeAdListener(new AdUtils.NativeAdListener() {
@Override
public void onNativeAdLoaded() {
}
@Override
public void onNativeAdError() {
frameLayoutAdOne.setVisibility(View.GONE);
}
});
}
и вызовите метод после выполнения findviewsby id.