Как разрешить нажатие на мой AdMob UnifiedNativeAdView? - PullRequest
0 голосов
/ 06 октября 2019

Я использую Собственные объявления AdMob , чтобы интегрировать нативные объявления в свое приложение. Я успешно показал активы UnifiedNativeAd в своем фрагменте, однако не могу перейти по URL / ссылке объявления.

MapFragment.kt

lateinit var ad: Fragment

private fun initialiseProfileFragment(){
    /** Ad fragment */
    ad = childFragmentManager.findFragmentById(R.id.ad) ?: return returnToLoginPage()
    // Hide ProfileFragment
    val transaction2 = childFragmentManager.beginTransaction()
    transaction2.hide(ad)
    transaction2.commit()
}

private fun showAd(){
    val adLoader = AdLoader.Builder(context, getString(R.string.test_admob_app_unit_id))
        .forUnifiedNativeAd { nativeAd: UnifiedNativeAd ->
            currentNativeAd = nativeAd
            /** Initialise views */
            val parent = ad_container as UnifiedNativeAdView
            val profilePic1 = parent.findViewById<AppCompatImageView>(R.id.profilePic1)
            val profilePic2 = parent.findViewById<AppCompatImageView>(R.id.profilePic2)
            val profilePic3 = parent.findViewById<AppCompatImageView>(R.id.profilePic3)
            val adChoicesIcon = parent.findViewById<AppCompatImageView>(R.id.adChoicesIcon)
            val name = parent.findViewById<AppCompatTextView>(R.id.name)
            val heading = parent.findViewById<AppCompatTextView>(R.id.heading)
            val sub = parent.findViewById<AppCompatTextView>(R.id.sub)
            val leftButton = parent.findViewById<AppCompatButton>(R.id.leftButton)
            val bio = parent.findViewById<AppCompatTextView>(R.id.bio)
            val profileScroll = parent.findViewById<ScrollView>(R.id.profileScroll)
            /** Load ad images */
            val numberOfImages = nativeAd.images.size
            val options = RequestOptions.centerCropTransform()
            Glide.with(this).load(nativeAd.mediaContent.mainImage).apply(options).into(profilePic1)
            if (numberOfImages > 0) Glide.with(this).load(nativeAd.images[0].drawable).into(profilePic2)
            if (numberOfImages > 1) Glide.with(this).load(nativeAd.images[1].drawable).into(profilePic3)
            /** Add AdChoices icon */
            if (nativeAd.icon?.drawable != null) Glide.with(this).load(nativeAd.icon.drawable).into(adIcon)
            val icon = nativeAd.adChoicesInfo.images[0].drawable
            Glide.with(this).load(icon).into(adChoicesIcon)
            /** Load text */
            heading.text = nativeAd.headline
            heading.setTextSize(TypedValue.COMPLEX_UNIT_SP, 24f)
            bio.text = nativeAd.body
            sub.text = getString(R.string.ad)
            profileScroll.clipToOutline = true
            leftButton.text = nativeAd.callToAction
            parent.setNativeAd(nativeAd)
            val transaction = childFragmentManager.beginTransaction()
            transaction.show(ad)
            transaction.commit()
        }
        .withAdListener(object : AdListener() {
            override fun onAdClicked() {
                log("AD CLICKED")
                super.onAdClicked()
            }
            override fun onAdFailedToLoad(p0: Int) {
                super.onAdFailedToLoad(p0)
                log("Ad failed to load: $p0")
            }
        })
        .build()
    adLoader.loadAd(AdRequest.Builder().build())
}

фрагмент_карты.xml

<!-- This shows fragment_ad.xml -->
<fragment
    android:id="@+id/ad"
    android:name="app.zorgan.AdFragment"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".HomeActivity"
    xmlns:tools="http://schemas.android.com/tools"/>

фрагмент_ад.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
    android:id="@+id/linear_container"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    xmlns:android="http://schemas.android.com/apk/res/android">
    <com.google.android.gms.ads.formats.UnifiedNativeAdView
        android:id="@+id/ad_container"
        android:layout_width="match_parent"
        android:layout_height="match_parent">
        <include
            android:layout_height="match_parent"
            android:layout_width="match_parent"
            layout="@layout/fragment_profile">
        </include>
    </com.google.android.gms.ads.formats.UnifiedNativeAdView>
</LinearLayout>

