1

AdMob documentation to integrate it on Android instructs to create the following XML, if we want to create the view via 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/res-auto"
              android:orientation="vertical"
              android:layout_width="match_parent"
              android:layout_height="match_parent">
  <com.google.android.gms.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"/>
</LinearLayout>

And set it as content view to Activity:

public class BannerExample extends Activity {
  @Override
  public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

    // Look up the AdView as a resource and load a request.
    AdView adView = (AdView) this.findViewById(R.id.adView);
    AdRequest adRequest = new AdRequest.Builder().build();
    adView.loadAd(adRequest);
  }
}

But in my case (I use cocos2d-x 3) I have already a subclass of Activity and I just need to add that view to my existing content, not set content view as it is done in the code above. How I can addContentView the view that is defined in XML?

fabian
  • 80,457
  • 12
  • 86
  • 114
Narek
  • 38,779
  • 79
  • 233
  • 389
  • could you please add code for your existing content view(xml file) and where you need to add this content in the existing content? Add some more explanation. – Anil Jadhav Jul 24 '14 at 01:13

2 Answers2

1

When you call.

setContentView(R.layout.main);

in onCreate it automatically inflates the view for you so you can start assigning your views from the xml to your own variables like such:

AdView adView = (AdView) findViewById(R.id.adView);

Make sure that the xml layout you defined at the top is the same that is being passed in setContentView(). In this case R.layout.main should contain a View with id adView of AdView type.

In other words, R.layout.main should be:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
              xmlns:ads="http://schemas.android.com/apk/res-auto"
              android:orientation="vertical"
              android:layout_width="match_parent"
              android:layout_height="match_parent">
  <com.google.android.gms.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"/>
</LinearLayout>

If what you want however, is inflate and use main layout and add the AdView, you must inflate the view yourself and add it to some element in main layout. Like such:

RelativeLayout item = (RelativeLayout)findViewById(R.id.item);
View child = getLayoutInflater().inflate(R.layout.child, null);
item.addView(child);

References: How to inflate one view with a layout

Community
  • 1
  • 1
Toguard
  • 437
  • 3
  • 9
  • am I right that `setContentView` adds the View to the Activity to be seen when activity starts? – Narek Jul 24 '14 at 01:12
  • It adds the view if you are explicitly telling the activity to inflate the corresponding layout. – Toguard Jul 24 '14 at 01:18
0

You can simply add

<com.google.android.gms.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"/>

to your existing XML layout file.

Code-Apprentice
  • 81,660
  • 23
  • 145
  • 268