Reputation: 640
In my app, user can add a tag in EditText. After hitting "Enter", the tag will be added to the screen. Each tag can have different lengths. User can add as many as he wants. What I want is: one TextView is added after another. When they fill one line, it will start a new row, then another row. Like below (two rows of TextViews):
"text 1","text 2","text 3","Another text"
"one","Monday"
Do I have to get the widths of the screen and every TextView and then do some calculations? Or is there a graceful way to do this?
Upvotes: 0
Views: 137
Reputation: 7936
You can use a RelativeLayout
as a container and you should add TextView
s as programmatically. While adding TextViews, you need to calculate screen width and each TextViews width. If last added TextView width exceeds the limit, you need to move that to next row.
You can get Screen Width like below:
public static int getScreenWidth(Context ctx) {
int width = 0;
try {
DisplayMetrics metrics = new DisplayMetrics();
WindowManager wm = (WindowManager) ctx
.getSystemService(Context.WINDOW_SERVICE);
wm.getDefaultDisplay().getMetrics(metrics);
width = metrics.widthPixels;
} catch (Exception e) {
e.printStackTrace();
}
return width;
}
But, there are many libraries to achieve that. You do not have to make this from scratch.
https://github.com/Cutta/TagView
https://github.com/kaedea/android-tagview
https://github.com/whilu/AndroidTagView
https://github.com/mcharmas/android-tagview
Upvotes: 1