-
-
Save snetts/15a78de92338a732019cd3154fec5862 to your computer and use it in GitHub Desktop.
Custom views at TabLayout with custom text style and indicator at selected tab
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class MyTabLayout : TabLayout { | |
private val mTitles: MutableList<String> = arrayListOf() | |
private var mUnselectedTypeFace: Typeface? = null | |
constructor(context: Context?) : super(context) { | |
init(context, null) | |
} | |
constructor(context: Context?, attrs: AttributeSet?) : super(context, attrs) { | |
init(context, attrs) | |
} | |
constructor(context: Context?, attrs: AttributeSet?, defStyleAttr: Int) : super(context, attrs, defStyleAttr) { | |
init(context, attrs) | |
} | |
private fun init(context: Context?, attrs: AttributeSet?) { | |
addOnTabSelectedListener() | |
} | |
private fun addOnTabSelectedListener() { | |
addOnTabSelectedListener(object : TabLayout.OnTabSelectedListener { | |
override fun onTabReselected(tab: Tab?) { | |
} | |
override fun onTabUnselected(tab: Tab?) { | |
val view = tab?.customView | |
if (view is AppCompatTextView) { | |
view.typeface = mUnselectedTypeFace | |
} | |
} | |
override fun onTabSelected(tab: Tab?) { | |
val view = tab?.customView | |
if (view is AppCompatTextView) { | |
view.setTypeface(view.typeface, Typeface.BOLD) | |
} | |
} | |
}) | |
} | |
override fun setupWithViewPager(viewPager: ViewPager?, autoRefresh: Boolean) { | |
super.setupWithViewPager(viewPager, autoRefresh) | |
addViews() | |
} | |
private fun addViews() { | |
for (i in 0 until tabCount) { | |
val tab = getTabAt(i) | |
if (tab != null) { | |
val customFontTextView = createCustomFontTextViewForTab() | |
if (i == 0) { | |
if (mUnselectedTypeFace == null) { | |
mUnselectedTypeFace = customFontTextView.typeface | |
} | |
customFontTextView.setTypeface(customFontTextView.typeface, Typeface.BOLD) | |
} | |
tab.customView = customFontTextView | |
} | |
} | |
} | |
private fun createCustomFontTextViewForTab(): AppCompatTextView { | |
val customFontTextView = AppCompatTextView(context) | |
customFontTextView.gravity = Gravity.CENTER | |
TextViewCompat.setTextAppearance(customFontTextView, R.style.MyTitleStyle) | |
return customFontTextView | |
} | |
fun setTitlesAtTabs(titles: List<String>?) { | |
if (titles == null || titles.size < tabCount) { | |
return | |
} | |
if (this.mTitles.size > 0) { | |
this.mTitles.clear() | |
} | |
this.mTitles.addAll(titles) | |
for (i in 0 until tabCount) { | |
val tab = getTabAt(i) | |
if (tab != null) { | |
val view = tab.customView | |
if (view is AppCompatTextView) { | |
view.text = mTitles[i] | |
} | |
} | |
} | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<!--Style for custom tab view--> | |
<style name="MyTitleStyle" parent="android:Widget.TextView"> | |
<item name="android:layout_width">wrap_content</item> | |
<item name="android:layout_height">wrap_content</item> | |
<item name="android:gravity">center_vertical</item> | |
<item name="android:textSize">20sp</item> | |
<item name="android:textColor">@color/primaryTextColor87</item> | |
</style> | |
<!--Style for TabLayout --> | |
<style name="MyTabLayoutStyle" parent="Widget.Design.TabLayout"> | |
<item name="tabIndicatorColor">#E97E78</item> | |
</style> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<solution.digital.creative.com.passwordmanager.app.article_tab_layout.MyTabLayout | |
android:id="@+id/tab_layout" | |
style="@style/MyTabLayoutStyle" | |
android:layout_width="match_parent" | |
android:layout_height="?attr/actionBarSize" | |
android:background="@color/colorWhite" /> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment