How dynamically add/delete textViews to/from linearLayout? - android

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:

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

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.

Related

can I create list of buttons for Android app using listview or do I need to use recyclerview?

I was wondering if I want to create a simple list of buttons, a recycler view will be needed or if I can make do with a listview. Thank you
The simplest method would be to create the buttons and add them the view. I strongly recommend you to not do it the following example is for demonstratiom purpose:
onCreate...
LinearLayout root = findViewBy...
for (item: dataList) {
Button btn = newn Button(this);
btn.setText(item.text);
root.addView(btn);
}
In this example Im using a linear with vertical orientation, that should be inside a scrollview.
That is bad because every view is in memory at the same time. If you have just 2 or 3 buttons then there is no problem but if the number raise to hundreds then there will be memory usage problems.
This is why ListView got deprecated, because every row was rendered. Large data set made the UI slow. Instead RecyclerView literally recyle the views as the name implies. In memory there is only the view on the screen and a bit extra, so when a view leaves the window is available to be reused by the incoming row.
By the comments I can see you are also confused with views and viewgroups. A TextView is a View it can not have another View inside. If you only need to have a click, then TextViews can use a setOnClickListener, other is the case if you need the appearance of a button. Anyway, when you create an adapter you can add any layout you want.
A list of buttons can be achieved in both the ways. But ListView is outdated. So better use RecyclerView

AppWidget - list of elements without ListView

I'm working on an AppWidget that shows a list of elements from my db. I see that there are problems (compatibility mostly) with listview in appwidget, so I am looking for alternative solutions.
My idea was to have a series of TextViews in my layout (i.e. 10 TextView) and fill them from db (the first 10 elements in example). Is it a good solution? Moreover I need to change the visibility of TextViews to hide some of them if elements are less than TextView, but I didn't find how to change visibility of views in AppWidget, is it possibile?
Alternative idea is to add only TextView I need from code, but is this possible in TextView? How can I add TextView?
Thanks.

XML dynamic creation

I want to create a XML layout dynamically but I had a question on doing so.
Say I have something like this (Looking at the layout from the "Outline" perspective):
-ScrollView
---Linear Layout(Vertical) (LL1)
-------Linear Layout(Horizontal) (LL2)
-----------Image View (IV1)
-----------Linear Layout(Vertical) (LL3)
---------------TextView
---------------TextView
So my question here is would I start with the most inner Layout (LL3) and add the 2 TextViews and then branch upwards (to LL2 then LL1 then ScrollView) with adding to the other views & layouts?
I believe you can do this several ways. I've not tried creating an entire hierarchy like this dynamically, but I've added buttons, radio-buttons, text-views and other Views this way several times. In those cases, I've just added new ones to the ones that already exist using AddView().
I think the easiest way is to just create it "top down", i.e. create the ScrollView first and add any settings, then add the other views to it downwards. I would typically do something like this:
// Call other methods to create the views first:
ScrollView myScrollView = createMainScrollView();
LinearLayout myHorizontalLayout = createLinearLayoutForAbc();
LinearLayout myOtherLayout = createLinearLayoutForXyz();
TextView myFirstTxt = (...)
(..etc..)
Now populate them in the right way:
myScrollView.addView(myHorizontalLayout);
myHorizontalLayout.addView(myOtherLayout);
(..etc..)
Note:
I do believe this should work, but I can not guarantee it; if the reference to an inner view is no longer correct after having been added to an outer view (Eg. myHorizontalLayout is no longer a valid ref to the actual view under myScrollView), you might not be able to add children to that inner view. Not sure about this, though.
(If so, you might try to fetch a new, correct reference using findViewByName() after adding each view, but I don't think that would be an optimal solution).
I would try the first way first - at least make a proof of concept, to see that you can add view in a hierarchy at least three levels deep. That should give you your answer. If it does not work, I suppose I would try adding them in the opposite order, as you suggest in your question, just to see if that works (maybe just switch the order in my second code block?).
Sorry for the imprecise answer, hope it is of some help anyway.

Multiple images in one TextView?

I would like to know what is the best way to place multiple, small(all of them the same size) images into one TextView? From what I've found, the best way would be to use Html, but how? All of my images are offline ones, so I can copy them for example in the raw folder, if that is the right way. Can anyone point me into the right direction, or show a similar thread, which I did not find? OR, is there any better approach, like don't use TextView, but something else, which can be solved in the layout files, and dynamically filled with images?
Btw, the whole thing I want to do is:
I have a ListView, filled with items
each item has different attributes, which I currently print in plaintext(I want to replace theese with images
atm, I use one separate TextView to display theese attributes
the number of attributes are random, but at least 1, and typically 3-4(so 1 picture at least, 3-4 typically)
cheers
Sounds like you are making smileys in a chat/message application, am I right? ;)
Anyway, the way to go is to use an ImageSpan. You can use a Matcher to find all text combinations you want to replace, and use a SpannableStringBuilder to add ImageSpans to the positions returned by the matcher, this will replace those characters with the image defined by the ImageSpan.
Why not just create a layout that can be used for each row of your ListView and populate the different elements of that layout based on the data for the row?
For example, if each item has a maximum of 4 attributes, add four ImageViews to the layout, and set their drawables and visibility in getView based on the position passed in.
One thing is for sure : You do not want to put your images inside your textview.
A textview can contain a background but should not be used to contain images.
What you want to do is simply design an item layout that will be used by your adapter to fill the listview.
This item layout will contain a textView that contains only your text and your images. Then in your listAdapter you'll simply show or hide the images you want.
Try to base your layout on a RelativeLayout that will allow you to have a simpler design and even overlap some elements(the images could overlap the textview for example)

android repeater-like functionality

is there any type of 'repeater' type functionality in android? I have a relative layout (inside a row in a listview) and inside that I'd like to have a series of TextViews be displayed one after the other (as if they are child rows in the listview row). The issue is that the number of those "child rows" will vary. is there any way to do this, or should i just create the TextView objects in code, and programatically add them to a linear or table layout?
The closest thing (besides ListView/ListAdapter, naturally) that I can think of offhand is ViewSwitcher and ViewSwitcher.ViewFactory, but there's really nothing magical there: it's an interface that you can call to get a view.
Since it's only one line to get a view and add it to your current hierarchy anyways, though (View.inflate(context, R.layout.somelayout, myContainerViewGroup)) it feels silly to go with something heavier, but if you feel better wrapping that up in a Factory of some sort, check the AOSP source for ViewSwitcher.
One option is TextViews support Multi-line text. So you could create the text with a StringBuilder using "\n" for new lines, and not have to worry about multiple text views.

Categories

Resources