0

I have db with name of countries(for example). My first goal is dynamically add textView for (each country) to linearLayout (android:layout_width="match_parent" android:layout_height="match_parent"). In fact adding is not a problem, but problem if the orientation of layout is horizontal and there is to much countries, but the width of layout is not enough to show all of them and they are hide!

So i need to add textViews dynamically, but if there is not enough space for new textView - add it in the next line or create new linearLayout.

I need to create new textView for each country cos than i want to make clickListener for each of them, and there is second goal...

the second goal is delete some of that textView by clicking on it, and the other textView must relocate to that empty space. Hope that my explanation is clear :) if not i will try another one.

so, i have idea how to add textView: every time when i add new textView - count the length of all previously added ones with this one and compare it with length of linearLayout, if the length of all textViews is less - add to this linLayout, else - create new lin layout and add textView there. I think this could work, it it looks ugly :) I hope there are must be more simply and pretty solution!

Talking about dynamically deleting textViews from layout - I have no idea how to do this correctly.

So I will be glad any solutions and ideas, thanks!

EDIT here is example how I want it looks like in the end:enter image description here

14
  • Make use of ScrollView Commented Aug 29, 2015 at 7:34
  • @Sridhar, that is not solution! Commented Aug 29, 2015 at 7:36
  • 3
    What you're describing is what ListView / RecyclerView does out of the box. Use that. Commented Aug 29, 2015 at 7:37
  • @LudoBagman Why did you tell that adding scrollview is not a solution? There are many solution for your problem like using listview or something, try to solve it step by step, And also show your effort here will be better. Commented Aug 29, 2015 at 7:43
  • To echo @Barend - everything you've described can be achieved with greater simplicity and infinitely better efficiency with either a ListView or RecyclerView. There is no need to concern yourself with manually adding or deleting TextViews. In addition, TextViews should auto word-wrap anyway, so there is no need to create a separate TextView for each line. Commented Aug 29, 2015 at 7:44

2 Answers 2

2

LinearLayout is usually used for fixed childs.

For dynamic and large number of childs, you should use ListView or RecyclerView, which is exactly designed for what you described.

More than that, ListView and RecyclerView can reuse the child views, i.e. TextView for your case, which is needed, because view objects are heavy to create and keep in memory.

Edit:

Given your image and replies in comment. I would suggest you to use TextView with ClickableSpan, and set update the whole Text on click.

You can check ClickableSpan in the link below.

http://www.programcreek.com/java-api-examples/index.php?api=android.text.style.ClickableSpan

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

5 Comments

i have not enough experience with recyclerView. can you give an example how it can be provided in my case (please take a look of a pic in the question how it must look in the end) ?? Thanks
Do you need each item to be clickable? From your image, I would suggest you simply use a TextView, it is very difficult to have line wrapping with Views and may not be needed for your case.
yep, every item must be clickable, and if it was clicked - it must be deleted from layout, and all other must move aside.
updated my answer, you need to use ClickableSpan in TextView
Thanks! Spannable is new for me, pretty cool solution! but it's still not exactly what I need. I need each of item a separated View
0

You can use flowlayout for that. Link for flowlayout is https://github.com/ApmeM/android-flowlayout. Just inflate your views inside flowlayout. You don't have to do any calculation when inflating. If there is space available for textviews then they will be added horizontally else in the next line.

Comments

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.