-
-
Save svenoaks/6ef23d38c333837e7df183f9d430f50e to your computer and use it in GitHub Desktop.
AdMob Mediation Workaround for Smart Banner Ad Types
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//Constants for tablet sized ads (728x90) | |
//AdMob name: LEADERBOARD | |
final int IAB_LEADERBOARD_WIDTH = 728; | |
final int IAB_LEADERBOARD_HEIGHT = 90; | |
//AdMob name: FULL_BANNER | |
final int MED_BANNER_WIDTH = 480; | |
final int MED_BANNER_HEIGHT = 60; | |
//Constants for phone sized ads (320x50) | |
//AdMob name: BANNER | |
final int BANNER_AD_WIDTH = 320; | |
final int BANNER_AD_HEIGHT = 50; | |
//Finds an ad that best fits a user's device. | |
LinearLayout.LayoutParams adViewLayoutParams = null; | |
if(canFit(activity, IAB_LEADERBOARD_WIDTH)) { | |
adViewLayoutParams = new LinearLayout.LayoutParams((int)TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, IAB_LEADERBOARD_WIDTH, activity.getResources().getDisplayMetrics()), | |
(int)TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, IAB_LEADERBOARD_HEIGHT, activity.getResources().getDisplayMetrics())); | |
adViewLayoutParams.gravity = Gravity.CENTER; | |
adView.setAdSize(AdSize.LEADERBOARD); | |
} else if(canFit(activity, MED_BANNER_WIDTH)) { | |
adViewLayoutParams = new LinearLayout.LayoutParams((int)TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, MED_BANNER_WIDTH, activity.getResources().getDisplayMetrics()), | |
(int)TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, MED_BANNER_HEIGHT, activity.getResources().getDisplayMetrics())); | |
adViewLayoutParams.gravity = Gravity.CENTER; | |
adView.setAdSize(AdSize.FULL_BANNER); | |
} else { | |
adViewLayoutParams = new LinearLayout.LayoutParams((int)TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, BANNER_AD_WIDTH, activity.getResources().getDisplayMetrics()), | |
(int)TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, BANNER_AD_HEIGHT, activity.getResources().getDisplayMetrics())); | |
adViewLayoutParams.gravity = Gravity.CENTER; | |
adView.setAdSize(AdSize.BANNER); | |
} | |
//Method from MillenialMedia | |
private static boolean canFit(Activity activity, int adWidth) { | |
int adWidthPx = (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, adWidth, activity.getResources().getDisplayMetrics()); | |
DisplayMetrics metrics = activity.getResources().getDisplayMetrics(); | |
return metrics.widthPixels >= adWidthPx; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment