Android custom multiple textviews

Viewed 53

I want to make something like image below, multiple TextView where the view goes to next line if it doesn't fit in the last line. enter image description here

Does anybody have a suggestion how to achieve something like this?

4 Answers

afaik there is no solution in Android SDK for such behavior, but you may write this by own (measuring text, if not fitting place View in next row). its not an easy task, so I would recommend FlexBox lib

The image you are showing is a ChipGroup with chips, not textviews. If you want to implement what you are seeing there, just create a chipgroup and add as many chips as you need, they will be set automatically on the screen.

//Flex Layout for Custom layouts

implementation 'com.google.android:flexbox:2.0.0'

Use this library for this layout design.

You can create chips using XML code as well as programmatically.

var chip: Chip = Chip(this)
        chip.setText(name)
        chip.setTextColor(ContextCompat.getColor(this, R.color.colorPrimaryDark))

        val typeface =
            ResourcesCompat.getFont(
                this,
                R.font.lato_semibold
            )
        chip.setTypeface(typeface)
        chip.chipBackgroundColor = ColorStateList.valueOf(
            ContextCompat.getColor(
                this,
                R.color.color_skyblue
            )
        )

        chip.setPadding(Global.dpToPx(this, 15f), 0, Global.dpToPx(this, 15f), 0)

        chip.chipCornerRadius = 40f

        var param: LinearLayout.LayoutParams? =
            LinearLayout.LayoutParams(
                LinearLayout.LayoutParams.WRAP_CONTENT,
                LinearLayout.LayoutParams.WRAP_CONTENT
            )
        chip.layoutParams = param

        flexLayoutSkills.addView(chip)
Related