autoSizeTextType property for wear LayoutElementBuilders.Text

104 views Asked by At

In Android wear TileService we have to use LayoutElementBuilders.Text for displaying Text. In xml we can use autoSizeTextType for TextView.

How can i apply this property in LayoutElementBuilders.Text for wear TileService ?

I didn't find any method in the Builder.

1

There are 1 answers

0
Abu Yousuf On BEST ANSWER

Here i am sharing my solution to solve my problem.

Requirement was not to change font size when user change display font size from setting.

For fixed size i have to use font size in dp in dimen file like 20dp.

In DimensionBuilders.SpProp.Builder class method setValue expect value in sp.

In LayoutElementBuilders.FontStyle.Builder class method setSize expect SpProp.

So i have to calculate a scale factor and multiply with dp value.

    fun getScaleFactorForDp(context: Context) : Float {
        return (1.0f/context.resources.configuration.fontScale)
    }


    fun getSpProp(context: Context, @DimenRes dimensionId: Int, scaleFactor : Float = 1f): DimensionBuilders.SpProp {
        val spValue = (context.resources.getDimension(dimensionId) / context.resources.displayMetrics.density) * scaleFactor
        return DimensionBuilders.SpProp.Builder()
            .setValue(spValue)
            .build()
    }

   -------------------------------------------------     
    
    val scaleFactor = getScaleFactorForDp(context)
    LayoutElementBuilders.FontStyle.Builder()
        .setSize(getSpProp(context, R.dimen.text_size,scaleFactor))
        ....
        build()