title: How to specify adUnitId programmatically for AdMob?
body: <p>I'm trying to set adUnitId programmatically to ads from the new Google Play services (old AdMob).</p>
  
  <p>I have this in XML (used in an <code>&lt;include&gt;</code>):</p>
  
  <pre><code>&lt;com.google.android.gms.ads.AdView
      xmlns:android="http://schemas.android.com/apk/res/android"
      xmlns:ads="http://schemas.android.com/apk/res-auto"
      android:id="@+id/adView"
      android:layout_width="match_parent"
      android:layout_height="wrap_content"
      ads:adSize="BANNER"/&gt;
  </code></pre>
  
  <p>and this in onCreate():</p>
  
  <pre><code>AdView mAdview = (AdView)findViewById(R.id.adView);
      mAdview.setAdUnitId(((App)getApplication()).getAdmobKey());
  
      mAdview.setAdListener(new AdListener() {
          @Override
          public void onAdLoaded() {
              super.onAdLoaded();
              findViewById(R.id.adView).setVisibility(View.VISIBLE);
          }
      });
  
      AdRequest adRequest = new AdRequest.Builder()
              .addTestDevice(AdRequest.DEVICE_ID_EMULATOR)
              .build();
      mAdview.loadAd(adRequest);
  </code></pre>
  
  <p>And I get:</p>
  
  <blockquote>
    <p>The ad size and ad unit ID must be set before loadAd is called.</p>
  </blockquote>
  
  <p>So the second option was to make the ad programmatically. </p>
  
  <p>The new XML:</p>
  
  <pre><code>&lt;LinearLayout
      xmlns:android="http://schemas.android.com/apk/res/android"
      android:layout_height="wrap_content"
      android:layout_width="match_parent"
      android:id="@+id/adView"
      /&gt;
  </code></pre>
  
  <p>The new code:</p>
  
  <pre><code>AdView mAdview = new AdView(this);
  ...
  ((LinearLayout)findViewById(R.id.adView)).addView(mAdview);
  mAdview.loadAd(adRequest);
  </code></pre>
  
  <p>But I get the same error.</p>
  
  <p>I tried also to inherit from com.google.android.gms.ads.AdView to make a custom view, but it's final.</p>
  
  <p>Any suggestion?</p>