Sunday, 23 June 2013

Admob Demo ( Show Ads on Android Apps )



Demo:

1)MainActivity.java:

package com.venky.admobdemo; 
import android.app.Activity;
import android.os.Bundle;
import android.widget.LinearLayout;
import com.google.ads.AdRequest;
import com.google.ads.AdSize;
import com.google.ads.AdView;

public class MainActivity extends Activity {

      public AdView adView;
      private static final String UNIT_ID = "a151bafe5965f08";

      @Override
      protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);

            adView = new AdView(MainActivity.this, AdSize.BANNER, UNIT_ID);

            LinearLayout ll = (LinearLayout) findViewById(R.id.layout);

            ll.addView(adView);

            adView.loadAd(new AdRequest());
      }
}


2)activity_main.xml:


<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:ads="http://schemas.android.com/apk/lib/com.google.ads"
    android:id="@+id/layout"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical" >

    <com.google.ads.AdView
        android:id="@+id/adView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        ads:adSize="BANNER"
        ads:adUnitId="a151bafe5965f08"
        ads:loadAdOnCreate="true" />

</LinearLayout>


3)Required Permission: 

<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />


4)Add this Activity in manifest.xml:


<activity android:name="com.google.ads.AdActivity"
     
android:configChanges="keyboard|keyboardHidden|orientation|
                                screenLayout|uiMode|screenSize|smallestScreenSize" />

5)Download this code Click Here 

No comments:

Post a Comment