Java Code Examples for com.google.android.gms.ads.AdView#setAdUnitId()

The following examples show how to use com.google.android.gms.ads.AdView#setAdUnitId() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: GooglePlayServicesBanner.java    From mobile-sdk-android with Apache License 2.0 6 votes vote down vote up
/**
 * Interface called by the AN SDK to request an ad from the mediating SDK.
 *
 * @param mBC                 the object which will be called with events from the 3rd party SDK
 * @param activity            the activity from which this is launched
 * @param parameter           String parameter received from the server for instantiation of this object
 * @param adUnitID            The 3rd party placement , in adMob this is the adUnitID
 * @param width               Width of ad
 * @param height              Height of ad
 * @param targetingParameters targetingParameters
 */
@Override
public View requestAd(MediatedBannerAdViewController mBC, Activity activity, String parameter,
                      String adUnitID, int width, int height, TargetingParameters targetingParameters) {
    adListener = new GooglePlayAdListener(mBC, super.getClass().getSimpleName());
    adListener.printToClog(String.format(" - requesting an ad: [%s, %s, %dx%d]",
            parameter, adUnitID, width, height));

    adView = new AdView(activity);
    adView.setAdUnitId(adUnitID);
    adView.setAdSize(new AdSize(width, height));
    adView.setAdListener(adListener);

    try {
        adView.loadAd(buildRequest(targetingParameters));
    } catch (NoClassDefFoundError e) {
        // This can be thrown by Play Services on Honeycomb.
        adListener.onAdFailedToLoad(AdRequest.ERROR_CODE_NO_FILL);
    }

    return adView;
}
 
Example 2
Source File: HomeFragment.java    From Android with MIT License 6 votes vote down vote up
private void Ads(View v) {
    //adView = view.findViewById(R.id.adView);
    View adContainer = v.findViewById(R.id.adMobView);
   // Log.e("TAG :BANNERhomefragment",ADMOB_PLEX_BANNER_1);

    AdView mAdView = new AdView(mContext);
    mAdView.setAdSize(AdSize.SMART_BANNER);
    mAdView.setAdUnitId(ADMOB_PLEX_BANNER_1);
    ((RelativeLayout)adContainer).addView(mAdView);
    AdRequest adRequest = new AdRequest.Builder().build();
    mAdView.loadAd(adRequest);
    mInterstitialAd = new InterstitialAd(getActivity());
    mInterstitialAd.setAdUnitId(ADMOB_PLEX_INTERSTITIAL_1);
    mInterstitialAd.loadAd(new AdRequest.Builder().build());
    mInterstitialAd.setAdListener(new AdListener() {
        @Override
        public void onAdClosed() {

        }
    });

}
 
Example 3
Source File: TestAdMobClassicActivity.java    From UltimateRecyclerView with Apache License 2.0 5 votes vote down vote up
private AdView createadmob() {
    AdView mAdView = new AdView(this);
    mAdView.setAdSize(AdSize.MEDIUM_RECTANGLE);
    mAdView.setAdUnitId("ca-app-pub-3940256099942544/6300978111");
    mAdView.setLayoutParams(new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT));
    // Create an ad request.
    AdRequest.Builder adRequestBuilder = new AdRequest.Builder();
    if (admob_test_mode)
        // Optionally populate the ad request builder.
        adRequestBuilder.addTestDevice(AdRequest.DEVICE_ID_EMULATOR);
    // Start loading the ad.
    mAdView.loadAd(adRequestBuilder.build());
    return mAdView;
}
 
Example 4
Source File: MyActivity.java    From googleads-mobile-android-examples with Apache License 2.0 5 votes vote down vote up
private void loadBanner() {
  // Create an ad request.
  adView = new AdView(this);
  adView.setAdUnitId(AD_UNIT_ID);
  adContainerView.removeAllViews();
  adContainerView.addView(adView);

  AdSize adSize = getAdSize();
  adView.setAdSize(adSize);

  AdRequest adRequest = new AdRequest.Builder().build();

  // Start loading the ad in the background.
  adView.loadAd(adRequest);
}
 
