I got an email from AdMob today saying:
Change to native ads policy: Native ads will require MediaView to render the video or main image asset. In a
I was facing the same problem, after trying a lot with trial and error, I found that wrapping <FrameLayout...>
(in which you are adding UnifiedNativeAdView
) with ScrollView
will resolve this problem.
for (int i = 0; i < mediaView.getChildCount(); i++) {
View view = mediaView.getChildAt(i);
if (view instanceof ImageView) {
((ImageView) view).setAdjustViewBounds(true);
}
}
This works for me.I tried Richard's answer,But it didn't works well in a RecyclerView.
mediaView.setOnHierarchyChangeListener(new ViewGroup.OnHierarchyChangeListener() {
@Override
public void onChildViewAdded(View parent, View child) {
if (child instanceof ImageView) {
ImageView imageView = (ImageView) child;
imageView.setAdjustViewBounds(true);
}
}
@Override
public void onChildViewRemoved(View parent, View child) {}
});
To ensure that all media are as wide as possible and that they respect a maximum height (so there are no surprises depending on the dimensions of the media).
XML
<com.google.android.gms.ads.formats.MediaView
android:id="@+id/ad_media"
android:layout_gravity="center_horizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
JAVA
mediaView.setOnHierarchyChangeListener(new ViewGroup.OnHierarchyChangeListener() {
@Override
public void onChildViewAdded(View parent, View child) {
float scale = context.getResources().getDisplayMetrics().density;
int maxHeightPixels = 175;
int maxHeightDp = (int) (maxHeightPixels * scale + 0.5f);
if (child instanceof ImageView) { //Images
ImageView imageView = (ImageView) child;
imageView.setAdjustViewBounds(true);
imageView.setMaxHeight(maxHeightDp);
} else { //Videos
ViewGroup.LayoutParams params = child.getLayoutParams();
params.height = maxHeightDp;
child.setLayoutParams(params);
}
}
@Override
public void onChildViewRemoved(View parent, View child) {}
});
If implementing Advanced Native Ads, use "imageScaleType" property of MediaView as suggested here in the official docs https://developers.google.com/admob/android/native/advanced#setting_imagescaletype
adView.mediaView.setImageScaleType(ImageView.ScaleType.CENTER_CROP)
or any other ScaleType as per requirement.