Search code examples
androidsyntaxeditorsyntax-highlighting

Android Syntax Highlighting?


Does anyone know of syntax highlighting libraries which work on Android? I've looked at jsyntaxpane but that doesn't seem to support Android.


Solution

  • For read-only syntax highlighting, you have two options:

    1. Find a Java library that can syntax highlight and generate HTML using <font color=""> (i.e., no CSS). You can then use Html.fromHtml() to create a Spanned object which you can hand to a TextView.

    2. Find a Java library that can syntax highlight and generate any sort of HTML. You can then display that in a WebView. This appears to be what the android-codepad project a commenter linked to does.

    If you are seeking syntax highlighting for an editor, that is significantly more difficult. While EditText can take the same Spanned that TextView can, you would have to run everything through the syntax highlighter to reflect changes, either on a per-keystroke basis, or after a pause in typing. Or, you would need to bake the syntax highlighting rules much more tightly to the editing process, so you can somehow incrementally adjust the highlighting without having to redo the entire contents of the EditText. I have not seen an open source syntax-highlighting editor for Android -- a few closed-source ones as apps on the Play Store, though.