so this image basicly shows what I want to do:
I want to add static text (that orange A) on a button which already has text on it. But I don't have any idea how.
You could try Html.fromHTML(htmlString). Something like:
myTextButton.setText(Html.fromHtml("<font color='#FFA500'>A: </font> whatever text you need here"));
With resp to #blackbelt
Spannable wordtoSpan = new SpannableString("A: Text");
wordtoSpan.setSpan(new ForegroundColorSpan(Color.ORANGE), 0, 2, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
button.setText(wordtoSpan);
where button is your Button object..
where
.setSpan(what,start,end,flag);
where
what has ForegroundColorSpan or StyleSpan or UnderlineSpan object. ForegroundColorSpan take one integer argument for color and StyleSpan also take one argument for style but UnderlineSpan don’t take any argument.
start has starting point of string where would you start string index for color of style.
end has ending point of string where would you end string index for color of style.
flag can have 1 or 0.
You can also change Fontsize to part of String.. refer this link for ref
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 take all the spans of one type in a CharSequence and convert them to a different type. For example, convert all the bold spans to underline spans:
How would I do that?
(This was a problem I was facing today, and since I have solved it now, I am adding a Q&A pair here. My answer is below.)
How to change spans from one type to another
In order change the spans, you need to do the following things
Get all the spans of the desired type by using getSpans()
Find the range of each span with getSpanStart() and getSpanEnd()
Remove the original spans with removeSpan()
Add the new span type with setSpan() in the same locations as the old spans
Here is the code to do that:
Spanned boldString = Html.fromHtml("Some <b>text</b> with <b>spans</b> in it.");
// make a spannable copy so that we can change the spans (Spanned is immutable)
SpannableString spannableString = new SpannableString(boldString);
// get all the spans of type StyleSpan since bold is StyleSpan(Typeface.BOLD)
StyleSpan[] boldSpans = spannableString.getSpans(0, spannableString.length(), StyleSpan.class);
// loop through each bold span one at a time
for (StyleSpan boldSpan : boldSpans) {
// get the span range
int start = spannableString.getSpanStart(boldSpan);
int end = spannableString.getSpanEnd(boldSpan);
// remove the bold span
spannableString.removeSpan(boldSpan);
// add an underline span in the same place
UnderlineSpan underlineSpan = new UnderlineSpan();
spannableString.setSpan(underlineSpan, start, end, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
}
Notes
If you want to just clear all the old spans, then use boldString.toString() when creating the SpannableString. You would use the original boldString to get the span ranges.
See also
Is it possible to have multiple styles inside a TextView?
Looping through spans in order (explains types of spans)
Meaning of Span flags
Edit:
my app allows users to post comments for all other users to see. I want to allow users to make certain words in their comment bold or italic etc, so that when they post a comment other people will also be able to see the bold words etc.
At the moment i am able to make the words bold but when the text is saved to my remote database server it is saved as normal text and i am unable to find out which words were bold and which arn't.
How can I preserve the bold/italic/underlined... formatting when saving the text from an EditText?
There is one way to achieve this.
You can store HTML content and display HTML content in TextView.
Just look at this Answer.
Here is Sample link.
Quite easily.
Use EditText.getText to get the text, which implements Spanned interface.
Then you need to somehow mark bold parts within text, for this you may use good old HTML format, which means that you'll end up with text (storable in database) with some formatting.
Use Html.toHtml to turn Spanned to (HTML) String. Save result to DB or where you need.
Reverse process is to get your HTML String and convert it to Spanned, then set it bact to EditText. Use Html.fromHtml to accomplish this.
final SpannableStringBuilder sb = new SpannableStringBuilder("Text from the edit Text");
final StyleSpan bss = new StyleSpan(android.graphics.Typeface.BOLD); // Span to make text bold
final StyleSpan iss = new StyleSpan(android.graphics.Typeface.ITALIC);//Span to make text italic
sb.setSpan(bss, 0, 10, Spannable.SPAN_INCLUSIVE_INCLUSIVE); // make first 10 characters Bold
sb.setSpan(iss, 10, 20, Spannable.SPAN_INCLUSIVE_INCLUSIVE); // make last 10 characters Italic
//Set text or save it in db
etx.setText(sb);
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"));
I am having a notes app,in that i have to take smilies into my edittext where the cursor i point, for that i have to take list of smilies placed at top or pin to any place to emulator in edittext Activity.how come i disign UI for taking smilies(list of pngs,shown like grid view).or if any idea let me know?
task is: while typing in edit text, place the corsor to insert smilies and open any pin(button)to show smilies list and do select on one smilies and has to place in my text.
You can convert smily image into spannableText which in turn you have to set into edittext
here code hints:
public SpannableStringBuilder addSmily(Drawable dd) {
happySmileys.setBounds(0, 0, dd.getIntrinsicWidth(),
dd.getIntrinsicHeight());
SpannableStringBuilder builder = new SpannableStringBuilder();
builder.append(":-)");
builder.setSpan(new ImageSpan(dd), builder.length()
- ":-)".length(), builder.length(),
Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
return builder;
}
above function return a spannableString which u can set like this
Drawable dd=getResources().getDrawable(R.id.ursmily_icon);
edittext.settext(addSmily(dd));
enjoy with coding!
here is the reference project adding sppannable to Edittext