Is there any (even if infinitesimal) memory/speed advantage if I use a DrawableLeft property of a TextLabel to show a image beside text instead of using a two objects (ImageView and TextLabel) adjusted next to next in a layout.
DrawableLeft solution would use just one object or widget, while the other one takes probaly three, as it may need a container to envelop image and text object. I would go for the one object solution, if it does job properly, as it would be more efficient both memory and speed wise.
Related
I'm fairly new to android and was wondering if in my activity I can display different text views for a certain amount of seconds before they vanish, replaced with a different text view.
Is there any way I can handle these events within an activity? I am creating a simple game and would like the Views in the activity to display after one another.
I could not find any help online (may have been searching for the wrong thing)
You can definitely use a timer variable to keep track of time and then use the setVisibility() method to set the textview visible(View.VISIBLE) or invisible(View.INVISIBLE).
For example:
TextView textView=(TextView)findViewById(R.id.text);
textView.setVisibility(View.INVISIBLE);
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.
I am wondering what is faster? A single TextView with set Spannable.Factory (which applies two styles to text) or two simple TextViews with plain text?
As this is inside of a list item (so re-rendered on scroll) it may be not so unsignificant as it may seem.
Citing Zsolt Vasvari via Google Groups:
"My guess, all else being equal, is that a View is a much "heavier"
object than a Spannable. But which one is better is impossible to say
because it requires to know what you want for and how you want to use
the text lines for. "
Mind you, this is only a guess. My feeling is that the expense you want to avoid is object creation, and that two simple Views are better than one Spannable, provided the Views are recycled.
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)
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.