5. Native Ad‎ > ‎1) Android‎ > ‎

ListView Sample

Sample Code for Native Ad in ListView

mypage_layout.xml

<?xml version="1.0" encoding="utf-8"?>

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"

    android:id="@+id/main_layout"

    android:orientation="vertical"

    android:layout_width="fill_parent"

    android:layout_height="fill_parent"

    android:background="@android:color/white"

    >

    <ListView

        android:id="@+id/friend_list"

        android:layout_width="fill_parent"

        android:layout_height="fill_parent"

        android:layout_below="@android:id/widget_frame"

        />

</LinearLayout>


friend_list_item.xml

<RelativeLayout

xmlns:android="http://schemas.android.com/apk/res/android"

android:id="@+id/contents_item"

android:layout_width="fill_parent"

android:layout_height="wrap_content"

android:padding="8dp"

android:background="#ffffffff">

<TextView

android:id="@+id/list_name_txt"

android:layout_width="fill_parent"

android:layout_height="wrap_content"

android:layout_alignParentTop="true"

android:singleLine="true"

/>

<TextView

android:id="@+id/list_desc_txt"

android:layout_width="fill_parent"

android:layout_height="wrap_content"

android:layout_below="@id/list_name_txt"

android:singleLine="true" 

/>

</RelativeLayout>  


native_ad_list_item.xml

<?xml version="1.0" encoding="utf-8"?>

<RelativeLayout

xmlns:android="http://schemas.android.com/apk/res/android"

android:id="@+id/ad_layout"

android:layout_width="match_parent"

android:layout_height="wrap_content"

android:padding="6dp"

android:background="#ffffffff">

<ImageView

android:id="@+id/ad_icon"

android:layout_width="72dp"

android:layout_height="72dp"

android:layout_alignParentTop="true"

android:layout_alignParentLeft="true"

android:padding="4dp"

android:scaleType="fitXY" />

<TextView

android:id="@+id/ad_title"

android:layout_width="fill_parent"

android:layout_height="wrap_content"

android:layout_toRightOf="@id/ad_icon"

android:layout_alignParentTop="true"

android:layout_marginTop="3dp"

android:layout_marginLeft="8dp"

android:gravity="center_vertical"

android:textColor="#ff020202"

android:textSize="17sp"

/>

<TextView

android:id="@+id/ad_desc"

android:layout_width="fill_parent"

android:layout_height="wrap_content"

android:layout_toRightOf="@id/ad_icon"

android:layout_below="@id/ad_title"

android:layout_marginLeft="8dp"

android:layout_marginTop="8dp"

android:gravity="center_vertical"

android:textColor="#ff179dce"

android:textSize="13sp"

/>

</RelativeLayout>


MyPageActivity.java

public class MyPageActivity extends Activity {


@Override

    public void onCreate(Bundle savedInstanceState) {

    super.onCreate(savedInstanceState);

    

    

    setContentView(R.layout.mypage_layout);

    

    ListView list = (ListView)findViewById(R.id.friend_list);

    list.setAdapter(new MyPageAdapter(this));

}

}


MyPageAdapter.java

public class MyPageAdapter extends BaseAdapter {


  private List<Object> items = new ArrayList<Object>();

  private Context context = null;

  public MyPageAdapter(Context context){

     this.context = context;

     initItems();

  }

  private void initItems(){

     items.add(new MyPageItem("title-1","desc-1"));

     items.add(new MyPageItem("title-2","desc-2"));

     items.add(new MyPageItem("title-3","desc-3"));

     items.add(new MyPageItem("title-4","desc-4"));

     items.add(new MyPageItem("title-5","desc-5"));

     items.add(new MyPageItem("title-6","desc-6"));

     items.add(new NativeAdItem(context, NativeAdItem.STYLE_ICON, adListener));

     items.add(new MyPageItem("title-7","desc-7"));

     items.add(new MyPageItem("title-8","desc-8"));

     items.add(new MyPageItem("title-9","desc-9"));

     items.add(new MyPageItem("title-10","desc-10"));

     items.add(new MyPageItem("title-11","desc-11"));

     items.add(new MyPageItem("title-12","desc-12"));

     items.add(new MyPageItem("title-13","desc-13"));

     items.add(new MyPageItem("title-14","desc-14"));

     items.add(new NativeAdItem(context, NativeAdItem.STYLE_ICON, adListener));

     items.add(new MyPageItem("title-15","desc-15"));

     items.add(new MyPageItem("title-16","desc-16"));

     items.add(new MyPageItem("title-17","desc-17"));

     items.add(new MyPageItem("title-18","desc-18"));

     items.add(new MyPageItem("title-19","desc-19"));

  }

  @Override

  public int getCount() {

     return items.size();

  }


  @Override

  public Object getItem(int position) {

     return items.get(position);

  }

  @Override

  public long getItemId(int position) {

return position;

  }

  @Override

  public int getViewTypeCount() {

     return 2;

  }

  @Override

  public int getItemViewType (int position) {

     Object item = items.get(position);

     if (item instanceof NativeAdItem) {

     return 0;

     }

     else {

     return 1;

     }

  }

  @Override

  public View getView(int position, View convertView, ViewGroup parent) {

     LayoutInflater inflater = (LayoutInflater) parent.getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);

     Object item = items.get(position);

     if (item instanceof NativeAdItem) {

     NativeAdItem adItem = (NativeAdItem)item;

     View adLayout = adItem.getAttachedLayout();

     if (adLayout == null) {

// not requested yet

           adLayout = inflater.inflate(R.layout.native_ad_list_item, null);

           adItem.attachLayout((ViewGroup)adLayout);

           adItem.prepareAd();

     }

     return adLayout;

}

     else {

     if (convertView == null ){

           convertView = inflater.inflate(R.layout.friend_list_item, null);

     }

// contents data

     MyPageItem pageItem = (MyPageItem)item;

     TextView titleView = (TextView)convertView.findViewById(R.id.list_name_txt);

     titleView.setText(pageItem.title);

     TextView descView = (TextView)convertView.findViewById(R.id.list_desc_txt);

     descView.setText(pageItem.description);

     return convertView;

}

  }

  private NativeAdListener adListener = new NativeAdListener() {


     @Override

     public void onFailure(int errCode) {

     Log.d("tnkad", "MyPageAdapter : onFailure " + errCode);

     }


     @Override

     public void onLoad(NativeAdItem adItem) {

     Log.d("tnkad", "MyPageAdapter : onLoad");

     View view = adItem.getAttachedLayout();

     ImageView iconView = (ImageView)view.findViewById(R.id.ad_icon);

     iconView.setImageBitmap(adItem.getIconImage());

     TextView titleView = (TextView)view.findViewById(R.id.ad_title);

     titleView.setText(adItem.getTitle());

     TextView descView = (TextView)view.findViewById(R.id.ad_desc);

     descView.setText(adItem.getDescription());

}


     @Override

     public void onClick() {

     Log.d("tnkad", "MyPageAdapter : onClick");

     }


     @Override

     public void onShow() {

     Log.d("tnkad", "MyPageAdapter : onShow");

     }

  };

  private class MyPageItem {

     public String title = null;

     public String description = null;

     public MyPageItem(String title, String desc){

     this.title = title;

     this.description = desc;

     }

  }


}

Comments