Android-tv Changing Text Color And Font Of Browse Fragment Rows Header
Solution 1:
I am assuming you are using the provided android.support.v17.leanback.widget.RowHeaderPresenter
as the presenter for the HeaderFragment
in your BrowseFragment
.
The RowHeaderPresenter
inflates the layout from R.layout.lb_row_header
which looks like this:
<android.support.v17.leanback.widget.RowHeaderView
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/row_header"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
style="?rowHeaderStyle" />
As you can see, this uses a style attribute called rowHeaderStyle
, which is normally pointing to @style/Widget.Leanback.Row.Header
. You can override this by putting the following in your styles.xml
:
<stylename="MyCustomRowHeaderStyle"parent="Widget.Leanback.Row.Header"><itemname="android:textColor">@color/red</item></style><stylename="MyCustomBrowseStyle"parent="Theme.Leanback.Browse"><itemname="rowHeaderStyle">@style/MyCustomRowHeaderStyle</item></style>
And then use MyCustomBrowseStyle
for the Activity
containing the BrowseFragment
by declaring it in your AndroidManifest.xml
.
Solution 2:
In addition to David's answer.
rowHeaderStyle
applies the style both to menu items in HeaderFragment
and row titles in RowFragment
(these two fragments compose your BrowseFragment
).
If you want their styles (font colors in particular) to be different, you can override BrowseFragment::onCreateHeadersFragment()
and apply specific theme at that point.
1) Add these styles to styles.xml
:
<stylename="AppTheme.Leanback.Browse.Row"parent="@style/Theme.Leanback.Browse"><itemname="rowHeaderStyle">@style/AppTheme.Leanback.Row</item></style><stylename="AppTheme.Leanback.Browse.Header"parent="@style/AppTheme.Leanback.Browse.Row"><itemname="rowHeaderStyle">@style/AppTheme.Leanback.Header</item></style><stylename="AppTheme.Leanback.Row"parent="Widget.Leanback.Row.Header"><itemname="android:textColor">@color/font_row</item></style><stylename="AppTheme.Leanback.Header"parent="Widget.Leanback.Row.Header"><itemname="android:textColor">@color/font_header</item></style>
2) Apply AppTheme.Leanback.Browse.Row
theme to your activity in manifest.
3) Apply AppTheme.Leanback.Browse.Header
theme to headers in your BrowseFragment
:
// Kotlin snippetoverridefunonCreateHeadersFragment() : HeadersFragment {
classCustomHeadersFragment : HeadersFragment() {
overridefunonCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View {
returnsuper.onCreateView(
inflater.cloneInContext(ContextThemeWrapper(inflater.context, R.style.AppTheme_Leanback_Browse_Header)),
container,
savedInstanceState
)
}
}
return CustomHeadersFragment()
}
Solution 3:
The answer by david.mihola helps with the color, however I still had problems with setting a custom font globally. For everybody who stumbles upon this question and is puzzled about this as well, here is my solution:
Thanks to the awesome work done by chrisjenx (Calligraphy), you can easily set a global (custom) font.
Simply add Calligraphy to your gradle.build and add the following snippet to your Application.onCreate()
:
CalligraphyConfig.initDefault(new CalligraphyConfig.Builder()
.setDefaultFontPath("fonts/MyCustomFont.ttf")
.setFontAttrId(R.attr.fontPath)
.build()
);
and in every Activity add the following:
@OverrideprotectedvoidattachBaseContext(Context newBase) {
super.attachBaseContext(CalligraphyContextWrapper.wrap(newBase));
}
This has done the font-trick for me on every single TextView, without modifying layouts. The documentation of Calligraphy also offers some more possibilities. Check it out.
I hope this helps other people who find this question and are trying to set (custom) fonts globally.
Post a Comment for "Android-tv Changing Text Color And Font Of Browse Fragment Rows Header"