What is the equivalent of "android:fontFamily="sans-serif-light" in Java code?

It should be possible with setTypeface() and Typeface.create():

convertView.setTypeface(Typeface.create("sans-serif-light", Typeface.NORMAL));

See Docs:

Create a typeface object given a family name, and option style information. If null is passed for the name, then the "default" font will be chosen. The resulting typeface object can be queried (getStyle()) to discover what its "real" style characteristics are.

Note that excessively using Typeface.create() is bad for your memory, as stated in this comment. The suggested Hashtable is a good solution, but you have to modify it a little since you don't create your typeface from an asset.


Android 4.1 (API Level 16) and Support Library 26 and higher

If you are using res -> font folder, you can use like this

  val typeface = ResourcesCompat.getFont(Context, R.font.YOUR_FONT)
  TextView.setTypeface(typeface)

In my opinion there is still a way to apply system fonts programatically on TextView without having any memory issue and that is using textview.setTextAppearance method :

<style name="styleA">
    <item name="android:fontFamily">sans-serif</item>
    <item name="android:textStyle">bold</item>
    <item name="android:textColor">?android:attr/textColorPrimary</item>
</style>
<style name="styleB">
    <item name="android:fontFamily">sans-serif-light</item>
    <item name="android:textStyle">normal</item>
    <item name="android:textColor">?android:attr/textColorTertiary</item>
</style>


if(condition){
    textView.setTextAppearance(context,R.style.styleA);
}else{
    textView.setTextAppearance(context,R.style.styleB);
}