Exemplo n.º 1
0
  private void _initAdMob() {

    FrameLayout.LayoutParams params =
        new FrameLayout.LayoutParams(
            FrameLayout.LayoutParams.MATCH_PARENT, FrameLayout.LayoutParams.WRAP_CONTENT);
    params.gravity = Gravity.TOP | Gravity.CENTER_HORIZONTAL;

    LayoutInflater inflate = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    View view = inflate.inflate(R.layout.admob, null);
    addContentView(view, params);

    adViewLayout = (FrameLayout) view.findViewById(R.id.admob);

    // Create an ad.
    adView = new AdView(this);
    adView.setAdSize(AdSize.BANNER);
    adView.setAdUnitId(getResources().getString(R.string.AD_UNIT_ID));
    adView.setLayoutParams(params);

    // Add the AdView to the view hierarchy. The view will have no size
    // until the ad is loaded.
    adViewLayout.addView(adView);

    // Create an ad request. Check logcat output for the hashed
    // device ID to
    // get test ads on a physical device.
    AdRequest adRequest =
        new AdRequest.Builder()
            .addTestDevice(AdRequest.DEVICE_ID_EMULATOR)
            .addTestDevice("INSERT_YOUR_HASHED_DEVICE_ID_HERE")
            .build();

    // Start loading the ad in the background.
    adView.loadAd(adRequest);

    adViewLayout.setBackgroundColor(Color.BLACK);
    adViewLayout.setBackgroundColor(0);
    showAd();
  }
  private void createAdView() {
    // Create and plate the Banner Ad in activity_main.xml
    adView = new AdView(this);
    adView.setAdUnitId("ca-app-pub-1314947069846070/4608411941");
    adView.setAdSize(AdSize.BANNER);

    RelativeLayout.LayoutParams params =
        new RelativeLayout.LayoutParams(
            RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT);
    params.addRule(RelativeLayout.CENTER_HORIZONTAL);
    params.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM);
    adView.setLayoutParams(params);

    RelativeLayout background = (RelativeLayout) findViewById(R.id.activity_main);
    background.addView(adView);

    // Request for Ads
    AdRequest adRequest =
        new AdRequest.Builder().addTestDevice("FFAA2B4ECD49CBF2A0AB7F9D447410D7").build();

    // Load ad request into Adview
    adView.loadAd(adRequest);
  }