frag_profile.xml

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:background="@color/colorPopupBackground"
    android:fadeScrollbars="false"
    android:id="@+id/profile_layout">
    <androidx.appcompat.widget.AppCompatTextView
        android:id="@+id/sub"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        app:layout_constraintVertical_chainStyle="packed"
        app:layout_constraintVertical_weight="0.1"
        android:layout_marginTop="66dp"
        android:textAlignment="center"
        android:textSize="@dimen/xlarge_text"
        android:textColor="@color/colorPrimary"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintBottom_toTopOf="@id/profileScroll"/>
    <ScrollView
        android:id="@+id/profileScroll"
        android:fillViewport="true"
        android:background="@drawable/curved_profile"
        app:layout_constraintVertical_weight="0.75"
        android:elevation="10dp"
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_marginStart="30dp"
        android:layout_marginEnd="30dp"
        android:layout_marginTop="10dp"
        app:layout_constraintTop_toBottomOf="@id/sub"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintBottom_toTopOf="@id/choiceContainer"
        app:layout_constraintEnd_toEndOf="parent">
        <androidx.constraintlayout.widget.ConstraintLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content">
            <FrameLayout
                android:background="@drawable/profile_container_gradient"
                android:id="@+id/container1"
                app:layout_constraintTop_toTopOf="parent"
                app:layout_constraintStart_toStartOf="parent"
                app:layout_constraintEnd_toEndOf="parent"
                app:layout_constraintBottom_toTopOf="@id/container1"
                android:layout_width="match_parent"
                android:layout_height="match_parent">
                <androidx.appcompat.widget.AppCompatImageView
                    android:id="@+id/adIcon"
                    android:layout_width="30dp"
                    android:layout_height="30dp"
                    android:layout_gravity="top|end"
                    android:layout_marginEnd="20dp"
                    android:layout_marginTop="20dp"/>
                <androidx.appcompat.widget.AppCompatImageView
                    android:id="@+id/profilePic1"
                    android:layout_width="match_parent"
                    android:layout_height="match_parent" />
                <View
                    android:layout_width="match_parent"
                    android:layout_height="100dp"
                    android:background="@drawable/profile_container_gradient"
                    android:layout_gravity="bottom"/>

                <androidx.appcompat.widget.AppCompatTextView
                    android:id="@+id/name"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:text=""
                    android:layout_marginStart="20dp"
                    android:textSize="@dimen/xxlarge_text"
                    android:textColor="@color/colorPrimary"
                    android:layout_gravity="bottom|start"
                    android:layout_marginBottom="40dp"/>
                <androidx.appcompat.widget.AppCompatTextView
                    android:id="@+id/heading"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:textSize="@dimen/large_text"
                    android:layout_marginStart="20dp"
                    android:textColor="@color/colorPrimary"
                    android:text=""
                    android:layout_gravity="bottom|start"
                    android:layout_marginBottom="10dp"/>
                <androidx.appcompat.widget.AppCompatImageView
                    android:id="@+id/adChoicesIcon"
                    android:layout_width="25dp"
                    android:layout_height="25dp"
                    android:layout_gravity="top|end"
                    android:layout_marginEnd="0dp"
                    android:layout_marginTop="0dp"/>
            </FrameLayout>
            <androidx.appcompat.widget.AppCompatTextView
                android:id="@+id/bio"
                app:layout_constraintTop_toBottomOf="@id/container1"
                app:layout_constraintStart_toStartOf="parent"
                app:layout_constraintEnd_toEndOf="parent"
                app:layout_constraintBottom_toTopOf="@id/profilePic2"
                android:background="@color/colorPrimary"
                android:textColor="@color/colorLightBlack"
                android:textSize="@dimen/small_text"
                android:padding="20dp"
                android:layout_width="match_parent"
                android:layout_height="wrap_content" />
            <androidx.appcompat.widget.AppCompatImageView
                android:id="@+id/profilePic2"
                app:layout_constraintTop_toBottomOf="@id/bio"
                app:layout_constraintStart_toStartOf="parent"
                app:layout_constraintEnd_toEndOf="parent"
                app:layout_constraintBottom_toTopOf="@id/profilePic3"
                android:layout_width="match_parent"
                android:layout_height="wrap_content" />
            <androidx.appcompat.widget.AppCompatImageView
                android:id="@+id/profilePic3"
                app:layout_constraintTop_toBottomOf="@id/profilePic2"
                app:layout_constraintStart_toStartOf="parent"
                app:layout_constraintEnd_toEndOf="parent"
                app:layout_constraintBottom_toBottomOf="parent"
                android:layout_width="match_parent"
                android:layout_height="wrap_content" />
        </androidx.constraintlayout.widget.ConstraintLayout>
    </ScrollView>
    <androidx.appcompat.widget.LinearLayoutCompat
        android:id="@+id/choiceContainer"
        app:layout_constraintVertical_weight="0.15"
        android:layout_width="wrap_content"
        android:layout_height="0dp"
        android:gravity="center"
        android:layout_margin="30dp"
        app:layout_constraintTop_toBottomOf="@id/profileScroll"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintBottom_toBottomOf="parent">
        <androidx.appcompat.widget.AppCompatButton
            android:id="@+id/leftButton"
            android:text=""
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintEnd_toStartOf="@id/pass"
            android:layout_marginEnd="10dp"
            android:width="140dp"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            style="@style/MyButton"
            android:background="@drawable/orange_button"/>
        <androidx.appcompat.widget.AppCompatButton
            android:id="@+id/pass"
            android:text="PASS"
            android:width="140dp"
            android:layout_marginStart="10dp"
            app:layout_constraintStart_toEndOf="@id/leftButton"
            app:layout_constraintEnd_toEndOf="parent"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            style="@style/MyButton"
            android:background="@drawable/red_button"/>
    </androidx.appcompat.widget.LinearLayoutCompat>
</androidx.constraintlayout.widget.ConstraintLayout>

Как включить URL объявления, чтобы я мог добавить его в R.id.leftButton (AppCompatButton)?

R.id.leftButton - это место, где я добавил текст UnifiedNativeAd.callToAction.

...