# Native Ad

## Declare `NativeAd` and `MediaView` class

```java
import com.intowow.sdk.NativeAd;
import com.intowow.sdk.NativeAd.MediaView;

private final static String PLACEMENT_NAME = "Obtain from your Intowow account manager";
private NativeAd mNativeAd  = null;
private MediaView mMediaView = null;
```

## Initialize `NativeAd` class

```java
mNativeAd = new NativeAd(mActivity);
```

**NOTE :**\
please make sure to pass `Activity` on to the NativeAd, if you can't, then you may need to pass it on `MediaView(Activity);` later.

## Setup the RequestInfo

* You need to set the placement ID and the request timeout in requestInfo.

```java
RequestInfo requestInfo = new RequestInfo();
requestInfo.setPlacement(PLACEMENT_NAME);
requestInfo.setTimeout(timeout);
```

### loadAdInstant

```java
import com.intowow.sdk.CERequestResult;
​
CERequestResult result = mNativeAd.loadAdInstant(requestInfo);
 if (result.isSuccess()) {
    //  Get ad title string.
    String title = mNativeAd.getAdTitle()
    
    //  Get ad description string.
    String adBody = mNativeAd.getAdBody();
    
    //  Get call to action string.
    String callToAction = mNativeAd.getAdCallToAction();
    
    //  Instantiate MediaView
    //  please make sure to pass activity on to the mediaview
    //
    int width = your_defined_width;
    mMediaView = new MediaView(activity);
    RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(width, 
    RelativeLayout.LayoutParams.WRAP_CONTENT);
    mMediaView.setLayoutParams(params);
    mMediaView.setNativeAd(mNativeAd);
    //  Add these materials to the UI hierarchy
 } else {
    // You can call result.getError() to know info of loadAd fail
 }
}
```

With `loadAdInstant` , our synchronous method , You will get the result of loading ad by [CERequestResult](https://intowow.gitbook.io/crystalexpress-documentation-v3-x/android-sdk/api-reference/cerequestresult) directly.&#x20;

### loadAdAsync

```java
import com.intowow.sdk.CEAdListener;
import com.intowow.sdk.CEAdRequestListener;

mNativeAd.loadAdAsync(requestInfo,  new CEAdRequestListener() {
        @Override
            public void onError(Ad ad, AdError error) {
            }

        @Override
            public void onAdLoaded(Ad ad) {
                if(mNativeAd != ad) {
                    return;
                }

            //  Get ad title string.
            String title = mNativeAd.getAdTitle()
    
            //  Get ad description string.
            String adBody = mNativeAd.getAdBody();
    
            //  Get call to action string.
            String callToAction = mNativeAd.getAdCallToAction();
    
            //  Instantiate MediaView
            //  please make sure to pass activity on to the mediaview
            //
            int width = your_defined_width;
            mMediaView = new MediaView(activity);
            RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(
              width, RelativeLayout.LayoutParams.WRAP_CONTENT);
            mMediaView.setLayoutParams(params);
            mMediaView.setNativeAd(mNativeAd);
    
            //  Add these materials to the UI hierarchy
        }
    });
```

loadAdAsync is an asynchronous method, you can set timeout via [RequestInfo](https://intowow.gitbook.io/crystalexpress-documentation-v3-x/~/edit/drafts/-LKuFjp4PCGTgRxN52Xr/android-sdk/api-reference/requestinfo), and get the result of ad request by [CEAdRequestListener](https://intowow.gitbook.io/crystalexpress-documentation-v3-x/android-sdk/api-reference/ceadrequestlistener).

## Add `AdListener` to get ad event callback.

```java
import com.intowow.sdk.Ad;
import com.intowow.sdk.AdError;
import com.intowow.sdk.CEAdListener;
import com.intowow.sdk.NativeAd;
import com.intowow.sdk.NativeAd.MediaView;

mNativeAd.setAdListener(new CEAdListener() {

    @Override
    public void onAdClicked(Ad ad) {
    }

    @Override
    public void onAdImpression(Ad ad) {
    }

    @Override
    public void onAdMute(Ad ad) {
    }

    @Override
    public void onAdUnmute(Ad ad) {
    }

    @Override
    public void onVideoStart(Ad ad) {
    }

    @Override
    public void onVideoProgress(Ad ad, int totoalDuration, int currentPosition) {
    }

    @Override
    public void onVideoEnd(Ad ad) {
    }
});
```

## Register ad

* You must register ad view to handle user engage after `onAdLoaded` callback.

```java
// Customize ad view
TextView callToActionView = new TextView(mActivity);
callToActionView.setText(mNativeAd.getAdCallToAction());

RelativeLayout nativeAdContainer = new RelativeLayout(mActivity);
nativeAdContainer.addView(mMediaView);
nativeAdContainer.addView(callToActionView);
...

// Register clickable area
List<View> actionViews = new ArrayList<View>();
actionViews.add(callToActionView);
mNativeAd.registerViewForInteraction(nativeAdContainer, actionViews);
```

## Release ad

```java
@Override
protected void onDestroy() {
    if(mMediaView != null) {
        mMediaView.destroy();
        mMediaView = null;
    }
    if(mNativeAd != null) {
        mNativeAd.destroy();
        mNativeAd = null;
    }
    ...
    ...
    super.onDestroy();
}
```

## If you want to get more information about integration, please refer to [NativeAd](https://intowow.gitbook.io/crystalexpress-documentation-v3-x/ad-formats/native) and [MediaView](https://intowow.gitbook.io/crystalexpress-documentation-v3-x/android-sdk/api-reference/ad/nativead/mediaview)
