-1

I want to create a TextView that contains programming language code. like the following picture

sample

I think that I should use html for give color and also will use string functions such as indexof,substring .. firstly, will find "for" and if i find it i will replace it with

  <h3 style="color:blue;margin-left:20px;">for</h3> 

But I am not sure if its the best way or useful.. Do you have any idea to do it on android?

PS: I am not making a compiler, just want to show piece of code

PS2: I am not asking for webview or textview.. Just want to learn how can I do it except my html case. Doesnt matter webview or textview or editview

ertan2002
  • 1,458
  • 1
  • 32
  • 68
  • You could use a **WebView** instead of a TextView. And populate its contents with some **html files** you have stored in the assets folder. Very easy, indeed. – Phantômaxx Feb 08 '14 at 13:33
  • @ArtooDetoo,thanx you for your answer. Yes I know that I should use webview for html file but I want to ask how to it with another ways? means maybe not use html code? – ertan2002 Feb 08 '14 at 13:34
  • Your **best bet** is a WebView. BUT, if you really want to use a Textview... see [this](http://stackoverflow.com/questions/2116162/how-to-display-html-in-textview) – Phantômaxx Feb 08 '14 at 13:36
  • @ArtooDetoo, I see thanx but my question is not about how to show html on texview or like that. I am looking for different way? it is not about html.It was just one idea from me.. Maybe in the picture, they used another way? I want to learn it.. – ertan2002 Feb 08 '14 at 13:38
  • Last idea, then I'm finished. Use Spananbles: see [this](http://www.chrisumbel.com/article/android_textview_rich_text_spannablestring) – Phantômaxx Feb 08 '14 at 13:41
  • You have all the answers to help your efforts in replicating the shown picture. – Phantômaxx Feb 08 '14 at 13:42
  • Thank you for your answer, it seems useful.. But How to I detected words that will be colored. For example in c#: for, private, protected, public, using, while ... I need somekind of list for making color. Actually I am looking for a library for it. Because each programming language's codes are different i mean that keywords.. You can see it in stackoverflow, codes are colored and programming languages are not important for colored.. – ertan2002 Feb 08 '14 at 13:47
  • If you want to write somewhere between a hundred and a thousand lines of code, feel free to extend `View` or `TextView` to create a view that displays syntax colored text. – zapl Feb 08 '14 at 13:47
  • 1
    This is another question! You'd use regular expressions to do a **syntax coloring**... Google around and find a zillion examples. – Phantômaxx Feb 08 '14 at 13:49
  • @ArtooDetoo, Thank you this is what I want to learn. I have just found it too.. Something like SyntaxHLDialog.. Thank you – ertan2002 Feb 08 '14 at 13:51

2 Answers2

2

Try this

public static Spannable setFourDifferentFontStyle(final Context context, String original, 
                                                 String firstWord, String color1, float size, 
                                                 String secondWord, String color2, float size2, 
                                                 String thirdWord, String color3, float size3, 
                                                 String fourthWord, String color4, float size4)

 {
  // Create a new spannable with the two strings
  Spannable spannable = new SpannableString(firstWord + secondWord + thirdWord + fourthWord);

  spannable.setSpan(new RelativeSizeSpan(size), 0, firstWord.length(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
  spannable.setSpan(new ForegroundColorSpan(Color.parseColor(color1)), 0, firstWord.length(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);

  spannable.setSpan(new RelativeSizeSpan(size2), firstWord.length(), firstWord.length() + secondWord.length(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
  spannable.setSpan(new ForegroundColorSpan(Color.parseColor(color2)), firstWord.length(),firstWord.length() + secondWord.length(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);

  spannable.setSpan(new RelativeSizeSpan(size3), firstWord.length() + secondWord.length(), firstWord.length() + secondWord.length() + thirdWord.length(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
  spannable.setSpan(new ForegroundColorSpan(Color.parseColor(color3)), firstWord.length() + secondWord.length(),firstWord.length() + secondWord.length() + thirdWord.length(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);

  spannable.setSpan(new RelativeSizeSpan(size4), firstWord.length() + secondWord.length() + thirdWord.length(), original.length(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
  spannable.setSpan(new ForegroundColorSpan(Color.parseColor(color4)), firstWord.length() + secondWord.length() + thirdWord.length(), original.length(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
}
Rajan
  • 1,069
  • 1
  • 9
  • 17
  • this way you can't have dynamic strings, unless you want to search text text for the key terms each time before wrtieing it. – Ayoub Feb 08 '14 at 14:30
0

Thank you for all attention, I've completly found a solution that I want. I've used codemirror library, and found a sample for android.

you can download the project from code google

ertan2002
  • 1,458
  • 1
  • 32
  • 68