i'm writing an application which consists some code snippets.
as the usual manner it can be displayed in TextView but i want to highlight some keywords say "main" , "int" , "class", "return" , etc.
after lots of time i figured out it can be done using html and css that loads to webview up , but it has some disadvantages like slow rendering so it makes bad user Experience .
is there a better way to approach the solution ?
it would be great to give me some code example...
thanks
Use SpannableString:
SpannableString string = new SpannableString("Your text that has to be highlighted");
string.setSpan(new BackgroundColorSpan(Color.YELLOW), 0, 10, 0);
textView.setText(string);
BackgroundColorSpan changes the background color. Also check out ForegroundColorSpan.
Or use Html.fromHtml which accepts Html tags:
String text1 = "Start your text ";
String text2 = "<font color='#EE0000'>and it is now red</font>";
textView.setText(Html.fromHtml(text1 + text2));
Html way might be a bit slower than SpannableString as it involves parsing the string.
Related
If i have the following string:
String string = “My string”
Then i could change it’s color background using SpanString:
SpannableString spannableString = new SpannableString(string);
BackgroundColorSpan backgroundSpan = new BackgroundColorSpan(Color.RED);
spannableString.setSpan(backgroundSpan, 0, spannableString.length(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
Then if i make
Toast(context , spannableString , Toast.LENGHT_SHORT).show();
It will display a toast “My string” with changed background to red.
However , how can I show the same string without using the type SpannableString??
I mean Toast(context , [toString] spannableString , Toast.LENGHT_SHORT).show();
[toString]: my note
PS:I've tried
Toast.makeText(context, Html.fromHtml(spannableString.toString()), Toast.LENGTH_SHORT).show();
and
Toast.makeText(context, spannableString.toString(), Toast.LENGTH_SHORT).show();
because i need to attribute this to a kind of:
entity.setAttribute([toString]Spannable);
but not showing commom text.
There is no way without using Spannables. Spannables are how you add formatting data to text in Android. Even your fromHTML hack turns html into a spannable.
The correct thing to do is probably take whatever API you're using that requires a string and make it take a CharSequence instead.
How can i set a particular string as a title in Edittext. I want to output to look like something like this:
Title
Some other text.
I have tried to make the text bold and give it a larger style. Something like this:
s.setSpan(new StyleSpan(Typeface.BOLD), 0, length ,Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
s.setSpan(new RelativeSizeSpan(1.2f), 0, length , Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
That does the trick but i wish if i could instead create a h1 tag around the string so the formatted html code i extract from Edittext can be displayed in textview or used elsewhere.
Update
It seems like a problem with Html class. Try something like this.
Log.d("Test", Html.toHtml(Html.fromHtml("<h1>Hello</h1>")));
You would get something like this in your logs:
<p dir="ltr"><b>Hello</b></p>
It seems like Html class is capable of converting h1 tags to spannable but not capable of being able to convert it back to h1 tags.
The fromHtml method converts the header tags to Spannable using a similar trick i already stated above. It's not implemented in the toHtml method and hence it don't create header tags from Spannable. Can someone try to fix this.
You can take a look at HTML.fromHtml(String text)
This will parse HTML and return a spanned string. The only thing notable is, that it's a bit tricky modifying the styles used for the spans.
Try something like this. Which worked for me
SpannableString text = new SpannableString(Html.fromHtml(<h1>Your content</h1>));
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);
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.