Title: Android - AddContentView defined in XML
Tags: java;android
Question: 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"?&gt;
<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"&gt;
  <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"/&gt;
</LinearLayout&gt;
```

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?

Comment: 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.


Here is another answer: 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"?&gt;
<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"&gt;
  <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"/&gt;
</LinearLayout&gt;
```

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

Comment for this answer: am I right that `setContentView` adds the View to the Activity to be seen when activity starts?
Comment for this answer: It adds the view if you are explicitly telling the activity to inflate the corresponding layout.

Here is another answer: 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"/&gt;
```

to your existing XML layout file.


{'source': 'StackExchangeDataset', 'subset': 'stackoverflow.com', 'metadata': {'AnswerCount': 2, 'CommentCount': 1, 'ContentLicense': 'CC BY-SA 3.0', 'CreationDate': '2014:00:07/24/14', 'FavoriteCount': None, 'PostTypeId': 1, 'Tags': '<java><android>', 'Title': 'Android - AddContentView defined in XML', 'ViewCount': 615, 'id': 24923485, 'user_AboutMe': '<p>Game developer.</p>\n', 'user_AccountId': 54601.0, 'user_DisplayName': 'Narek', 'user_Reputation': 37511.0, 'user_Views': 4004.0, 'user_id': 163394.0}}