# 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](/crystalexpress-documentation-v3-x/android-sdk/api-reference/cerequestresult.md) 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](/crystalexpress-documentation-v3-x/android-sdk/api-reference/ceadrequestlistener.md).

## 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](/crystalexpress-documentation-v3-x/ad-formats/native.md) and [MediaView](/crystalexpress-documentation-v3-x/android-sdk/api-reference/ad/nativead/mediaview.md)


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://intowow.gitbook.io/crystalexpress-documentation-v3-x/android-sdk/quickstart-guide/step-2.-choose-ad-format/native-ad.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
