I want to make the text of a selected tab bold. How can I do this either through xml or java code, whatever is easier.
This is a Kotlin code for this solution
for (i in 0..tabLayout.tabCount){
val tab:TabLayout.Tab? = tabLayout.getTabAt(i)
if (tab != null){
val tabTextView:TextView = TextView(this)
tab.customView = tabTextView
tabTextView.layoutParams.width = ViewGroup.LayoutParams.WRAP_CONTENT
tabTextView.layoutParams.height = ViewGroup.LayoutParams.WRAP_CONTENT
tabTextView.text = tab.text
if (i == 0){
// This set the font style of the first tab
tabTextView.setTypeface(null,BOLD)
}
if (i == 1){
// This set the font style of the first tab
tabTextView.setTypeface(null,NORMAL)
}
}
}
tabLayout!!.addOnTabSelectedListener(object: TabLayout.OnTabSelectedListener {
override fun onTabSelected(tab: TabLayout.Tab?) {
viewPager.currentItem = tab!!.position
val text:TextView = tab.customView as TextView
text.setTypeface(null,BOLD)
}
override fun onTabUnselected(tab: TabLayout.Tab?) {
val text:TextView = tab?.customView as TextView
text.setTypeface(null,NORMAL)
}
override fun onTabReselected(tab: TabLayout.Tab?) {
}
})