android repeater-like functionality - android

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.

Related

How to convert a group of TextView to EditText in android without ViewSwitcher?

ViewSwitcher can be used only for two childs in it. But for a group of textviews to be converted to Edittexts what can be the ways?
It's easy enough to replace views for other views but converting - I don't think so.
To replace views, you can simply do something like the following:
LayoutInflater inflater = LayoutInflater.from(this);
View view = inflater.inflate(R.layout.edit_texts_layout, null);
viewSwitcher.removeAllViews();
viewSwitcher.addView(view.findViewById(R.id.first_edit_text));
viewSwitcher.addView(view.findViewById(R.id.second_edit_text));
So what's going on here?
1) You create a layout called edit_texts_layout which contains two EditText's with the ID's: first_edit_text and second_edit_text.
2) You then inflate this in your Activity or Fragment.
3) You then remove all of the existing views in your ViewSwitcher since, as you rightfully said, there can only ever be two views in a ViewSwitcher.
4) You then add the two EditText's inside that ViewSwitcher by view.findViewById()
Also...
It would be wise to have another layout called text_views_layout and do the exact same thing so that you can switch the EditText's with the TextView's.
But why have the EditText's inside a layout and not create them programmatically?
Good question. Views should live in layouts and live separately from code when possible. It makes life easy where you can work with a view in a layout since you have XML autocomplete and the preview screen so you know exactly what the view looks like - saving you from any nasty surprises later on.
Are there any other alternatives to this?
There's always more than one way to skin a cat and this situation is no different. You could possibly create TWO ViewSwitcher's and with different child views. What you'd have to do then is toggle the visibility to View.GONE and View.VISIBLE alternatively. The caveat? It'll be troublesome to maintain both in memory and to perform any operations on the child views as you'd have to remember which one is visible and which one isn't. With my original answer, you won't have to worry.

How dynamically add/delete textViews to/from linearLayout?

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.

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)

How to implement custom view with variable fields?

I have a ListView that displays a set of notes, each with varying ammounts of data (i.e. some have a due date, others don't).
Currently, each view in the list is a RelativeLayout containing a TextView for each field, plus two Button and a CheckBox. I then simply hide the unused fields by setting visible false on each one.
This has worked well, but I'm about to add a lot more data fields to the notes and inflating that many unneeded views for each row will surely kill my app. I need a more dynamic solution.
I've decided the best way to go is to create a custom view. How can I implement/design my view so that it can display a variable number of text fields without creating/destroying textviews each time (which would be quite expensive and worse than my current situation), or maintaining a large pool of hidden textviews?
You can create a class that extends LinearLayout
and use addView to dynamically place your views.
Sounds like you might want to look into a view with a stub. The stubs will save space until they are inflated, so each row will be lighter until it is used on a heftier view. If you have a relatively low number of these larger views you might save a bit of overhead.

Categories

Resources