-
Notifications
You must be signed in to change notification settings - Fork 1.4k
Admob implementation deprecated
Leetsong edited this page Jun 5, 2017
·
4 revisions
private AdView createadmob() {
AdView mAdView = new AdView(this);
mAdView.setAdSize(AdSize.MEDIUM_RECTANGLE);
mAdView.setAdUnitId("__GOOGLE_AD_UNIT__ID__");
mAdView.setLayoutParams(new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT));
// 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;
}
wokring example 1 implementation of Admob banner with static Adview ...oncreate
/**
* wokring example 1 implementation of Admob banner with static Adview
*/
simpleRecyclerViewAdapter = new admobdfpadapter(createadmob(), 5, stringList);
working example 2 with multiple called Adviews
/**
* working example 2 with multiple called Adviews
*/
simpleRecyclerViewAdapter = new admobdfpadapter(createadmob(), 5, stringList, new AdmobAdapter.AdviewListener() {
@Override
public AdView onGenerateAdview() {
return createadmob();
}
});
There is a new way to implement the AdView into the recyclerView
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("___google__ad_id__");
// 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();
getWindowManager().getDefaultDisplay().getMetrics(displaymetrics);
int dw = displaymetrics.widthPixels;
int dh = displaymetrics.heightPixels;
// final double ratio = ((float) (dw)) / 300.0;
RelativeLayout layout = new RelativeLayout(this);
final double ratio = 1.21d;
//Math.max(dw / 1280.0f, dh / 800.0f);
int height = (int) (ratio * 250);
// Add the adView to it
RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(
RelativeLayout.LayoutParams.WRAP_CONTENT,
RelativeLayout.LayoutParams.WRAP_CONTENT);
params.addRule(RelativeLayout.ALIGN_PARENT_RIGHT, RelativeLayout.TRUE);
params.addRule(RelativeLayout.ALIGN_PARENT_LEFT, RelativeLayout.TRUE);
params.addRule(RelativeLayout.ALIGN_PARENT_TOP, RelativeLayout.TRUE);
params.addRule(RelativeLayout.ALIGN_BOTTOM, RelativeLayout.TRUE);
params.addRule(RelativeLayout.CENTER_HORIZONTAL, RelativeLayout.TRUE);
mAdView.setLayoutParams(params);
// get display info
DisplayMetrics displayMetrics = new DisplayMetrics();
getWindowManager().getDefaultDisplay().getMetrics(displayMetrics);
/* 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 g = mAdView.getLayoutParams().width;
int h = mAdView.getLayoutParams().height;
ViewCompat.setScaleX(mAdView, (float) ratio);
ViewCompat.setScaleY(mAdView, (float) ratio);
}
});
layout.addView(mAdView);
return layout;
}