Skip to content

Native Integration

CMAdSDK edited this page May 13, 2016 · 16 revisions

NativeAd Integration for Android

Set up your Native Ad Layout

For example: res/layout/native_ad_layout.xml

		<FrameLayout
     		 android:id="@+id/big_ad_container"
     		 android:layout_width="match_parent"
      	 android:layout_height="wrap_content"
     		 android:layout_margin="4dp"
    	     android:padding="2dp" >
  	</FrameLayout>

admob ad you need two layout

1:InstallAdView
	<com.google.android.gms.ads.formats.NativeAppInstallAdView
		xmlns:android="http://schemas.android.com/apk/res/android"
		android:layout_width="match_parent"
		android:layout_height="wrap_content">
		<LinearLayout
    		xmlns:android="http://schemas.android.com/apk/res/android"
    		android:layout_width="match_parent"
    		android:orientation="vertical"
    		android:background="@drawable/ad_border"
    		android:layout_height="wrap_content" >
		</LinearLayout>
	</com.google.android.gms.ads.formats.NativeAppInstallAdView>

2:ContentAdView
	<com.google.android.gms.ads.formats.NativeContentAdView
		xmlns:android="http://schemas.android.com/apk/res/android"
		android:layout_width="match_parent"
		android:layout_height="wrap_content" >
		<LinearLayout
    		xmlns:android="http://schemas.android.com/apk/res/android"
    		android:layout_width="match_parent"
    		android:orientation="vertical"
    		android:background="@drawable/ad_border"
    		android:layout_height="wrap_content" >
		</LinearLayout>
	</com.google.android.gms.ads.formats.NativeContentAdView>

Sample code:

 // The first parameter:Context
 // The second parameter:Posid
 if(nativeAdManagerEx == null){
    nativeAdManagerEx = new NativeAdManagerEx(this, "Your posid");
 }
 nativeAdManagerEx.setNativeAdListener(new INativeAdLoaderListener() {
	       @Override
	       public void adLoaded() {
			     //get ad
	       		 INativeAd ad = nativeAdManagerEx.getAd();
	       		 if (ad == null) {
               			 return;
                  }
			    
			     //Initialize the ad view,and use the elemnets of the 
			     //INativeAd object to fill the mAdView
				//the layout should be prepared by yourself
	           	 View mAdView = View.inflate(MainActivity.this,"Your Ad layout", null);
					
	           
	           
	            //Bind the ad with the mAdView
	            //notice: this step is necessary,if don't ,the event like 
	            //click of the ad will not effective.
	            //unregisterView should be used when the ad no need to show.
	             ad.registerViewForInteraction(mAdView);
			
	             if (mAdView != null) {
                    // remove old mAdView
                    nativeAdContainer.removeView(mAdView);
            	  }
	            	//add the mAdView into the layout of view container.
	            	//(the container should be prepared by youself)
            	 nativeAdContainer.addView(mAdView); 
   		  }

        @Override
        public void adFailedToLoad(int i) {
          // load failed
        }


        @Override
        public void adClicked(INativeAd ad) {
        	//ad clicked
        }
    });
 
	nativeAdManagerEx.loadAd();

Clone this wiki locally