yandexmobile / yandex-ads-sdk-android

Other
80 stars 22 forks source link

NativeBannerView text themes are not changing to black #171

Closed kardenvan7 closed 1 year ago

kardenvan7 commented 2 years ago

Hello! I recently tried to implement native ads using NativeBannerView. But I found out that titleAppearance, domainAppearance, bodyAppearance and callToAction text colors can not be set to black and shades of grey programmatically. Changing these styles to other colors works as it should. I've tried to change these colors by using NativeTemplateAppearance but it seems to do absolutely nothing. Is this a real thing or am I simply doing something wrong?

P.S. Looks like it works incorrectly with darker shades of gray and black colors. I tried to get #FF222222 (which is dark grey) and the color of text was very very light grey. Which leads me to think that #FF000000 changes the setting but gives white text instead of black.

My code:


import android.content.Context
import android.graphics.Color
import android.graphics.fonts.FontStyle
import android.util.Log
import android.view.View
import com.yandex.mobile.ads.common.AdRequestError
import com.yandex.mobile.ads.common.ImpressionData
import com.yandex.mobile.ads.nativeads.*
import com.yandex.mobile.ads.nativeads.template.NativeBannerView
import com.yandex.mobile.ads.nativeads.template.appearance.BannerAppearance
import com.yandex.mobile.ads.nativeads.template.appearance.ButtonAppearance
import com.yandex.mobile.ads.nativeads.template.appearance.NativeTemplateAppearance
import com.yandex.mobile.ads.nativeads.template.appearance.TextAppearance
import io.flutter.plugin.platform.PlatformView

class NativeYandexAdView(
    context: Context?,
    private val id: String,
    width: Int,
    height: Int
) : PlatformView {
    private var nativeBannerView: NativeBannerView

    init {
        val viewContext = context!!

        nativeBannerView = NativeBannerView(viewContext)

        setAppearance()
        loadAd(viewContext, width, height)
    }

    private fun loadAd(context: Context, width: Int, height: Int) {
        val loader = NativeAdLoader(context)

        val parameters: HashMap<String, String> = hashMapOf(
            "preferable-height" to "$height",
            "preferable-width" to "$width"
        )

        val nativeAdRequestConfiguration = NativeAdRequestConfiguration.Builder(id)
            .setShouldLoadImagesAutomatically(true)
            .setParameters(parameters)
            .build()

        loader.setNativeAdLoadListener(NativeYandexAdEventListener())
        loader.loadAd(nativeAdRequestConfiguration)
    }

    override fun getView(): View {
        return nativeBannerView
    }

    private inner class NativeYandexAdEventListener: NativeAdLoadListener {
        override fun onAdLoaded(nativeAd: NativeAd) {
            Log.d("FlutterYandexAdPlus", "Ad $id loaded")
            bindNativeAd(nativeAd)
        }

        override fun onAdFailedToLoad(requetsError: AdRequestError) {
            Log.d("FlutterYandexAdPlus", "Ad $id failed to load: ${requetsError.description}")
        }
    }

    private fun bindNativeAd(nativeAd: NativeAd) {
        nativeAd.setNativeAdEventListener(NativeAdYandexAdsEventListener(id = id))

        nativeBannerView.setAd(nativeAd)
    }

    private fun setAppearance() {
        val nativeTemplateAppearance = NativeTemplateAppearance.Builder()
            .withBannerAppearance(
                BannerAppearance.Builder()
                    .setBorderColor(Color.parseColor("#ff00ffff"))
                    .setBackgroundColor(Color.parseColor("#ffffff00"))
                    .build()
            )
            .withTitleAppearance(
                TextAppearance.Builder()
                    .setTextColor(Color.parseColor("#ff222222"))
                    .setTextSize(12F)
                    .setFontStyle(900)
                    .setFontFamilyName(null)
                    .build()
            )
            .withBodyAppearance(
                TextAppearance.Builder()
                    .setTextColor(Color.parseColor("#ff000000"))
                    .setTextSize(12F)
                    .setFontStyle(900)
                    .setFontFamilyName(null)
                    .build()
            )
            .withDomainAppearance(
                TextAppearance.Builder()
                    .setTextColor(Color.parseColor("#ff000000"))
                    .setTextSize(12F)
                    .setFontStyle(900)
                    .setFontFamilyName(null)
                    .build()
            )
            .withAgeAppearance(
                TextAppearance.Builder()
                    .setTextColor(Color.parseColor("#ff000000"))
                    .build()
            )
            .withSponsoredAppearance(
                TextAppearance.Builder()
                    .setTextColor(Color.parseColor("#ff0000ff"))
                    .build()
            )
            .withReviewCountAppearance(
                TextAppearance.Builder()
                    .setTextColor(Color.parseColor("#ff00ff00"))
                    .build()
            )
            .withCallToActionAppearance(
                ButtonAppearance.Builder()
                    .setNormalColor(Color.parseColor("#ffff0000"))
                    .setTextAppearance(
                        TextAppearance.Builder()
                            .setTextColor(Color.parseColor("#ff000000"))
                            .setTextSize(12F)
                            .setFontStyle(900)
                            .setFontFamilyName(null)
                            .build()
                    )
                    .build()
            )
            .build()

        nativeBannerView.applyAppearance(nativeTemplateAppearance)
    }

    override fun dispose() {
        Log.d("FlutterYandexAdPlus", "Ad $id disposed")
    }
}

class NativeAdYandexAdsEventListener(private val id: String): NativeAdEventListener {

    override fun onAdClicked() {
       Log.d("FlutterYandexAd Plus", "Ad $id clicked")
    }

    override fun onLeftApplication() {
        Log.d("FlutterYandexAd Plus", "Ad $id left application")
    }

    override fun onReturnedToApplication() {
        Log.d("FlutterYandexAdPlus", "Ad $id returned to application")
    }

    override fun onImpression(impression: ImpressionData?) {
        Log.d("FlutterYandexAdPlus", "Ad $id impression: $impression")
    }

}
mobile-ads-github commented 2 years ago

Hello! We can’t reproduce this problem. In our sample, your appearance setting banner looks as expected. Which version of SDK did you use? If it’s possible, send a broken app example to mobile-ads-github@yandex-team.ru.