Example 5
Source File: AdMobManager.java    From ANEAdMob with Apache License 2.0 5 votes vote down vote up
public void show(String adID, int size, /*int autoHW,*/ int halign, int valign, String testDevice)
{
	hide();
	
	switch(size)
	{
	case 1: _adSize = AdSize.BANNER; break; //set by default, but leave it here for reference
	case 2: _adSize = AdSize.MEDIUM_RECTANGLE; break;
	case 3: _adSize = AdSize.FULL_BANNER; break;
	case 4: _adSize = AdSize.LEADERBOARD; break;
	case 5: _adSize = AdSize.SMART_BANNER; break;
	case 6: _adSize = AdSize.WIDE_SKYSCRAPER; break;
	}
	
	_adView = new AdView(_act);
	_adView.setAdUnitId(adID);
	_adView.setAdSize(_adSize);
	
	AdRequest adRequest = null;
	if(testDevice == null) //no test device
		adRequest = new AdRequest.Builder().build();
	else
		adRequest = new AdRequest.Builder().addTestDevice(testDevice).build(); //eto pizdec
	
	_adView.setAdListener(new AdMobListener(_ctx, "BANNER"));
	
	_params = new RelativeLayout.LayoutParams(-2, -2);
	_params.addRule(halign, -1);
	_params.addRule(valign, -1);
	
	_parentView.addView(_adView, _params);

_adView.loadAd(adRequest);
}
 
Example 6
Source File: AdMobComboProvider.java    From remixed-dungeon with GNU General Public License v3.0 5 votes vote down vote up
@Override
public void displayBanner() {

    adView = new AdView(Game.instance());
    adView.setAdSize(AdSize.SMART_BANNER);
    adView.setAdUnitId(Game.getVar(R.string.easyModeAdUnitId));
    adView.setBackgroundColor(Color.TRANSPARENT);
    adView.setAdListener(new AdmobBannerListener());

    adView.loadAd(AdMob.makeAdRequest());
}
 
Example 7
Source File: MainActivity.java    From googleads-mobile-android-examples with Apache License 2.0 5 votes vote down vote up
/**
 * Adds banner ads to the items list.
 */
private void addBannerAds() {
    // Loop through the items array and place a new banner ad in every ith position in
    // the items List.
    for (int i = 0; i <= recyclerViewItems.size(); i += ITEMS_PER_AD) {
        final AdView adView = new AdView(MainActivity.this);
        adView.setAdSize(AdSize.BANNER);
        adView.setAdUnitId(AD_UNIT_ID);
        recyclerViewItems.add(i, adView);
    }
}
 
Example 8
Source File: AndroidLauncher.java    From martianrun with Apache License 2.0 5 votes vote down vote up
private AdView createAdView() {
    AdView adView = new AdView(this);

    adView.setAdSize(AdSize.SMART_BANNER);
    adView.setAdUnitId(getAdMobUnitId());

    return adView;
}
 
Example 9
Source File: DownloadProfileImageActivity.java    From Instagram-Profile-Downloader with MIT License 4 votes vote down vote up
private void showBannerAd() {
    try {
        adView = new AdView(this);
        adView.setAdSize(AdSize.MEDIUM_RECTANGLE);
        adView.setAdUnitId(getString(R.string.banner_home_footer));
        adContainer.addView(adView);
        AdRequest adRequest = new AdRequest.Builder()
                .build();
        adView.loadAd(adRequest);
        adView.setAdListener(new AdListener() {
            @Override
            public void onAdLoaded() {
                adContainer.setVisibility(View.VISIBLE);
            }

            @Override
            public void onAdFailedToLoad(int errorCode) {
                showFbBannerAd();
            }

            @Override
            public void onAdOpened() {
                // Code to be executed when an ad opens an overlay that
                // covers the screen.
            }

            @Override
            public void onAdLeftApplication() {
                // Code to be executed when the user has left the app.
            }

            @Override
            public void onAdClosed() {
                // Code to be executed when the user is about to return
                // to the app after tapping on an ad.
            }
        });
    } catch (Exception e) {
        Log.e(TAG, "showBannerAd: " + e);
    }
}
 
