Reputation: 1182
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 ?
Upvotes: 0
Views: 291
Reputation: 1129
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:
Upvotes: 1