googleads-mobile-android-native-templates
googleads-mobile-android-native-templates copied to clipboard
Can't set primary and secondary texts' typeface color
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)
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