Example 10
Source File: TestAdvancedAdmobActivity.java    From UltimateRecyclerView with Apache License 2.0 4 votes vote down vote up
private RelativeLayout createadmob() {

        AdSize adSize = AdSize.SMART_BANNER;

        DisplayMetrics dm = getResources().getDisplayMetrics();

        double density = dm.density * 160;
        double x = Math.pow(dm.widthPixels / density, 2);
        double y = Math.pow(dm.heightPixels / density, 2);
        double screenInches = Math.sqrt(x + y);

        if (screenInches > 8) { // > 728 X 90
            adSize = AdSize.LEADERBOARD;
        } else if (screenInches > 6) { // > 468 X 60
            adSize = AdSize.MEDIUM_RECTANGLE;
        } else { // > 320 X 50
            adSize = AdSize.BANNER;
        }

        adSize = AdSize.MEDIUM_RECTANGLE;
        final AdView mAdView = new AdView(this);
        mAdView.setAdSize(adSize);
        mAdView.setAdUnitId("ca-app-pub-3940256099942544/6300978111");
        // Create an ad request.
        AdRequest.Builder adRequestBuilder = new AdRequest.Builder();
        if (admob_test_mode)
            // Optionally populate the ad request builder.
            adRequestBuilder.addTestDevice(AdRequest.DEVICE_ID_EMULATOR);
        // Start loading the ad.
        mAdView.loadAd(adRequestBuilder.build());
        DisplayMetrics displaymetrics = new DisplayMetrics();
        final RelativeLayout layout = AdGoogleDisplaySupport.initialSupport(this, displaymetrics);
        final double ratio = AdGoogleDisplaySupport.ratioMatching(displaymetrics);
        final int ad_height = AdGoogleDisplaySupport.defaultHeight(displaymetrics);
        AdGoogleDisplaySupport.panelAdjust(mAdView, (int) (ad_height * ratio));
        // get display info
        /*  G.display_w = displayMetrics.widthPixels;
        G.display_h = displayMetrics.heightPixels;
        G.scale = Math.max(G.display_w/1280.0f, G.display_h/800.0f);*/
        mAdView.setAdListener(new AdListener() {
            @Override
            public void onAdLoaded() {
                super.onAdLoaded();
                int h = mAdView.getLayoutParams().height;
                AdGoogleDisplaySupport.scale(mAdView, ratio);
                AdGoogleDisplaySupport.panelAdjust(mAdView, (int) (h * ratio));
                //  if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP)
            }
        });
        layout.addView(mAdView);
        return layout;
    }
 
Example 11
Source File: MainActivity.java    From HeartbeatFixerForGCM with Apache License 2.0 4 votes vote down vote up
private void showAd() {
    Log.v(TAG, "showAd");
    if (!mShowAd) {
        mShowAd = true;
        mAdView = new AdView(this);
        mAdView.setAdSize(AdSize.SMART_BANNER);
        mAdView.setAdUnitId("ca-app-pub-5964196502067291/5460955376");
        mAdView.setAdListener(new AdListener() {
            @Override
            public void onAdClosed() {
                Log.v(TAG, "onAdClosed");
            }

            @Override
            public void onAdFailedToLoad(int errorCode) {
                Log.v(TAG, "onAdFailedToLoad, errorCode: " + errorCode);
            }

            @Override
            public void onAdLeftApplication() {
                Log.v(TAG, "onAdLeftApplication");
            }

            @Override
            public void onAdOpened() {
                Log.v(TAG, "onAdOpened");
            }

            @Override
            public void onAdLoaded() {
                Log.v(TAG, "onAdLoaded");
            }

            @Override
            public void onAdClicked() {
                Log.v(TAG, "onAdClicked");
            }

            @Override
            public void onAdImpression() {
                Log.v(TAG, "onAdImpression");
            }
        });
        getRootViewGroup().addView(mAdView, ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);
        mAdView.loadAd(new AdRequest.Builder().build());
    }
}
 
Example 12
Source File: MainActivity.java    From MockSMS with Apache License 2.0 4 votes vote down vote up
private void startDaWork() {
    AdView mAdView = new AdView(MainActivity.this);
    mAdView.setAdSize(AdSize.SMART_BANNER);
    mAdView.setAdUnitId(All.ID_1_BAN);

    LinearLayout container = findViewById(R.id.adContainer);
    container.addView(mAdView, container.getChildCount());

    AdRequest adRequest = new AdRequest
            .Builder()
            .build();
    mAdView.loadAd(adRequest);

    mInterstitialAd = new InterstitialAd(this);
    mInterstitialAd.setAdUnitId(All.ID_1_INTER);
    mInterstitialAd.loadAd(new AdRequest.Builder().build());
    mInterstitialAd.setAdListener(new AdListener() {
        @Override
        public void onAdLoaded() {
        }

        @Override
        public void onAdFailedToLoad(int errorCode) {
            Crashlytics.log("Ad ErrorCode" + errorCode);
        }

        @Override
        public void onAdOpened() {
            Extra.getInstance(getBaseContext()).setClickCount(0);
        }

        @Override
        public void onAdLeftApplication() {
            Extra.getInstance(getBaseContext()).setClickCount(0);
        }

        @Override
        public void onAdClosed() {
            mInterstitialAd.loadAd(new AdRequest.Builder().build());
        }
    });

}
 
Example 13
Source File: ConsentSDK.java    From GDPR-Admob-Android with MIT License 4 votes vote down vote up
public static void initDummyBanner(Context context) {
    AdView adView = new AdView(context);
    adView.setAdSize(AdSize.BANNER);
    adView.setAdUnitId(DUMMY_BANNER);
    adView.loadAd(new AdRequest.Builder().build());
}
 
Example 14
Source File: SearchActivity.java    From Instagram-Profile-Downloader with MIT License 4 votes vote down vote up
private void showBannerAd() {
    try {
        adView = new AdView(this);
        adView.setAdSize(AdSize.BANNER);
        adView.setAdUnitId(getString(R.string.banner_home_footer));
        adContainer.addView(adView);
        AdRequest adRequest = new AdRequest.Builder()
                .build();
        adView.loadAd(adRequest);
        adView.setAdListener(new AdListener() {
            @Override
            public void onAdLoaded() {
                adContainer.setVisibility(View.VISIBLE);
            }

            @Override
            public void onAdFailedToLoad(int errorCode) {
                showFbBannerAd();
            }

            @Override
            public void onAdOpened() {
                // Code to be executed when an ad opens an overlay that
                // covers the screen.
            }

            @Override
            public void onAdLeftApplication() {
                // Code to be executed when the user has left the app.
            }

            @Override
            public void onAdClosed() {
                // Code to be executed when the user is about to return
                // to the app after tapping on an ad.
            }
        });
    }catch (Exception e){
        Log.e(TAG, "showBannerAd: "+e );
    }
}
 
Example 15
Source File: ViewProfileActivity.java    From Instagram-Profile-Downloader with MIT License 4 votes vote down vote up
private void showBannerAd() {
    try {
        adView = new AdView(this);
        adView.setAdSize(AdSize.BANNER);
        adView.setAdUnitId(getString(R.string.banner_home_footer));
        adContainer.addView(adView);
        AdRequest adRequest = new AdRequest.Builder()
                .build();
        adView.loadAd(adRequest);
        adView.setAdListener(new AdListener() {
            @Override
            public void onAdLoaded() {
                adContainer.setVisibility(View.VISIBLE);
            }

            @Override
            public void onAdFailedToLoad(int errorCode) {
                showFbBannerAd();
            }

            @Override
            public void onAdOpened() {
                // Code to be executed when an ad opens an overlay that
                // covers the screen.
            }

            @Override
            public void onAdLeftApplication() {
                // Code to be executed when the user has left the app.
            }

            @Override
            public void onAdClosed() {
                // Code to be executed when the user is about to return
                // to the app after tapping on an ad.
            }
        });
    }catch (Exception e){
        Log.e(TAG, "showBannerAd: "+e );
    }
}
 
