I have a TextView and I want to show multi-colored text in that. I am using html.fromHtml for this, but it is not showing any color.
My code is:
//statusToShow is a String
Log.d("html ==>", statusToShow) ;
RStatus.setText(Html.fromHtml(statusToShow),
TextView.BufferType.SPANNABLE);
Value from Log is:
<span style='background-color:#80B5FF;color:#fff'>C</span> <span style='background-color:#CF8DEA;color:#fff'>P</span> <span style='background-color:red;color:#fff'>E</span> <span style='background-color:green;color:#fff'>D</span>
Not all tags are supported, list of supported tags is here: http://commonsware.com/blog/Android/2010/05/26/html-tags-supported-by-textview.html
Try using Spannable like this:
Spannable WordtoSpan = new SpannableString("Text To Span");
WordtoSpan.setSpan(new BackgroundColorSpan(Color.BLUE), 0, 4,
Spannable.SPAN_INCLUSIVE_INCLUSIVE);
This will make the background color of Text Blue.
Hope this helps.
Related
I am creating a Gallery application. Each image will show the title along with the name of the person who uploaded the image. Example of what I want is below:
Here the Night is dark, but this darkness has the silence is the title for the image, and the shaggy boy is the user who uploaded the image for example.
So, now I want Night is dark, but this darkness has the silence to be in the separate textview and the shaggy boy at the end of the Night is dark, but this darkness has the silence's textview even if Night is dark, but this darkness has the silence is a single line or multi-line.
I have used the Flexbox Layout but I am failed to achieve what I want.
How to append the textview to the end of another textview? Thanks!!!
You can use a single TextView and set its text String with a Spannable and match each portion of text with indices here I am separating both portions by a hyphen; but you can change that as you would like.
TextView textView = findViewById(...);
String tag = "Night is dark, but this darkness has the silence - shaggy boy";
SpannableString spannableString = new SpannableString(tag);
spannableString.setSpan(new ForegroundColorSpan(Color.BLACK), 0, tag.indexOf("-"), 0);
spannableString.setSpan(new ForegroundColorSpan(Color.GRAY), tag.indexOf("-"), tag.length(), 0);
textView.setText(spannableString);
Maybe you could just get the text of the author's TextView and add it to the String of the first TextView?
You can use a single TextView with String palceholders of the quote and the author, and add the colors using HTML tags.
strings.xml
<resources>
<string name="tag">
<![CDATA[
<font color="#00000">%1$s</font> -
<font color="#D3D3D3">%2$s</font>
]]>
</string>
</resources>
Behavior:
String message = "Night is dark, but this darkness has the silence";
String name = "shaggy boy";
String formattedTag = String.format(getString(R.string.tag), message, name);
textview.setText(Html.fromHtml(formattedTag));
I am using SpannableString to insert emotion icons into EditText.
The following four cases are all OK:
(1) append text or emotion icon at the end of EditText,
(2) insert emotion icon between text,
(3) insert emotion icon between existed emotion icons.
(4) insert text between existed text.
But the problem is I cannot insert text between those icons. Here is my code and logcat info.
(1) Add icon to EditText:
Drawable d = UiUtil.getCachedDrawable(mActivity, EmResource.findResIdByTag(value));
if (d != null) {
d.setBounds(0, 0, w, w);
String str = "<img src='" + value + "'/>";
SpannableString ss = new SpannableString(str);
ImageSpan span = new ImageSpan(d, ImageSpan.ALIGN_BOTTOM);
ss.setSpan(span, 0, str.length(), Spannable.SPAN_INCLUSIVE_EXCLUSIVE);
contentEt.getText().replace(contentEt.getSelectionStart(), contentEt.getSelectionEnd(), ss);
}
(2) After inputing some icons, it looks like this (I have moved the cursor manualy after typing):
I have added a TextWatcher for this EditText. Log info from TextWatcher:
<img src='115'/><img src='116'/><img src='117'/><img src='118'/>
(3) Then I press 'y' on the keyboard (in the position of previous screen shot). After that the cursor is moving but the text isn't shown. Like the following screen shot:
But the log info shows that the text is inserted to EditText successfully.
<img src='115'/><img src='116'/>y<img src='117'/><img src='118'/>
<img src='115'/><img src='116'/>yy<img src='117'/><img src='118'/>
<img src='115'/><img src='116'/>yyy<img src='117'/><img src='118'/>
Any suggestions? Thanks a lot.
Change Spannable.SPAN_INCLUSIVE_EXCLUSIVE to Spannable.SPAN_EXCLUSIVE_EXCLUSIVE
Hope it helps.
As SpannableString content is immutable, it might not allow you to edit between two emojis, Instead of that try with SpannableStringBuilder whose content and markup can both be changed. Hope it will help
I want to achieve this functionality. Please provide a suggestion for it.
Try this, this adds a drawable in a TextView after text.
TextView text=(TextView)findViewById(R.id.textView);
SpannableStringBuilder builder = new SpannableStringBuilder();
builder.append("This is text with image!").append(" ");
Drawable myIcon = getResources().getDrawable(R.drawable.ic_new);
myIcon.setBounds(0, 0, test.getLineHeight(),test.getLineHeight());
builder.setSpan(new ImageSpan(myIcon, ImageSpan.ALIGN_BASELINE),builder.length() - 1, builder.length(), 0);
text.setText(builder);
I think you are looking for the Spannable interface, which can allow you to add images to a TextView.
If you want to know more about its usage, please read this post. Its a really good read.
I am developing eBook Reader.Kindly suggest me how can i hightlight particular sentence of my string in android Like iBook In IOS ,MoonReader in android & many more.
There is any library for it.Kindly suggest me any example or tutorial .
try this code hope this help you
textView.setText("This Is Android", TextView.BufferType.SPANNABLE);
Spannable WordtoSpan = (Spannable) textView.getText();
WordtoSpan.setSpan(new BackgroundColorSpan(FF0000), 6, 15, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
textView.setText(WordtoSpan);
It will highlight the text from character 6 to 15
This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
Is it possible to have multiple styles inside a TextView?
I want my TextView to show some part of its text in red and others in black. It's content (the text) is created dynamically and i don't know how many words will be red colored.
Is there any way to do this like in html-css?
You can use Spannable to achieve what you want.
String text = "This is <font color='red'>red</font>. This is <font color='blue'>blue</font>.";
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
textView.setText(Html.fromHtml(text, Html.FROM_HTML_MODE_LEGACY), TextView.BufferType.SPANNABLE);
} else {
textView.setText(Html.fromHtml(text), TextView.BufferType.SPANNABLE);
}
try this way
TextView tv = (TextView)findViewById(R.id.tv);
Spannable wordtoSpan = new SpannableString("I know just how to whisper, And I know just how to cry,I know just where to find the answers");
wordtoSpan.setSpan(new ForegroundColorSpan(Color.BLUE), 15, 30, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
wordtoSpan.setSpan(new ForegroundColorSpan(Color.RED), 5, 10, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
tv.setText(wordtoSpan);
What you basically want is SpannableString
See this for the complete example: