I've searched around on Google and came across this site where I found a question similar to mine in which how to include a image in a TextView
text, for example "hello my name is [image]", and the answer was this:
ImageSpan is = new ImageSpan(context, resId);
text.setSpan(is, index, index + strLength, 0);
I would like to know in this code,
- What am I supposed to type or do in the context?
- Am I supposed to do something to the
text.setSpan()
like import or reference or leave it text?
If someone can break this down for me that would be much appreciated.
Try this ..
Also see this.. http://developer.android.com/reference/android/widget/TextView.html
Try this in xml file
This is partly based on this earlier answer above by @A Boschman. In that solution, I found that the input size of the image greatly affected the ability of
makeImageSpan()
to properly center-align the image. Additionally, I found that the solution affected text spacing by creating unnecessary line spacing.I found BaseImageSpan (from Facebook's Fresco library) to do the job particularly well:
Then supply your betterImageSpan instance to
spannable.setSpan()
as usualI tried many different solutions and this for me was the best:
This code uses a minimum of memory.
com/xyz/customandroid/ TextViewWithImages .java:
Use:
in res/layout/mylayout.xml:
Note that if you place TextViewWithImages.java in some location other than com/xyz/customandroid/, you also must change the package name,
com.xyz.customandroid
above.in res/values/strings.xml:
where ok16.png and retry16.png are icons in the res/drawable/ folder
This answer is based on this excellent answer by 18446744073709551615. Their solution, though helpful, does not size the image icon with the surrounding text. It also doesn't set the icon colour to that of the surrounding text.
The solution below takes a white, square icon and makes it fit the size and colour of the surrounding text.
How to use:
Simply embed references to the desired icons in the text. It doesn't matter whether the text is set programatically through
textView.setText(R.string.string_resource);
or if it's set in xml.To embed a drawable icon named example.png, include the following string in the text:
[img src=example/]
.For example, a string resource might look like this: