evrencoskun / TableView

TableView is a powerful Android library for displaying complex data structures and rendering tabular data composed of rows, columns and cells.
MIT License
3.13k stars 453 forks source link

Dynamic Row header #384

Open mnallamalli97 opened 3 years ago

mnallamalli97 commented 3 years ago

@evrencoskun Frustrated that I cannot have the row header width be the largest width in my list of strings. I am getting ellipses on all my row headers since one header is much longer than the static row header dimens.

Could you please show solution for row header to be width of longest item in list.

Eugeniydak commented 2 years ago

You can calculate max width of your RowHeaders:

private fun getMaxWidth(rowHeaderList: List<RowHeaderData>): Int {
    return rowHeaderList.maxOf {
        it.shortName.getTextWidth(16f, 10f)
    }
}

fun String.getTextWidth(textSize: Float, extraWidth: Float): Int {
    val paint = Paint()
    paint.textSize = textSize
    val systemScale = resources.configuration.fontScale
    val density = resources.displayMetrics.density
    return ((paint.measureText(this) + extraWidth) * systemScale * density).toInt()
}

And then set this value to TableView: updateRowHeaderWidth(getMaxWidth(rowHeaderList))

fun updateRowHeaderWidth(rowHeaderWidth: Int) {
     tableView.rowHeaderWidth = rowHeaderWidth
}