material-components / material-components-android

Modular and customizable Material Design UI components for Android
Apache License 2.0
16.25k stars 3.05k forks source link

[TabLayout] tabs font is reset to Roboto when attaching new TabLayoutMediator #2938

Open jeffdgr8 opened 2 years ago

jeffdgr8 commented 2 years ago

Description: Similar to #965, except not while switching tabs, but resetting tabs with new data after view creation. Using a StateFlow in the Fragment's ViewModel, which provides data for tabs, when the StateFlow receives a new value, attaching a new TabLayoutMediator causes the tabs to refresh with the Roboto font instead of the app theme's font. When the tabs are loaded for the first time on view creation, the tabs' font comes from the app theme's fontFamily attribute.

Expected behavior: The font should be the same theme font, both on the initial tab load and any subsequent refreshes.

Source code:

override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
    viewLifecycleOwner.lifecycleScope.launch {
        viewModel.info.collect { info ->
            with(binding) {
                tabMediator?.detach()
                pager.adapter = InfoAdapter(info)
                tabMediator = TabLayoutMediator(tabs, pager, InfoTabConfigurationStrategy(info)).apply {
                    attach()
                }
            }
        }
    }
}

Minimal sample app repro: Run the app and wait 10 seconds for the tab data refresh. My Application.zip

Android API version: Android 13 (API 33)

Material Library version: 1.6.1 (also 1.7.0-beta01)

Device: Pixel 6 Pro

jonakuitse-ct commented 1 year ago

I can also reproduce the issue, and it is worse on 1.8.0 as the tabs reset to Roboto on click

bohdanfk21 commented 1 year ago

@jeffdgr8 you can try set font manually to the tab layout. It helped me. https://briskandroid.medium.com/how-to-change-fonts-in-material-tab-layouts-tabs-ea777c7bd1af