1
0

More than 1 year has passed since last update.

【Android】TabLayoutのIndicator色をtabごとに設定

Last updated at Posted at 2020-11-30

setSelectedTabIndicatorColorが非推奨になったので、addOnTabSelectedListenerを使用。

MainActivity.kt
        tab_layout.addOnTabSelectedListener(object : TabLayout.OnTabSelectedListener {
            override fun onTabReselected(p0: TabLayout.Tab?) {}
            override fun onTabUnselected(p0: TabLayout.Tab?) {}
            override fun onTabSelected(p0: TabLayout.Tab?) {
                tab_layout.setSelectedTabIndicatorColor(
                    ContextCompat.getColor(
                        applicationContext,
                        when (p0?.position) {
                            0 -> R.color.red
                            1 -> R.color.blue
                            else -> R.color.green
                        }
                    )
                )
            }
        })
1
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
0