Is there any way to tell linkify not to underline my links? I am already using textView.setLinkTextColor(stateList) to change the color, but I'd like to remove the underline that is inserts on the link.
Is this even possible?
TextView tv = new TextView(this);
SpannableString ss = new SpannableString(
""+getString(R.string.nonlinktext)+"\n"+getString(R.string.linkedtext)+"");
ss.setSpan(new URLSpan(getString((R.string.linkedtext))), ss.length() - numberofcharactersinlink, ss.length(),Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
I managed to get this working using this implementation. A bit of a hack but it works all the same.
Related
I do spell checking, and want to show all wrong letters in red.
Like the user typed Mossosoppo and the correct word would be Missisippi. I would want to show the wrong letters in red.
I tried with SpannedString, SpannableString and SpannableStringBuilder. I also checked all examples Google would find. Nothing worked ( = maximum 1 letter was shown in red or all letters after a position).
And i tried all possible combinations of flags -
SPAN_INCLUSIVE_INCLUSIVE, INCLUSIVE_EXCLUSIVE, EXCLUSIVE_INCLUSIVE, EXCLUSIVE_EXCLUSIVE.
This also didnt allow to use the same color in 2 places.
The maximum i could see was a combination of different properties like color, bold, italic.
The interface to set a Span is setSpan(Object _what_, int _start_, int _end_, int _flags_).
Now this doesnt tell if it is possible to call it several times for the same property (color). Did anybody succeed to add more than 1 color span to a single string ?
If so i could digg again, maybe i had an error when i tested all possible combinations of flags. At the moment i am switching code to a LinearLayout with many TextViews where each TextView contains a single character. That should work. But it isnt elegant.
Ok, thanks to Sajjad i found the solution.
It is possible to add 2 colors and more. However a ForegroundColorSpan may only be used once. So to set red 2 times you need 2 ForegroundColorSpan. If you reuse the same ForegroundColorSpan it does not work.
SpannableStringBuilder fullstring = new SpannableStringBuilder();
ForegroundColorSpan fg_red = new ForegroundColorSpan(Color.RED);
ForegroundColorSpan fg_black = new ForegroundColorSpan(Color.BLACK);
ForegroundColorSpan fg_red2 = new ForegroundColorSpan(Color.RED);
ForegroundColorSpan fg_black2 = new ForegroundColorSpan(Color.BLACK);
SpannableString blackText = new SpannableString("This is black");
blackText.setSpan(fg_black, 0, blackText.length(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
fullstring.append (blackText);
SpannableString redText = new SpannableString("red");
redText.setSpan(fg_red, 0, redText.length(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
fullstring.append(redText);
SpannableString blackText2 = new SpannableString("blackagain");
blackText2.setSpan(fg_black2, 0, blackText2.length(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
fullstring.append(blackText2);
SpannableString redText2 = new SpannableString("redagain");
redText2.setSpan(fg_red2, 0, redText2.length(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
fullstring.append(redText2);
return fullstring;
// the returned string can then be used for TextView.setText();
I would like to highlight a TextView and achieve the design shown below.
Does anyone know how to achieve this using a TextView?
I took this screenshot from an existing Android app.
By using this code I get results shown below, which is not what I want:
sp.setSpan(new BackgroundColorSpan(color), start, length, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
You need to use a Spannable String:
TextView textView = (TextView)findViewById(R.id.textView1);
String text = "Test";
Spannable spanText = Spannable.Factory.getInstance().newSpannable(text);
spanText.setSpan(new BackgroundColorSpan(0xFFFFFF00), 14, 19, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
textView.setText(spanText);
Try this. This will work.
If I understand you correctly, you want to highlight the text with a light color? If that's the case, then simply change the transparency of the color. For example: change the first two FF of 0xFFFFFF00 to something like 80(50 % transparency). So, it would look like 0x80FFFF00.
TextView textView = (TextView)findViewById(R.id.textView1);
String text = "Your String";
Spannable spanText = Spannable.Factory.getInstance().newSpannable(text);
spanText.setSpan(new BackgroundColorSpan(0x80FFFF00), 14, 19,Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
textView.setText(spanText);
Here's a relevant thread regarding hex color transparency.
The image is from an app called kakao story.
Suppose there's a post with a list of comments like any sns apps.
When you click a comment, it inserts the user name of the commenter in the edit-text to indicate my new comment is a reply to the user.
(You can't add the same name more than once.)
When you hit backspace to delete the name, the entire characters that make up the name(e.g., chabeau in the example) will be deleted by 1-backspace.
I'm trying to mimic the behavior and want some pointers how to implement it or what to search for.
If you are in search of bubble view. You can achieve it by creating a subclass of android.text.style.DynamicDrawableSpan.ImageSpan which will convert a portion of EditText string into formatted span.
This SO Question will give you some basic idea about creating formatted span.
This is a good tutorial for customizing editext with spans.
And for deleting whole word at once, you can use SPAN_EXCLUSIVE_EXCLUSIVE property.
Below code will format the first four character of the string, Hope this will give you some hint.
final SpannableStringBuilder sb = new SpannableStringBuilder("your text here");
final ForegroundColorSpan fcs
= new ForegroundColorSpan(Color.rgb(158, 158, 158));
// Span to set text color to some RGB value
sb.setSpan(fcs, 0, 4, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
yourTextView.setText(sb);
EditText et = (EditText) findViewById(R.id.edit1);
et.setTextColor(Color.parseColor("yourColorCodeHere"));
Is there any control in android which we can use as a rich text box which can handle formatting of characters such as bold, italic etc. and use fonts and color ?
If anyone having any link or source related to the above information please share it with me.
SpannableString class or HTML.fromHtml() allows you to manipulate different styles in actual string. See the links below:
http://developer.android.com/reference/android/text/SpannableString.html
http://developer.android.com/reference/android/text/Html.html#toHtml(android.text.Spanned)
Sample SpannableString and TextView implementation:
TextView tv = new TextView;
String text = "String String String String body";
SpannableString spannableStr = new SpannableString(text);
spannableStr.setSpan(new StyleSpan(Typeface.BOLD), 0 , 10, 0);
tv.setText(spannableStr);
WebView is the closest that I can think of, but it is super duper heavy unless you want the entire screen to be a webview.
I'm struggling with using EditText and Spannable text object, These days, I've read API documents around ten times, even I'm not certain that I understand correctly. So I'm looking for a kind of example which show me how to utilize EditText and Spannable.
Since you don't specify what you can't grasp from the API it's hard to answer your questions (short answer: rewrite your question to a specific questions rather than a general one).
A typical Spannable-example is something like this to turn selected text in an EditText into Italic:
Spannable str = mBodyText.getText();
if(mBodyText.getSelectionEnd() > mBodyText.getSelectionStart())
str.setSpan(new StyleSpan(android.graphics.Typeface.ITALIC),
mBodyText.getSelectionStart(), mBodyText.getSelectionEnd(),
Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
else
str.setSpan(new StyleSpan(android.graphics.Typeface.ITALIC),
mBodyText.getSelectionEnd(),
mBodyText.getSelectionStart(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
This is cut and pasted from something else, so your direct-pastability might have suffered, but it at least shows a working example of a Spannable (in this case a StyleSpan). In the API you can find the other types of Spans (notably ImageSpan, which is a common questions among newly converted droiders).
I'm just starting to try to figure it out too, and it seems unnecessarily tricky.
Here's a working method to add NEW spannable text to an existing view. I wanted to add colored text to a view, and this seemed like the only way to do it.
Though it feels like an ugly hack, you can create a dummy TextView (not shown anywhere) and style the text there, then append that styled text to wherever you want. Credit for it goes to iifuzz at anddev.org. My code looks like so:
spanbuffer = new TextView(context);
spanbuffer.setText(newText, TextView.BufferType.SPANNABLE);
Spannable s = (Spannable) spanbuffer.getText();
s.setSpan(new ForegroundColorSpan(Color.RED), 0, newText.length() - 1, 0);
this.append(s);
I think you're supposed to be able to create new spannable text using the SpannableFactory, like so:
Spannable s = Spannable.Factory.getInstance().newSpannable(newText);
but I couldn't get this text to actually show new span effects, so I gave up.