googleads / googleads-mobile-android-native-templates

Apache License 2.0
116 stars 102 forks source link

Can't set primary and secondary texts' typeface color #11

Open IAmDarush opened 4 years ago

IAmDarush commented 4 years ago

I'm using the following method to set the typeface color. However, withPrimaryTextTypefaceColor() and withSecondaryTextTypefaceColor() don't seem to be working.

val styles = NativeTemplateStyle.Builder()
          .withMainBackgroundColor(ColorDrawable(resources.getColor(R.color.colorWhite)))
          .withPrimaryTextSize(16f)
          .withPrimaryTextTypefaceColor(resources.getColor(R.color.colorRed))
          .withPrimaryTextTypeface(ResourcesCompat.getFont(requireContext(), R.font.lato_regular))
          .withSecondaryTextSize(14f)
          .withSecondaryTextTypefaceColor(resources.getColor(R.color.colorGreen))
          .withSecondaryTextTypeface(ResourcesCompat.getFont(requireContext(), R.font.lato_regular))
          .withTertiaryTextTypefaceColor(resources.getColor(R.color.colorBlack))
          .withCallToActionTextSize(14f)
          .withCallToActionTextTypeface(ResourcesCompat.getFont(requireContext(), R.font.lato_bold))
          .withCallToActionTypefaceColor(resources.getColor(R.color.colorWhite))
          .withCallToActionBackgroundColor(ColorDrawable(resources.getColor(R.color.colorAdCallToAction)))
          .build()

        binding.bannerAd.setStyles(styles)
marchbold commented 4 years ago

There is a bug in the TemplateView when checking colours.

if (primaryTypefaceColor > 0 && primaryView != null) {
  primaryView.setTextColor(primaryTypefaceColor);
}

As colours are normally ARGB values, the first bit is normally set which gets treated as a negative value and fails this test.

Changing all these tests to be != 0 seemed to work for me.

int primaryTypefaceColor = styles.getPrimaryTextTypefaceColor();
if (primaryTypefaceColor != 0 && primaryView != null)
{
    primaryView.setTextColor( primaryTypefaceColor );
}

int secondaryTypefaceColor = styles.getSecondaryTextTypefaceColor();
if (secondaryTypefaceColor != 0 && secondaryView != null)
{
    secondaryView.setTextColor( secondaryTypefaceColor );
}

int tertiaryTypefaceColor = styles.getTertiaryTextTypefaceColor();
if (tertiaryTypefaceColor != 0 && tertiaryView != null)
{
    tertiaryView.setTextColor( tertiaryTypefaceColor );
}

int ctaTypefaceColor = styles.getCallToActionTypefaceColor();
if (ctaTypefaceColor != 0 && callToActionView != null)
{
    callToActionView.setTextColor( ctaTypefaceColor );
}

https://github.com/googleads/googleads-mobile-android-native-templates/blob/master/nativetemplates/src/main/java/com/google/android/ads/nativetemplates/TemplateView.java#L120-L143