Question

I have an inner class which extends View class inside my activity. Am setting content View as the innerclass view. I want to add an AdView to myactivity. which is a banner.. can anybody help me to achive this? Am new to android..

Class MyActivity extends Activity {

  View myView;

     OnCreate()
      {
       myView= new ViewClass(this);
       setContentView(myView);
      }


   Class ViewClass extends View
     {
       //drawing inside a canvas and other drawing activities
     }

}

Now I want to add a AdView to the screen. How can I achieve this?

Thanks in advance

Was it helpful?

Solution

In your xml add your view class like

Let's name it myLayout.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
        xmlns:android="http://schemas.android.com/apk/res/android"
        android:orientation="vertical"
        android:layout_width="match_parent"
        android:layout_height="match_parent">
<package.MyActivity$ViewClass android:layout_width="wrap_content"
        android:layout_height="wrap_content"/>
<com.google.ads.AdView android:id="@+id/adView"
                     android:layout_width="wrap_content"
                     android:layout_height="wrap_content"
                     ads:adUnitId="MY_AD_UNIT_ID"
                     ads:adSize="BANNER"
                     ads:testDevices="TEST_EMULATOR, TEST_DEVICE_ID"
                     ads:loadAdOnCreate="true"/>

<LinearLayout/>



@Override
  onCreate()
  {
   setContentView(R.layout.myLayout);
   myView= (ViewClass)findViewById(R.id.viewClass);

  }

More information on creating a view class.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top