The adTypes
parameter in the code is responsible for the ad formats you are going to implement into your app.
Appodeal supports all major mobile ad formats:
- Interstitial (
Appodeal.INTERSTITIAL
): full-screen ads, that contain a static image or a skippable video. - Rewarded video (
Appodeal.REWARDED_VIDEO
): non-skippable video ads that users may choose to watch to get a reward. - Non-skippable video (
Appodeal.NON_SKIPPABLE_VIDEO
): video ads that must be watched until the end. - Native ads (
Appodeal.NATIVE
): ads that can match the structure and the style of an app and naturally continue the user experience. - Banner (
Appodeal.BANNER
): rectangular ads that occupy a spot within an app's layout - MREC (
Appodeal.MREC
): medium-rectangule ads (300x250px) that appear within in-app content. It’s a deprecated ad format and it is recommended to use native ads instead.
Interstitial
1. Display interstitial
To display interstitial, you need to call the following code in the activity:
Appodeal.show(this, Appodeal.INTERSTITIAL);
2. Check if interstitial is loaded
Appodeal.isLoaded(Appodeal.INTERSTITIAL);
3. Use interstitial callbacks
Appodeal.setInterstitialCallbacks(new InterstitialCallbacks() {
@Override
public void onInterstitialLoaded(boolean isPrecache) {
// Called when interstitial is loaded
}
@Override
public void onInterstitialFailedToLoad() {
// Called when interstitial failed to load
}
@Override
public void onInterstitialShown() {
// Called when interstitial is shown
}
@Override
public void onInterstitialShowFailed() {
// Called when interstitial show failed
}
@Override
public void onInterstitialClicked() {
// Called when interstitial is clicked
}
@Override
public void onInterstitialClosed() {
// Called when interstitial is closed
}
@Override
public void onInterstitialExpired() {
// Called when interstitial is expired
}
});
4. Cache ad manually
To disable automatic caching for interstitials, use the code below before the SDK initialization:
Appodeal.setAutoCache(Appodeal.INTERSTITIAL, false);
To cache interstitial use:
Appodeal.cache(this, Appodeal.INTERSTITIAL);
Read more on manual caching in our FAQ. Rewarded video
1. Display rewarded video
To display rewarded video, call the following code in the activity:
Appodeal.show(this, Appodeal.REWARDED_VIDEO);
2. Check if rewarded video is loaded
Appodeal.isLoaded(Appodeal.REWARDED_VIDEO);
3. Use rewarded video callbacks
Appodeal.setRewardedVideoCallbacks(new RewardedVideoCallbacks() {
@Override
public void onRewardedVideoLoaded(boolean isPrecache) {
// Called when rewarded video is loaded
}
@Override
public void onRewardedVideoFailedToLoad() {
// Called when rewarded video failed to load
}
@Override
public void onRewardedVideoShown() {
// Called when rewarded video is shown
}
@Override
public void onRewardedVideoShowFailed() {
// Called when rewarded video show failed
}
@Override
public void onRewardedVideoClicked() {
// Called when rewarded video is clicked
}
@Override
public void onRewardedVideoFinished(double amount, String name) {
// Called when rewarded video is viewed until the end
}
@Override
public void onRewardedVideoClosed(boolean finished) {
// Called when rewarded video is closed
}
@Override
public void onRewardedVideoExpired() {
// Called when rewarded video is expired
}
});
4. Cache rewarded video manually
To disable automatic caching for rewarded videos, use the code below before the SDK initialization:
Appodeal.setAutoCache(Appodeal.REWARDED_VIDEO, false);
To cache rewarded video, use:
Appodeal.cache(this, Appodeal.REWARDED_VIDEO);
Read more on manual caching in our FAQ.5. Advanced
Server-to-server (S2S) rewarded video callbacks
To secure you apps economy, we offer S2S reward callbaks. To validate each reward, you need to setup a callback URL on your server that will receive the reward information. Appodeal will pass the user data to your callback URL that you will need to validate and adjust the user balance accordingly.
1. Create the reward callback URL on your server that will receive the reward information.
2. Fill the created URL and the encryption key in the app settings in your dashboard.
3. The reward callback will be sent to your URL using GET request with two parameters:
{http://example.com/reward}?data1={data1}&data2={data2}
4. Your URL should decrypt the data and validate it.
5. Check impression_id
for uniqueness and store it in your system to prevent the duplicate of transactions.
To set user ID, use the Appodeal.getUserSettings(this).setUserId("User#123")
method before the SDK initialization.
We offer sample scripts in Go, PHP, Ruby, Java, Node.js, Python 3 and C# to decrypt the data. If you need samples in other languages, please contact our support team and we will provide it to you.
Sample in PHP: reward.php.
Sample in Ruby: reward.rb.
Sample in Java: reward.java.
Sample in Node.js: reward.js.
Sample in Python 3: reward.py.
Sample in C#: reward.cs.
Sample in Go: reward.go.
Get reward data for placement
To get placement reward data before video is shown, use:
Appodeal.getRewardParameters(placementName);
This method returns Pair with the reward amount and the currency. Non-skippable video
1. Display non-skippable video
To display non-skippable videos, you need to call the following code in the activity:
Appodeal.show(this, Appodeal.NON_SKIPPABLE_VIDEO);
2. Check if non-skippable video is loaded
Appodeal.isLoaded(Appodeal.NON_SKIPPABLE_VIDEO);
3. Use non-skippable video callbacks
Appodeal.setNonSkippableVideoCallbacks(new NonSkippableVideoCallbacks() {
@Override
public void onNonSkippableVideoLoaded(boolean isPrecache) {
// Called when non-skippable video is loaded
}
@Override
public void onNonSkippableVideoFailedToLoad() {
// Called when non-skippable video failed to load
}
@Override
public void onNonSkippableVideoShown() {
// Called when non-skippable video is shown
}
@Override
public void onNonSkippableVideoShowFailed() {
// Called when non-skippable video show failed
}
@Override
public void onNonSkippableVideoFinished() {
// Called when non-skippable video is viewed until the end
}
@Override
public void onNonSkippableVideoClosed(boolean finished) {
// Called when non-skippable video is closed
}
@Override
public void onNonSkippableVideoExpired() {
// Called when non-skippable video is expired
}
});
4. Cache non-skippable video manually
To disable automatic caching for non-skippable videos, use the code below before the SDK initialization:
Appodeal.setAutoCache(Appodeal.NON_SKIPPABLE_VIDEO, false);
To cache non-skippable videos, use:
Appodeal.cache(this, Appodeal.NON_SKIPPABLE_VIDEO);
Read more on manual caching in our FAQ. Native ads
1. Cache native ads
To cache native ads, use:
Appodeal.cache(this, Appodeal.NATIVE);
To cache multiple native ads, use:
Appodeal.cache(this, Appodeal.NATIVE, 3);
The number of cached ads is not guaranteed and could be less than requested.2. Check if native ad was loaded
Appodeal.isLoaded(Appodeal.NATIVE);
3. Get loaded native ads
To get loaded native ads, use the following method:
Appodeal.getNativeAds(int amount);
This method returns
list
of NativeAd objects as per requested amount of loaded native ads. Once you get the ads, they are removed from our SDK cache.
4. Use native ads callbacks
Appodeal.setNativeCallbacks(new NativeCallbacks() {
@Override
public void onNativeLoaded() {
// Called when native ads are loaded
}
@Override
public void onNativeFailedToLoad() {
// Called when native ads are failed to load
}
@Override
public void onNativeShown(NativeAd nativeAd) {
// Called when native ad is shown
}
@Override
public void onNativeShowFailed(NativeAd nativeAd) {
// Called when native ad show failed
}
@Override
public void onNativeClicked(NativeAd nativeAd) {
// Called when native ads is clicked
}
@Override
public void onNativeExpired() {
// Called when native ads is expired
}
});
5. Cache native ads manually
To disable automatic caching for native ads, use the code below before the SDK initialization:
Appodeal.setAutoCache(Appodeal.NATIVE, false);
To cache native ads, use:
Appodeal.cache(this, Appodeal.NATIVE);
Read more on manual caching in our FAQ. 6. Get available native ads count
Appodeal.getAvailableNativeAdsCount();
7. Common mistakes with native ads
- No ad attribution or AdChoices icon
The majority of ad networks require from publishers to add a special mark to a native ad, so users don’t mistake them for content. That’s why you always need to make sure, that native ads in your app have the ad attribution (e.g., “Ad”) or the AdChoices icon.
- Absence of the required native ad elements
Every native ad should contain:
- title;
- call-to-action button;
- ad attribution or AdChoices icon;
icon, image or video.
- Native ad elements alteration
Advertisers expect that their ads will be displayed clearly and without any alteration. You can scale buttons and images, but you shouldn't crop, cover or distort them.
- Overlaying elements of native ads on each other
Make sure, that all elements of a native ad are visible and not overlaid.
8. NativeAd object
After getting loaded native ads using Appodeal.getNativeAds()
, a list of NativeAd objects is returned.
Name of methods | Type | Mandatory | Description |
---|
nativeAd.getTitle() | String | Mandatory | Title of the native ad. Maximum 25 symbols of the title should always be displayed. You can add ellipsis at the end if the title is longer. |
nativeAd.getCallToAction() | String | Mandatory | Call-to-action text. Should be displayed without truncation on a visible button. |
nativeAd.getDescription() | String | Optional | Text description of the native ad. If you choose to display the description, you should display maximum 100 characters. You can add ellipsis at the end. |
nativeAd.getRating() | Float | Optional | Rating of the app in [0-5] range |
nativeAd.getAgeRestrictions() | String | Optional | App age restriction. May return null . |
nativeAd.getAdProvider() | String | Optional | Gets provider name of native ad. |
nativeAd.getProviderView(context) | View | Mandatory | If it doesn’t return null , it’s mandatory to display the provider icon in any corner of the native ad. Used by some networks to display AdChoices or the privacy icon. |
nativeAd.containsVideo() | Boolean | Optional | Returns true, if NativeAd object contains video. |
nativeAd.canShow(String placementName) | Boolean | Optional | Check if native ad can be shown with the placement. |
nativeAd.isPrecache() | Boolean | Optional | Check if native ad is precache. |
nativeAd.getPredictedEcpm() | Double | Optional | Returns the predicted ecpm for creative. |
nativeAd.destroy() | Void | Mandatory | Destroys the native ad, unregisters from tracking. |
9. Configuration
Native ads assets caching
Set required native media assets for what necessary for show. Default value is ALL
.
Appodeal.setRequiredNativeMediaAssetType(Native.MediaAssetType.ALL)
- assets caching for NativeIconView
and NativeMediaView
;Appodeal.setRequiredNativeMediaAssetType(Native.MediaAssetType.ICON)
- assets caching only for NativeIconView
;Appodeal.setRequiredNativeMediaAssetType(Native.MediaAssetType.IMAGE)
- assets caching only for NativeMediaView
.
Native video
To show video native ads, add NativeMediaView
to native ad layout.
NativeMediaView nativeMediaView = (NativeMediaView) adView.findViewById(R.id.appodeal_media_view_content);
if (nativeAd.containsVideo()) {
nativeAd.setNativeMediaView(nativeMediaView);
} else {
nativeMediaView.setVisibility(View.GONE);
}
Native video settings
To control whether you want to show video native ads, use the following methods:
Appodeal.setNativeAdType(Native.NativeAdType.Auto)
- both static image and video native ads will be loaded;Appodeal.setNativeAdType(Native.NativeAdType.NoVideo)
- only static image native ads will be loaded;Appodeal.setNativeAdType(Native.NativeAdType.Video)
- only video native ads will be loaded.
Templates
Appodeal SDK provides 3 types of templates for native ads:
NativeAdViewNewsFeed
;NativeAdViewAppWall
;NativeAdViewContentStream
.
If you want to use one of these templates, you can add the selected template in the layout:
<com.appodeal.ads.native_ad.views.NativeAdViewNewsFeed
android:id="@+id/native_ad_view_news_feed"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="10dp">
</com.appodeal.ads.native_ad.views.NativeAdViewNewsFeed>
<com.appodeal.ads.native_ad.views.NativeAdViewAppWall
android:id="@+id/native_ad_view_app_wall"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="10dp">
</com.appodeal.ads.native_ad.views.NativeAdViewAppWall>
<com.appodeal.ads.native_ad.views.NativeAdViewContentStream
android:id="@+id/native_ad_view_content_stream"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="10dp">
</com.appodeal.ads.native_ad.views.NativeAdViewContentStream>
And when you need to show the ad:
NativeAdViewNewsFeed nav_nf = (NativeAdViewNewsFeed) mActivity.findViewById(R.id.native_ad_view_news_feed);
nav_nf.setNativeAd(mNativeAd);
NativeAdViewAppWall nav_aw = (NativeAdViewAppWall) mActivity.findViewById(R.id.native_ad_view_app_wall);
nav_aw.setNativeAd(mNativeAd);
NativeAdViewContentStream nav_cs = (NativeAdViewContentStream) mActivity.findViewById(R.id.native_ad_view_content_stream);
nav_cs.setNativeAd(mNativeAd);
You can also create a view programmatically and add it to the screen:
RelativeLayout holder = (RelativeLayout) mActivity.findViewById(R.id.native_template_holder);
NativeAdViewAppWall nativeAdView = new NativeAdViewAppWall(mActivity, mNativeAd);
holder.addView(nativeAdView);
Template elements
TextView getTitleView()
TextView getDescriptionView()
View getRatingView()
RatingBar getRatingBar()
View getProviderView()
TextView getCallToActionView()
NativeIconView getNativeIconView()
NativeMediaView getNativeMediaView()
By default, native ads are labeled «Ad». You can replace it with «Sponsored»:To change the color of the call-to-action button in the templates, use:
nav.setCallToActionColor(int color)
nav.setCallToActionColor(String color)
Сustom layout
NativeAdView nativeAdView = (NativeAdView) findViewById(R.id.native_layout)
TextView tvTitle = (TextView) nativeAdView.findViewById(R.id.tv_title);
tvTitle.setText(nativeAd.getTitle());
nativeAdView.setTitleView(tvTitle);
TextView tvDescription = (TextView) nativeAdView.findViewById(R.id.tv_description);
tvDescription.setText(nativeAd.getDescription());
nativeAdView.setDescriptionView(tvDescription);
RatingBar ratingBar = (RatingBar) nativeAdView.findViewById(R.id.rb_rating);
if (nativeAd.getRating() == 0) {
ratingBar.setVisibility(View.INVISIBLE);
} else {
ratingBar.setVisibility(View.VISIBLE);
ratingBar.setRating(nativeAd.getRating());
ratingBar.setStepSize(0.1f);
}
nativeAdView.setRatingView(ratingBar);
Button ctaButton = (Button) nativeAdView.findViewById(R.id.b_cta);
ctaButton.setText(nativeAd.getCallToAction());
nativeAdView.setCallToActionView(ctaButton);
View providerView = nativeAd.getProviderView(mNativeListView.getContext());
if (providerView != null) {
if (providerView.getParent() != null && providerView.getParent() instanceof ViewGroup) {
((ViewGroup) providerView.getParent()).removeView(providerView);
}
FrameLayout providerViewContainer = (FrameLayout) nativeAdView.findViewById(R.id.provider_view);
ViewGroup.LayoutParams layoutParams = new ViewGroup.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
providerViewContainer.addView(providerView, layoutParams);
}
nativeAdView.setProviderView(providerView);
TextView tvAgeRestrictions = (TextView) nativeAdView.findViewById(R.id.tv_age_restriction);
if (nativeAd.getAgeRestrictions() != null) {
tvAgeRestrictions.setText(nativeAd.getAgeRestrictions());
tvAgeRestrictions.setVisibility(View.VISIBLE);
} else {
tvAgeRestrictions.setVisibility(View.GONE);
}
NativeIconView nativeIconView = nativeAdView.findViewById(R.id.icon);
nativeAdView.setNativeIconView(nativeIconView);
NativeMediaView nativeMediaView = (NativeMediaView) nativeAdView.findViewById(R.id.appodeal_media_view_content);
nativeAdView.setNativeMediaView(nativeMediaView);
nativeAdView.registerView(nativeAd);
To show NativeAd in custom layout you should use
NativeAdView
, populate and register the asset views.
Bind assets view to NativeAdView
:
nativeAdView.setTitleView(View view)
nativeAdView.setCallToActionView(View view)
nativeAdView.setRatingView(View view)
nativeAdView.setDescriptionView(View view)
nativeAdView.setProviderView(View view)
nativeAdView.setNativeIconView(NativeIconView nativeIconView)
nativeAdView.setNativeMediaView(NativeMediaView nativeMediaView)
And register NativeAd
object in NativeAdView:
nativeAdView.registerView(NativeAd nativeAd) // or nativeAdView.registerView(NativeAd nativeAd, placementName) if you use placements
If another NativeAd
was registered to this instance of view, it wil be automatically unregistered.To unregister NativeAd from NativeAdView,
call:
nativeAdView.unregisterViewForInteraction()
If
NativeAdView
is reused to display the same ad or to display other ads, it's better call the
unregisterViewForInteraction()
method before you can register the same view with another instance of
NativeAd
.
To destroy NativeAd that was registered to NativeAdView
, you should call:
You can't use this instance on NativeAdView
after call destroy()
. You should hide this view or register another NativeAd
.10. Native ads integration example
public void loadNativeAds() {
Appodeal.setRequiredNativeMediaAssetType(Native.MediaAssetType.ICON);
Appodeal.initialize(this, YOUR_APP_KEY, Appodeal.NATIVE, consentValue);
Appodeal.setNativeCallbacks(new NativeCallbacks() {
@Override
public void onNativeLoaded() {
Toast.makeText(MainActivity.this, "onNativeLoaded", Toast.LENGTH_SHORT).show();
}
@Override
public void onNativeFailedToLoad() {
Toast.makeText(MainActivity.this, "onNativeFailedToLoad", Toast.LENGTH_SHORT).show();
}
@Override
public void onNativeShown(NativeAd nativeAd) {
Toast.makeText(MainActivity.this, "onNativeShown", Toast.LENGTH_SHORT).show();
}
@Override
public void onNativeClicked(NativeAd nativeAd) {
Toast.makeText(MainActivity.this, "onNativeClicked", Toast.LENGTH_SHORT).show();
}
@Override
public void onNativeExpired() {
Toast.makeText(MainActivity.this, "onNativeExpired", Toast.LENGTH_SHORT).show();
}
});
};
To show loaded native ad:
public void showNativeAd(){
List<NativeAd> loadedNativeAds = Appodeal.getNativeAds(1);
if (loadedNativeAds.isEmpty()){
//Native Ads not loaded yet
return;
}
NativeAd nativeAd = loadedNativeAds.get(0);
NativeAdView nativeAdView = (NativeAdView) findViewById(R.id.native_layout);
TextView tvTitle = (TextView) nativeAdView.findViewById(R.id.tv_title);
tvTitle.setText(nativeAd.getTitle());
nativeAdView.setTitleView(tvTitle);
TextView tvDescription = (TextView) nativeAdView.findViewById(R.id.tv_description);
tvDescription.setText(nativeAd.getDescription());
nativeAdView.setDescriptionView(tvDescription);
RatingBar ratingBar = (RatingBar) nativeAdView.findViewById(R.id.rb_rating);
if (nativeAd.getRating() == 0) {
ratingBar.setVisibility(View.INVISIBLE);
} else {
ratingBar.setVisibility(View.VISIBLE);
ratingBar.setRating(nativeAd.getRating());
ratingBar.setStepSize(0.1f);
}
nativeAdView.setRatingView(ratingBar);
Button ctaButton = (Button) nativeAdView.findViewById(R.id.b_cta);
ctaButton.setText(nativeAd.getCallToAction());
nativeAdView.setCallToActionView(ctaButton);
View providerView = nativeAd.getProviderView(this);
if (providerView != null) {
if (providerView.getParent() != null && providerView.getParent() instanceof ViewGroup) {
((ViewGroup) providerView.getParent()).removeView(providerView);
}
FrameLayout providerViewContainer = (FrameLayout) nativeAdView.findViewById(R.id.provider_view);
ViewGroup.LayoutParams layoutParams = new ViewGroup.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
providerViewContainer.addView(providerView, layoutParams);
}
nativeAdView.setProviderView(providerView);
TextView tvAgeRestrictions = (TextView) nativeAdView.findViewById(R.id.tv_age_restriction);
if (nativeAd.getAgeRestrictions() != null) {
tvAgeRestrictions.setText(nativeAd.getAgeRestrictions());
tvAgeRestrictions.setVisibility(View.VISIBLE);
} else {
tvAgeRestrictions.setVisibility(View.GONE);
}
NativeIconView nativeIconView = nativeAdView.findViewById(R.id.icon);
nativeAdView.setNativeIconView(nativeIconView);
NativeMediaView nativeMediaView = (NativeMediaView) nativeAdView.findViewById(R.id.appodeal_media_view_content);
nativeAdView.setNativeMediaView(nativeMediaView);
nativeAdView.registerView(nativeAd);
nativeAdView.setVisibility(View.VISIBLE);
}
11. Native Ad integration to the feed
You can use this example to integrate Naitve Ad to existing feed in your application:
import android.support.annotation.Nullable;
import android.support.v7.widget.RecyclerView;
import android.util.SparseArray;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.FrameLayout;
import android.widget.RatingBar;
import android.widget.TextView;
import com.appodeal.ads.Appodeal;
import com.appodeal.ads.NativeAd;
import com.appodeal.ads.NativeAdView;
import com.appodeal.ads.NativeCallbacks;
import com.appodeal.ads.NativeIconView;
import com.appodeal.ads.NativeMediaView;
import java.util.List;
/**
* Wrapper adapter to show Native Ad in recycler view with fixed step
*/
public class AppodealWrapperAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> implements NativeCallbacks {
private static final int DEFAULT_NATIVE_STEP = 5;
private static final int VIEW_HOLDER_NATIVE_AD_TYPE = 600;
private RecyclerView.Adapter<RecyclerView.ViewHolder> userAdapter;
private int nativeStep = DEFAULT_NATIVE_STEP;
private SparseArray<NativeAd> nativeAdList = new SparseArray<>();
/**
* @param userAdapter user adapter
* @param nativeStep step show {@link com.appodeal.ads.NativeAd}
*/
public AppodealWrapperAdapter(RecyclerView.Adapter<RecyclerView.ViewHolder> userAdapter, int nativeStep) {
this.userAdapter = userAdapter;
this.nativeStep = nativeStep + 1;
userAdapter.registerAdapterDataObserver(new RecyclerView.AdapterDataObserver() {
@Override
public void onChanged() {
super.onChanged();
AppodealWrapperAdapter.this.notifyDataSetChanged();
fillListWithAd();
}
@Override
public void onItemRangeInserted(int positionStart, int itemCount) {
super.onItemRangeInserted(positionStart, itemCount);
AppodealWrapperAdapter.this.notifyDataSetChanged();
fillListWithAd();
}
});
Appodeal.setNativeCallbacks(this);
fillListWithAd();
}
@Override
public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
if (viewType == VIEW_HOLDER_NATIVE_AD_TYPE) {
View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.include_native_ads, parent, false);
return new NativeCustomAdViewHolder(view);
} else {
return userAdapter.onCreateViewHolder(parent, viewType);
}
}
@Override
public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
if (holder instanceof NativeCustomAdViewHolder) {
((NativeCustomAdViewHolder) holder).fillNative(nativeAdList.get(position));
} else {
userAdapter.onBindViewHolder(holder, getPositionInUserAdapter(position));
}
}
@Override
public int getItemCount() {
int resultCount = 0;
resultCount += getNativeAdsCount();
resultCount += getUserAdapterItemCount();
return resultCount;
}
@Override
public int getItemViewType(int position) {
if (isNativeAdPosition(position)) {
return VIEW_HOLDER_NATIVE_AD_TYPE;
} else {
return userAdapter.getItemViewType(getPositionInUserAdapter(position));
}
}
@Override
public void onViewRecycled(RecyclerView.ViewHolder holder) {
super.onViewRecycled(holder);
if (holder instanceof NativeCustomAdViewHolder) {
((NativeCustomAdViewHolder) holder).unregisterViewForInteraction();
}
}
/**
* Destroy all used native ads
*/
public void destroyNativeAds() {
if (nativeAdList != null) {
for (int i = 0; i < nativeAdList.size(); i++) {
NativeAd nativeAd = nativeAdList.valueAt(i);
nativeAd.destroy();
}
nativeAdList.clear();
}
}
@Override
public void onNativeLoaded() {
fillListWithAd();
}
@Override
public void onNativeFailedToLoad() {
}
@Override
public void onNativeShown(NativeAd nativeAd) {
}
@Override
public void onNativeClicked(NativeAd nativeAd) {
}
@Override
public void onNativeExpired() {
}
/**
* @return count of loaded ads {@link com.appodeal.ads.NativeAd}
*/
private int getNativeAdsCount() {
if (nativeAdList != null) {
return nativeAdList.size();
}
return 0;
}
/**
* @return user items count
*/
private int getUserAdapterItemCount() {
if (userAdapter != null) {
return userAdapter.getItemCount();
}
return 0;
}
/**
* @param position index in wrapper adapter
* @return {@code true} if item by position is {@link com.appodeal.ads.NativeAd}
*/
private boolean isNativeAdPosition(int position) {
return nativeAdList.get(position) != null;
}
/**
* Method for searching position in user adapter
* @param position index in wrapper adapter
* @return index in user adapter
*/
private int getPositionInUserAdapter(int position) {
return position - Math.min(nativeAdList.size(), position / nativeStep);
}
/**
* Method for filling list with {@link com.appodeal.ads.NativeAd}
*/
private void fillListWithAd() {
int insertPosition = findNextAdPosition();
NativeAd nativeAd;
while (canUseThisPosition(insertPosition) && (nativeAd = getNativeAdItem()) != null) {
nativeAdList.put(insertPosition, nativeAd);
notifyItemInserted(insertPosition);
insertPosition = findNextAdPosition();
}
}
/**
* Get native ad item
* @return {@link com.appodeal.ads.NativeAd}
*/
@Nullable
private NativeAd getNativeAdItem() {
List<NativeAd> ads = Appodeal.getNativeAds(1);
return !ads.isEmpty() ? ads.get(0) : null;
}
/**
* Method for finding next position suitable for {@link com.appodeal.ads.NativeAd}
* @return position for next native ad view
*/
private int findNextAdPosition() {
if (nativeAdList.size() > 0) {
return nativeAdList.keyAt(nativeAdList.size() - 1) + nativeStep;
}
return nativeStep - 1;
}
/**
* @param position index in wrapper adapter
* @return {@code true} if you can add {@link com.appodeal.ads.NativeAd} to this position
*/
private boolean canUseThisPosition(int position) {
return nativeAdList.get(position) == null && getItemCount() > position;
}
/**
* View holder for create custom {@link com.appodeal.ads.native_ad.views.NativeAdView}
*/
static class NativeCustomAdViewHolder extends RecyclerView.ViewHolder {
private NativeAdView nativeAdView;
private TextView tvTitle;
private TextView tvDescription;
private RatingBar ratingBar;
private Button ctaButton;
private NativeIconView nativeIconView;
private TextView tvAgeRestrictions;
private NativeMediaView nativeMediaView;
private FrameLayout providerViewContainer;
NativeCustomAdViewHolder(View itemView) {
super(itemView);
nativeAdView = itemView.findViewById(R.id.native_item);
tvTitle = itemView.findViewById(R.id.tv_title);
tvDescription = itemView.findViewById(R.id.tv_description);
ratingBar = itemView.findViewById(R.id.rb_rating);
ctaButton = itemView.findViewById(R.id.b_cta);
nativeIconView = itemView.findViewById(R.id.icon);
providerViewContainer = itemView.findViewById(R.id.provider_view);
tvAgeRestrictions = itemView.findViewById(R.id.tv_age_restriction);
nativeMediaView = itemView.findViewById(R.id.appodeal_media_view_content);
}
void fillNative(NativeAd nativeAd) {
tvTitle.setText(nativeAd.getTitle());
tvDescription.setText(nativeAd.getDescription());
if (nativeAd.getRating() == 0) {
ratingBar.setVisibility(View.INVISIBLE);
} else {
ratingBar.setVisibility(View.VISIBLE);
ratingBar.setRating(nativeAd.getRating());
ratingBar.setStepSize(0.1f);
}
ctaButton.setText(nativeAd.getCallToAction());
View providerView = nativeAd.getProviderView(nativeAdView.getContext());
if (providerView != null) {
if (providerView.getParent() != null && providerView.getParent() instanceof ViewGroup) {
((ViewGroup) providerView.getParent()).removeView(providerView);
}
providerViewContainer.removeAllViews();
ViewGroup.LayoutParams layoutParams = new ViewGroup.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
providerViewContainer.addView(providerView, layoutParams);
}
if (nativeAd.getAgeRestrictions() != null) {
tvAgeRestrictions.setText(nativeAd.getAgeRestrictions());
tvAgeRestrictions.setVisibility(View.VISIBLE);
} else {
tvAgeRestrictions.setVisibility(View.GONE);
}
if (nativeAd.containsVideo()) {
nativeAdView.setNativeMediaView(nativeMediaView);
} else {
nativeMediaView.setVisibility(View.GONE);
}
nativeAdView.setTitleView(tvTitle);
nativeAdView.setDescriptionView(tvDescription);
nativeAdView.setRatingView(ratingBar);
nativeAdView.setCallToActionView(ctaButton);
nativeAdView.setNativeIconView(nativeIconView);
nativeAdView.setProviderView(providerView);
nativeAdView.registerView(nativeAd);
nativeAdView.setVisibility(View.VISIBLE);
}
void unregisterViewForInteraction() {
nativeAdView.unregisterViewForInteraction();
}
}
}
To use this wrapper adapter, you should create a new instance of AppodealWrapperAdapter
:
AppodealWrapperAdapter appodealWrapperAdapter = new AppodealWrapperAdapter(myAdapter, 2);
And set this wrapper adapter to your recycler view. Banner
1. Display banner at the specific position of the screen
Appodeal.show(this, Appodeal.BANNER_BOTTOM); // Display banner at the bottom of the screen
Appodeal.show(this, Appodeal.BANNER_TOP); // Display banner at the top of the screen
Appodeal.show(this, Appodeal.BANNER_LEFT); // Display banner at the left of the screen
Appodeal.show(this, Appodeal.BANNER_RIGHT); // Display banner at the right of the screen
2. Display banner in the specified view in layout file
Add com.appodeal.ads.BannerView
to your layout file:
<com.appodeal.ads.BannerView
android:id="@+id/appodealBannerView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:visibility="gone" />
Set view id before the SDK initialization to show banner in:
Appodeal.setBannerViewId(R.id.appodealBannerView);
Now you can show banner in the view specified (Make sure that the required view is on the screen):
Appodeal.show(this, Appodeal.BANNER_VIEW);
3. Display banner in programmatically created view
Create banner view:
Appodeal.getBannerView(this);
Add view to your layout and then show banner in it:
Appodeal.show(this, Appodeal.BANNER_VIEW);
4. Check if banner is loaded
Appodeal.isLoaded(Appodeal.BANNER);
5. Use banner callbacks
Appodeal.setBannerCallbacks(new BannerCallbacks() {
@Override
public void onBannerLoaded(int height, boolean isPrecache) {
// Called when banner is loaded
}
@Override
public void onBannerFailedToLoad() {
// Called when banner failed to load
}
@Override
public void onBannerShown() {
// Called when banner is shown
}
@Override
public void onBannerShowFailed() {
// Called when banner show failed
}
@Override
public void onBannerClicked() {
// Called when banner is clicked
}
@Override
public void onBannerExpired() {
// Called when banner is expired
}
});
6. Activity "Paused" state handling
We’ll automatically handle pause and resume state for already displayed Banners on Activity on which they were displayed, however, we don’t restore displayed Banners after Activity recreation (e.g. - orientation changes) and we don’t show Banners in newly created Activities.
For display ads on newly created Activity just call Appodea.show()
method if required.
@Override
public void onResume() {
super.onResume();
Appodeal.show(this, Appodeal.BANNER_TOP);
}
This behavior can be changed by call Appodeal.setSharedAdsInstanceAcrossActivities(true)
(See more: Enable Shared View Ads Instance Across Activities Logic)7. Hide banner
Appodeal.hide(this, Appodeal.BANNER);
8. Destroy shown banner
If you want to hide the banner from all activities and clear the memory, call the code below:
Appodeal.destroy(Appodeal.BANNER);
9. Cache banner manually
To disable automatic caching for banners, use the code below before the SDK initialization:
Appodeal.setAutoCache(Appodeal.BANNER, false);
To cache banners, use:
Appodeal.cache(this, Appodeal.BANNER);
Read more on manual caching in our FAQ.10. Configuration
Enable 728*90 banners
To enable 728*90 banners, use the following method:
Appodeal.set728x90Banners(true);
Disable banner refresh animation
To disable banner refresh animation, use:
Appodeal.setBannerAnimation(false);
Disable smart banners
Smart banners are the banner ads which automatically fit the screen size. Using them helps to deal with the increasing fragmentation of the screen sizes on different devices. In the Appodeal SDK the smart banners are enabled by default. To disable them, use the following method:
Appodeal.setSmartBanners(false);
MREC
1. Display MREC
Add com.appodeal.ads.MrecView
to your layout file:
<com.appodeal.ads.MrecView
android:id="@+id/appodealMrecView"
android:layout_width="300dp"
android:layout_height="250dp"
android:layout_centerInParent="true"
android:visibility="gone" />
Set view id before the SDK initialization:
Appodeal.setMrecViewId(R.id.appodealMrecView);
Now you can show banner in the specified view:
Appodeal.show(this, Appodeal.MREC);
2. Display MREC in programmatically created view
Create banner view:
Appodeal.getMrecView(this);
Add view to your layout and then show the banner in it:
Appodeal.show(this, Appodeal.MREC);
3. Check if MREC is loaded
Appodeal.isLoaded(Appodeal.MREC);
4. Use MREC callbacks
Appodeal.setMrecCallbacks(new MrecCallbacks() {
@Override
public void onMrecLoaded(boolean isPrecache) {
// Called when MREC is loaded
}
@Override
public void onMrecFailedToLoad() {
// Called when MREC failed to load
}
@Override
public void onMrecShown() {
// Called when MREC is shown
}
@Override
public void onMrecShowFailed() {
// Called when MREC show failed
}
@Override
public void onMrecClicked() {
// Called when MREC is clicked
}
@Override
public void onMrecExpired() {
// Called when MREC is expired
}
});
5. Activity "Paused" state handling
We’ll automatically handle pause and resume state for already displayed MRECs on Activity on which they were displayed, however, we don’t restore displayed MRECs after Activity recreation (e.g. - orientation changes) and we don’t show MRECs in newly created Activities.
For display ads on newly created Activity just call Appodea.show()
method if required.
@Override
public void onResume() {
super.onResume();
Appodeal.show(this, Appodeal.MREC);
}
This behavior can be changed by call Appodeal.setSharedAdsInstanceAcrossActivities(true)
(See more: Enable Shared View Ads Instance Across Activities Logic)6. Hide MREC
Appodeal.hide(this, Appodeal.MREC);
7. Destroy shown MREC
If you want to hide the MREC from all activities and clear the memory, call the code below:
Appodeal.destroy(Appodeal.MREC);
8. Cache MREC manually
To disable automatic caching for MREC, use the code below before the SDK initialization:
Appodeal.setAutoCache(Appodeal.MREC, false);
To cache 300*250 banners, use:
Appodeal.cache(this, Appodeal.MREC);
Read more on manual caching in our FAQ.