0

In Java, building an Android app, I need to create one or more custom views and insert them in a Layout.

Each view can have different width. The heigh is always the same. I would like to place each view side by side and if the right border of the screen is reached, the next one must go to a new line.

In fact, imagine that each view represents a word, all the words are a paragraph. So all my custom view must be placed like the words, side by side and line by line.

Is there a way to do this easily with any existing Android object ?

2
  • Did you try using ChipGroup and Chip? Commented Aug 5, 2020 at 9:54
  • No, I didn't know that component. Apparently it allows to use multi lines, but is it possible to create severals views on one line if there is enough room, and use multiples lines too ? If yes, maybe that component can help me. Commented Aug 5, 2020 at 10:02

1 Answer 1

1

You can try ChipGroup and Chip. I think it will meet your requirements. At first add ChipGroup in your view xml like this:

<com.google.android.material.chip.ChipGroup
    android:id="@+id/cgWords"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"/>

Then, to dynamically add Chip in this, sample code can be as following:

private void showTags(List<String> words) {
    for (String word : words) {
        Chip chip = new Chip(context);
        chip.setText(word);
        ChipGroup cgWords = findViewById(R.id.cgWords);
        cgWords.addView(chip);
    }
}

This is a screenshot after running a sample code:

enter image description here

Sign up to request clarification or add additional context in comments.

1 Comment

Thx, it's exactly what I'm looking for !

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.