https://kotlinlang.org logo
#compose
Title
# compose
c

Can Korkmaz

07/21/2022, 5:42 PM
Hello, I'm having problem with using downloaded custom fonts with FontStyles. App crashes on both emulator and physical device. It also crashes when I comment out the textStyle assignation and any resource getter (colorResource), but it won't crash with a simple red Box.
Copy code
Cannot create Typeface from ResourceFont(resId=2131296260, weight=FontWeight(weight=400), style=Normal)
Fragment:
Copy code
@RequiresApi(Build.VERSION_CODES.M)
override fun onCreateView(
    inflater: LayoutInflater,
    container: ViewGroup?,
    savedInstanceState: Bundle?
): View {
    hideAppBar()
    /*_binding = FragmentLandingBinding.inflate(inflater, container, false)
    //val view = binding.root

    binding.composeViewLandingScreen.apply {
        setViewCompositionStrategy(ViewCompositionStrategy.DisposeOnLifecycleDestroyed(viewLifecycleOwner))
        setContent {
            LandingScreen(landingViewModel)
        }
    }*/
    return ComposeView(requireContext()).apply{
        setViewCompositionStrategy(ViewCompositionStrategy.DisposeOnLifecycleDestroyed(viewLifecycleOwner))
        setContent {
        LandingScreenRadioGroup()
        //LandingScreen(landingViewModel)
        }
    }
}
Composable:
Copy code
@Composable
fun LandingScreenRadioGroup(
    onRadioGroupCheckedChanged: (optionName: String) -> Unit = {},
) {
    Row(
        Modifier
            .fillMaxWidth()
            .wrapContentHeight()
            .padding(top = 4.dp)
    ) {
        val currentButton = remember { mutableStateOf(0) }
        val radioGroupOptions = listOf("TRY", "TRYB", "USDT")

        LaunchedEffect(key1 = currentButton.value) {
            onRadioGroupCheckedChanged(radioGroupOptions[currentButton.value])
        }

        for (i in radioGroupOptions.indices) {
            CustomButtonPrimary(
                text = radioGroupOptions[i],
                onClick = {
                    currentButton.value = i
                },
                height = 34,
                width = 52,
                padding = PaddingValues(end = 8.dp),/*
                buttonColors = ButtonDefaults.buttonColors(
                    backgroundColor = if (currentButton.value == i) colorResource(id = R.color.quoteAssetSelected) else colorResource(
                        id = R.color.quoteAssetDeselected
                    )
                ),*/
                contentPaddingValues = PaddingValues(),
                /*
                textStyle = CustomTextAppearances.Medium.Standard.copy(
                    color = if (currentButton.value == i) colorResource(id = R.color.colorAccent) else colorResource(
                        id = R.color.colorWhite
                    )
                )*/
            )
        }
    }
}
Copy code
val MazzardBoldFontFamily =FontFamily(listOf(Font(R.font.mazzardbold)))
val MazzardSemiBoldFontFamily =FontFamily(listOf(Font(R.font.mazzardsemibold)))


val CustomTextAppearance: TextStyle
    @Composable
    get() = TextStyle(
        color = colorResource(R.color.colorBlack),
        fontSize = CustomTextSizes.STANDARD.size,
        fontStyle = FontStyle.Normal,
        letterSpacing = 1.0.sp,
        fontFamily = FontFamily.SansSerif,
    )
val Standard: TextStyle
            @Composable
            get() = CustomTextAppearance.copy(
                fontWeight = FontWeight(400),
                fontFamily = MazzardLightFontFamily,
                fontSize = CustomTextSizes.STANDARD.size,
            )
This is the way I created custom TextStyles.
Copy code
<?xml version="1.0" encoding="utf-8"?>
<font-family xmlns:app="<http://schemas.android.com/apk/res-auto>">

    <font app:fontStyle="normal" app:fontWeight="400" app:font="@font/mazzardboldfont"/>

</font-family>
s

Siyamed

07/21/2022, 6:14 PM
It sounds like a problem with font itself. It would be better to work with a simpler example
Why do you use the font XML?
c

Can Korkmaz

07/21/2022, 6:26 PM
@Siyamed Font problem.
Copy code
return ComposeView(requireContext()).apply{
        setViewCompositionStrategy(ViewCompositionStrategy.DisposeOnLifecycleDestroyed(viewLifecycleOwner))
        setContent {
            Text("Compose text without font", style   CustomTextAppearances.Medium.Standard)
        //LandingScreenRadioGroup()
        //LandingScreen(landingViewModel)
        }
    }
}
This crashed while it runs when I remove the style parameter.
May it be caused by the way I get the font from xml instead of reaching the otf file directly(don't know how actually).
s

Siyamed

07/21/2022, 6:29 PM
You should be able to directly point to the font itself (rather than XML, where i assume R.font.fontxyz points to xml but not ttf)
🙏 1
c

Can Korkmaz

07/21/2022, 6:33 PM
Much thanks. It's working now as I changed the reference from mazzardlight(xml) to mazzarlightfont (otf file)
I'll be able to show the pages I migrated to compose at tomorrow's standup now (intern) :D
❤️ 1
s

Siyamed

07/21/2022, 6:35 PM
Ship it :)
🚀 1
22 Views