I have integrated Mopub native ads in listview all working good but i want to show native ads in between my content not in listview.
I tried this
MoPubNative.MoPubNativeNetworkListener moPubNativeListener = new MoPubNative.MoPubNativeNetworkListener() {
@Override
public void onNativeLoad(NativeAd nativeAd) {
// ...
}
@Override
public void onNativeFail(NativeErrorCode errorCode) {
// ...
}
};
MoPubNative moPubNative = new MoPubNative(SingleActivity.this, "ffb8734de73e4d62b93bae99c06db41f", moPubNativeListener);
ViewBinder viewBinder = new ViewBinder.Builder(R.layout.native_ad_layout)
.mainImageId(R.id.native_ad_main_image)
.iconImageId(R.id.native_ad_icon_image)
.titleId(R.id.native_ad_title)
.privacyInformationIconImageId(R.id.native_ad_daa_icon_image)
.textId(R.id.native_ad_text)
.build();
MoPubStaticNativeAdRenderer moPubStaticNativeAdRenderer = new MoPubStaticNativeAdRenderer(viewBinder);
moPubNative.registerAdRenderer(moPubStaticNativeAdRenderer);
Location exampleLocation = new Location("example_location");
exampleLocation.setLatitude(23.1);
exampleLocation.setLongitude(42.1);
exampleLocation.setAccuracy(100);
//Specify which native assets you want to use in your ad.
EnumSet<RequestParameters.NativeAdAsset> assetsSet = EnumSet.of(RequestParameters.NativeAdAsset.TITLE,
RequestParameters.NativeAdAsset.TEXT,
RequestParameters.NativeAdAsset.CALL_TO_ACTION_TEXT,
RequestParameters.NativeAdAsset.ICON_IMAGE);
RequestParameters requestParameters = new RequestParameters.Builder()
.keywords("gender:m,age:27")
.location(exampleLocation)
.desiredAssets(assetsSet)
.build();
moPubNative.makeRequest(requestParameters);
but as you can see i am loading layout
ViewBinder viewBinder = new ViewBinder.Builder(R.layout.native_ad_layout)
but how to render this layout in between my content in detail activity.
Thanks.
Mopub native manual advertisement integration example
First of all define your view native_advetisement.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@color/white">
<ImageView
android:id="@+id/native_ad_main_image"
android:layout_width="match_parent"
android:layout_height="200dp"
/>
<ImageView
android:id="@+id/native_ad_icon_image"
android:layout_width="20dp"
android:layout_height="20dp"
/>
<ImageView
android:id="@+id/native_ad_daa_icon_image"
android:layout_width="20dp"
android:layout_height="20dp"
android:layout_below="@+id/native_ad_main_image"/>
<TextView
android:id="@+id/native_ad_title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="@color/red"
android:layout_marginLeft="20dp"
android:layout_toRightOf="@+id/native_ad_daa_icon_image"
android:textSize="12dp"
android:layout_below="@+id/native_ad_main_image"/>
<TextView
android:id="@+id/native_ad_text"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:textColor="@color/black"
android:layout_toRightOf="@+id/native_ad_daa_icon_image"
android:textSize="11dp"
android:layout_marginLeft="20dp"
android:layout_below="@+id/native_ad_title"
android:layout_marginBottom="10dp"/>
</RelativeLayout>
</LinearLayout>
And then prepare your component.
public class AdverNative extends LinearLayout {
private static String LOG_TAG = AdverNative.class.getName();
private Context mContext;
Activity activity;
public AdverNative(Context context, AttributeSet attrs) {
super(context, attrs);
this.mContext = context;
this.activity = (Activity) context;
initView();
}
private void initView() {
final ViewBinder viewBinder = new ViewBinder.Builder(R.layout.native_advetisement)
.titleId(R.id.native_ad_title)
.textId(R.id.native_ad_text)
.mainImageId(R.id.native_ad_main_image)
.iconImageId(R.id.native_ad_icon_image)
.privacyInformationIconImageId(R.id.native_ad_daa_icon_image)
.build();
MoPubNative.MoPubNativeNetworkListener moPubNativeNetworkListener = new MoPubNative.MoPubNativeNetworkListener() {
@Override
public void onNativeLoad(NativeAd nativeAd) {
AdapterHelper ah = new AdapterHelper(mContext, 0, 3);
View v = ah.getAdView(null, AdverNative.this, nativeAd, viewBinder);
LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT);
addView(v, params);
}
@Override
public void onNativeFail(NativeErrorCode nativeErrorCode) {
}
};
MoPubNative moPubNative = new MoPubNative(activity, Constants.CODE_NATIVE, moPubNativeNetworkListener);
moPubNative.registerAdRenderer(new MoPubStaticNativeAdRenderer(viewBinder));
EnumSet<RequestParameters.NativeAdAsset> assetsSet = EnumSet.of(
RequestParameters.NativeAdAsset.TITLE,
RequestParameters.NativeAdAsset.TEXT,
RequestParameters.NativeAdAsset.CALL_TO_ACTION_TEXT,
RequestParameters.NativeAdAsset.MAIN_IMAGE,
RequestParameters.NativeAdAsset.ICON_IMAGE,
RequestParameters.NativeAdAsset.STAR_RATING);
Location exampleLocation = new Location("example_location");
exampleLocation.setLatitude(23.1);
exampleLocation.setLongitude(42.1);
exampleLocation.setAccuracy(100);
final String keywords = "";
RequestParameters mRequestParameters = new RequestParameters.Builder()
.location(exampleLocation)
.keywords(keywords)
.desiredAssets(assetsSet)
.build();
moPubNative.makeRequest(mRequestParameters);
}
}
And use like this
<AdverNative
android:layout_width="match_parent"
android:layout_height="wrap_content">
</AdverNative>
This is the way to do it, at least in latest and greatest MoPub SDK.
public void onNativeLoad(NativeAd nativeAd) {
RelativeLayout adParent = findViewById(R.id.ad_holder);
View adView = nativeAd.createAdView(getActivity(), adParent);
nativeAd.prepare(adView);
nativeAd.renderAdView(adView);
adParent.addView(adView);
}