1

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

Shahin Ghasemi
  • 1,600
  • 1
  • 20
  • 38
  • "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" -- I have not had a problem displaying formatted source code in a `WebView`. – CommonsWare Aug 05 '17 at 17:25
  • are you sure ? just test it with a native widget like TextView . say your ParentLayout Consists of two widget , TextView and WebView Loads both up with some Text and compare rendering time ... in addition sometimes webview gets blank!! i don't know why though – Shahin Ghasemi Aug 05 '17 at 18:59
  • "are you sure ?" -- considering that I have been publishing [my book](https://commonsware.com/Android) for several years as an APK, and that I use formatted source code in my chapters, and those chapters are displayed in `WebView` widgets, yes, I am quite certain that it works. – CommonsWare Aug 05 '17 at 19:00
  • can you give me some pics of your Formatted source code into your app ? i just wanted to consider the slow performance that has been issue for many developers like me...@CommonsWare – Shahin Ghasemi Aug 05 '17 at 19:08

1 Answers1

0

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.

Bob
  • 13,447
  • 7
  • 35
  • 45
  • what if my Code snippet consists of 100 line ? is there a way to do template automatically ? – Shahin Ghasemi Aug 05 '17 at 17:10
  • You have to give the start index and the end index of the text that has to be highlighted. – Bob Aug 05 '17 at 17:11
  • yes , but in my case i have roughly 100 code snippet , and this is some tedious working to set by hand , i wish how could this done automatically ? just like css tags – Shahin Ghasemi Aug 05 '17 at 17:13
  • check this for more info on that: http://www.grokkingandroid.com/android-quick-tip-formatting-text-with-html-fromhtml/ – Bob Aug 05 '17 at 17:16
  • fromHtml is deprecated , what is difference between both ? although i think the first one is a better choice in comparison – Shahin Ghasemi Aug 05 '17 at 17:19
  • yes. its deprecated in Nougat. you have to do version check and use new method from N. like this: https://stackoverflow.com/a/37905107/4586742 – Bob Aug 05 '17 at 17:21
  • In the first method, you give the index directly to color the text. So it is faster than Html, where the system has to parse the text to find the Html tags. But you will run into trouble in the first method if you localize your strings to other languages, where the order of the text might change in other languages but the index will be static. In that case, using Html is the preferred one. – Bob Aug 05 '17 at 17:23