Example 16
Source File: DownloadHistoryActivity.java    From Instagram-Profile-Downloader with MIT License 4 votes vote down vote up
private void showBannerAd() {
    try {
        adView = new AdView(this);
        adView.setAdSize(AdSize.BANNER);
        adView.setAdUnitId(getString(R.string.banner_home_footer));
        adContainer.addView(adView);
        AdRequest adRequest = new AdRequest.Builder()
                .build();
        adView.loadAd(adRequest);
        adView.setAdListener(new AdListener() {
            @Override
            public void onAdLoaded() {
                adContainer.setVisibility(View.VISIBLE);
            }

            @Override
            public void onAdFailedToLoad(int errorCode) {

                showFbBannerAd();
            }

            @Override
            public void onAdOpened() {
                // Code to be executed when an ad opens an overlay that
                // covers the screen.
            }

            @Override
            public void onAdLeftApplication() {
                // Code to be executed when the user has left the app.
            }

            @Override
            public void onAdClosed() {
                // Code to be executed when the user is about to return
                // to the app after tapping on an ad.
            }
        });
    } catch (Exception e) {
        Log.e(TAG, "showBannerAd: " + e);
    }
}
 
Example 17
Source File: ProfileFragment.java    From Instagram-Profile-Downloader with MIT License 4 votes vote down vote up
private void showBannerAd() {
    try {
        adView = new AdView(getActivity());
        adView.setAdSize(AdSize.MEDIUM_RECTANGLE);
        adView.setAdUnitId(getString(R.string.banner_home_footer));
        adContainer.addView(adView);
        AdRequest adRequest = new AdRequest.Builder()
                .build();
        adView.loadAd(adRequest);
        adView.setAdListener(new AdListener() {
            @Override
            public void onAdLoaded() {
                adContainer.setVisibility(View.VISIBLE);
            }

            @Override
            public void onAdFailedToLoad(int errorCode) {
                showFbBannerAd();
            }

            @Override
            public void onAdOpened() {
                // Code to be executed when an ad opens an overlay that
                // covers the screen.
            }

            @Override
            public void onAdLeftApplication() {
                // Code to be executed when the user has left the app.
            }

            @Override
            public void onAdClosed() {
                // Code to be executed when the user is about to return
                // to the app after tapping on an ad.
            }
        });
    }catch (Exception e){
        Log.e(TAG, "showBannerAd: "+e );
    }
}
 
