I’m implementing the Native Ads in Android using Native Ads - Huawei documentation. When I come across to change the height of the MediaView according to Screen Size (0 dp), I have found that it couldn't be changed.
Even though I have implemented the setOnHierarchyChangeListener, but it doesn't work either. The following is what I have achieved currently.
File native_ad.xml
<com.huawei.hms.ads.nativead.NativeView xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="@+id/native_video_view"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_centerInParent="true"
android:background="#FFFFFF"
android:orientation="vertical">
<androidx.constraintlayout.widget.ConstraintLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<androidx.constraintlayout.widget.ConstraintLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<com.huawei.hms.ads.nativead.MediaView
android:id="@+id/ad_media"
android:layout_width="0dp"
android:layout_height="0dp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHeight_percent="0.8"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintBottom_toTopOf="@id/constraintLayout"/>
<androidx.constraintlayout.widget.ConstraintLayout
android:id="@+id/constraintLayout"
android:layout_width="0dp"
android:layout_height="0dp"
app:layout_constraintHeight_percent="0.2"
android:background="@drawable/white_bg"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@id/ad_media">
<TextView
... />
<TextView
... />
<TextView
.. />
<Button
... />
</androidx.constraintlayout.widget.ConstraintLayout>
</androidx.constraintlayout.widget.ConstraintLayout>
</com.huawei.hms.ads.nativead.NativeView>
Function inItNativeAdView
private fun initNativeAdView(nativeAd: NativeAd?, nativeView: NativeView) {
nativeView.titleView = nativeView.findViewById(R.id.ad_title)
(nativeView.titleView as TextView).text = nativeAd!!.title
nativeView.mediaView = nativeView.findViewById(R.id.ad_media)
nativeView.mediaView.setMediaContent(nativeAd.mediaContent)
nativeView.mediaView.setOnHierarchyChangeListener(object : OnHierarchyChangeListener {
override fun onChildViewAdded(parent: View, child: View) {
*//* if (child is ImageView) {
val imageView: ImageView = child as ImageView
imageView.adjustViewBounds = true
}*//*
val scale = context.resources.displayMetrics.density
val maxHeightPixels = 175
val maxHeightDp = (maxHeightPixels * scale + 0.5f).toInt()
if (child is ImageView) { //Images
val imageView = child
imageView.adjustViewBounds = true
imageView.maxHeight = maxHeightDp
} else { //Videos
val params = child.layoutParams
params.height = maxHeightDp
child.layoutParams = params
}
}
override fun onChildViewRemoved(parent: View, child: View) {}
})
}
Implementation of setOnHierarchyChangeListener doesn't change the height of the mediaView.
Currently I’m viewing the native ads as follows:
I expect something like this with the dynamic mediaView:
How can I fix this?