Android anko DSL - adding textviews and make them "newline" when there are no more screen width?

244 views Asked by At

my task is simple I have an array of names, each name is added as a individual textview. However some of my arrays are long, and the textviews is too big to fit on the screen. The result is very ugle and I have no clue how to solve it. I have tried to use all different (as far as I know) layout types, and none of them seems to do the job I want. I simplye want them to jump to a new line when there is no more space left. A little like how this works: https://www.w3schools.com/css/tryit.asp?filename=trycss_float_elements

This is how it looks: enter image description here This is my current code:

for (game in games) {
    verticalLayout {
        padding = dip(5)
        //border color
        verticalLayout {
            padding = dip(3)
            backgroundColor = Color.rgb(227, 227, 227)
            //background golor
            verticalLayout {
                padding = dip(3)
                backgroundColor = Color.rgb(250, 250, 250)
                linearLayout {
                    var players = App.instance.db.getPlayerScores(game.toLong())
                    var isFirst = true
                    var colors = ArrayList(Arrays.asList(Color.BLUE, Color.RED, Color.GREEN, Color.MAGENTA, Color.YELLOW,Color.BLUE, Color.RED, Color.GREEN, Color.MAGENTA, Color.YELLOW,Color.BLUE, Color.RED, Color.GREEN, Color.MAGENTA, Color.YELLOW))
                    var ci = 0;
                    for (player in players) {
                        if (!isFirst) {
                            textView {
                                text = " vs. "
                                textSize = 18f
                            }
                        }
                        textView {
                            text = player.name
                            textSize = 24f
                            textColor = colors[ci]
                            ci += 1
                        }
                        isFirst = false
                    }
                }
                textView {
                    val date = App.instance.db.getGameTime(game)
                    text = date.substring(0, 16)
                    textSize = 18f
                    textColor = Color.GRAY
                }
            }
        }
    }
}

There should be some kind of solution to this without calculating the lenght of the content in the linearLayout and make it create a new leanaerLayout if next item will be too large to fit? If not... How could that be done?

0

There are 0 answers