Example 18
Source File: MainPageActivity.java    From AndroidFaceRecognizer with MIT License 4 votes vote down vote up
@Override
protected void onCreate(Bundle savedInstanceState) {
	super.onCreate(savedInstanceState);
	setContentView(R.layout.main_page);
	
	
	adView = new AdView(this);
	adView.setAdUnitId("ca-app-pub-4147454460675251/3837655321");
	adView.setAdSize(AdSize.BANNER);
	
	adView.setAdListener(adListener);
	
	RelativeLayout adLayout = (RelativeLayout)findViewById(R.id.main_page_adview_layout);
	adLayout.addView(adView);
	
	RelativeLayout.LayoutParams adViewlp = new RelativeLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
	adViewlp.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM);
	adViewlp.addRule(RelativeLayout.CENTER_HORIZONTAL);
	adView.setLayoutParams(adViewlp);

	
	AdRequest request = new AdRequest.Builder()
	.addTestDevice(AdRequest.DEVICE_ID_EMULATOR)
    .addTestDevice("04E14595EE84A505616E50A99B15252D")
    .build();
	
	
	adView.loadAd(request);
	
	System.out.println("---------------------- istestdevice: "+request.isTestDevice(this));

	
	mContext = this;
	
	DisplayMetrics dm = new DisplayMetrics();
	getWindowManager().getDefaultDisplay().getMetrics( dm );
	
	screenWidth = dm.widthPixels;
	screenHeight = dm.heightPixels;
	
	final TextView headerText = (TextView)findViewById(R.id.mainHeaderText);
	RelativeLayout.LayoutParams headerTextParams = (RelativeLayout.LayoutParams)headerText.getLayoutParams();
	headerTextParams.leftMargin = screenHeight/8;
	headerText.setLayoutParams(headerTextParams);
	headerText.setTextSize(TypedValue.COMPLEX_UNIT_DIP, (float)screenHeight/35);
	headerText.setText("Face Recognition");
	headerText.setTextColor(Color.LTGRAY);
	headerText.setGravity(Gravity.LEFT | Gravity.CENTER_VERTICAL);
	headerText.setTypeface(null, Typeface.BOLD);
	
	ImageView headerIcon = (ImageView)findViewById(R.id.mainHeaderIcon);
	RelativeLayout.LayoutParams iconLParams = new RelativeLayout.LayoutParams(screenHeight/11, screenHeight/11);
	iconLParams.addRule(RelativeLayout.ALIGN_PARENT_LEFT | RelativeLayout.CENTER_VERTICAL);
	iconLParams.leftMargin = screenHeight/80;
	headerIcon.setLayoutParams(iconLParams);
	
	
	ListView listView = (ListView)findViewById(R.id.mainListView);
	ListAdapter listAdapter = new ListAdapter();
	listView.setAdapter(listAdapter);
	listView.setOnItemClickListener(itemClickListener);
	
	RelativeLayout.LayoutParams params = (RelativeLayout.LayoutParams)listView.getLayoutParams();
	params.leftMargin = screenWidth/10;
	params.rightMargin = screenWidth/10;
	params.topMargin = screenHeight/12;
	listView.setLayoutParams(params);
	listView.setVerticalScrollBarEnabled(false);

}
 
Example 19
Source File: StoriesOverview.java    From Instagram-Profile-Downloader with MIT License 4 votes vote down vote up
private void showBannerAd() {
    try {
        adView = new AdView(getActivity());
        adView.setAdSize(AdSize.BANNER);
        adView.setAdUnitId(getString(R.string.banner_home_footer));
        adContainer.addView(adView);
        AdRequest adRequest = new AdRequest.Builder()
                .build();
        adView.loadAd(adRequest);
        adView.setAdListener(new AdListener() {
            @Override
            public void onAdLoaded() {
                adContainer.setVisibility(View.VISIBLE);
            }

            @Override
            public void onAdFailedToLoad(int errorCode) {
                showFbBannerAd();
            }

            @Override
            public void onAdOpened() {
                // Code to be executed when an ad opens an overlay that
                // covers the screen.
            }

            @Override
            public void onAdLeftApplication() {
                // Code to be executed when the user has left the app.
            }

            @Override
            public void onAdClosed() {
                // Code to be executed when the user is about to return
                // to the app after tapping on an ad.
            }
        });
    } catch (Exception e) {
        Log.e(TAG, "showBannerAd: " + e);
    }
}
 
Example 20
Source File: DownloadPostFragment.java    From Instagram-Profile-Downloader with MIT License 4 votes vote down vote up
private void showBannerAd() {
    try {
        adView = new AdView(getActivity());
        adView.setAdSize(AdSize.MEDIUM_RECTANGLE);
        adView.setAdUnitId(getString(R.string.banner_home_footer));
        adContainer.addView(adView);
        AdRequest adRequest = new AdRequest.Builder()
                .build();
        adView.loadAd(adRequest);
        adView.setAdListener(new AdListener() {
            @Override
            public void onAdLoaded() {
                adContainer.setVisibility(View.VISIBLE);
            }

            @Override
            public void onAdFailedToLoad(int errorCode) {
                showFbBannerAd();
            }

            @Override
            public void onAdOpened() {
                // Code to be executed when an ad opens an overlay that
                // covers the screen.
            }

            @Override
            public void onAdLeftApplication() {
                // Code to be executed when the user has left the app.
            }

            @Override
            public void onAdClosed() {
                // Code to be executed when the user is about to return
                // to the app after tapping on an ad.
            }
        });
    } catch (Exception e) {
        Log.e(TAG, "showBannerAd: " + e);
    }
}