I am using Design TabLayout,
As setOnTabSelectedListener
is deprecated, and there is no new answer, I'll update an updated answer
Should use addOnTabSelectedListener with OnTabSelectedListener
tabLayout.addOnTabSelectedListener(object : TabLayout.OnTabSelectedListener {
override fun onTabReselected(p0: TabLayout.Tab?) {
}
override fun onTabUnselected(p0: TabLayout.Tab?) {
(p0?.customView?.findViewById(R.id.tab) as TextView).setTextColor(
context!!.resources.getColor(
R.color.v3_tint_tab_unselected_color
)
)
}
override fun onTabSelected(tab: TabLayout.Tab?) {
(tab?.customView?.findViewById(R.id.tab) as TextView).setTextColor(
context!!.resources.getColor(
R.color.v3_tint_tab_selected_color
)
(tab?.customView?.findViewById(R.id.tab_img) as ImageView).setImageResource(R.drawable.custom_image)
//for getting the posiiton if needed you can use
tab.position or tab.getPosition()
)
}
})
Custom tab xml