Does anyone know of syntax highlighting libraries which work on Android? I've looked at jsyntaxpane but that doesn't seem to support Android.
-
What do yiu want to highlight? – Ahmad Aug 16 '12 at 13:01
-
please elaborate more on your question, which component do you want to display highlighting? – devmiles.com Aug 16 '12 at 13:01
-
1http://code.google.com/p/android-codepad/ – Chirag Aug 16 '12 at 13:07
-
sorry for delay, i want to highlight an edittext with html, css, javascript or php code – MindlessBarber Aug 16 '12 at 19:27
-
You can use this library https://stackoverflow.com/a/52641368/1770868 – Ahmad Aghazadeh Oct 29 '18 at 11:03
7 Answers
I managed to create a syntax highlighter for Android, based on the Prettify. It was easy, actually, when I found the Java Prettify. Just download it (sadly, it is not published for maven) and add its jar to the build path of you application.
The syntax highlighter I created based on it:
public class PrettifyHighlighter {
private static final Map<String, String> COLORS = buildColorsMap();
private static final String FONT_PATTERN = "<font color=\"#%s\">%s</font>";
private final Parser parser = new PrettifyParser();
public String highlight(String fileExtension, String sourceCode) {
StringBuilder highlighted = new StringBuilder();
List<ParseResult> results = parser.parse(fileExtension, sourceCode);
for(ParseResult result : results){
String type = result.getStyleKeys().get(0);
String content = sourceCode.substring(result.getOffset(), result.getOffset() + result.getLength());
highlighted.append(String.format(FONT_PATTERN, getColor(type), content));
}
return highlighted.toString();
}
private String getColor(String type){
return COLORS.containsKey(type) ? COLORS.get(type) : COLORS.get("pln");
}
private static Map<String, String> buildColorsMap() {
Map<String, String> map = new HashMap<>();
map.put("typ", "87cefa");
map.put("kwd", "00ff00");
map.put("lit", "ffff00");
map.put("com", "999999");
map.put("str", "ff4500");
map.put("pun", "eeeeee");
map.put("pln", "ffffff");
return map;
}
}
The colors of the syntax are hardcoded, but may be also set by i.e. application preferences. In order to display a Java source code in a TextView
, just do:
// code is a String with source code to highlight
// myTextView is a TextView component
PrettifyHighlighter highlighter = new PrettifyHighlighter();
String highlighted = highlighter.highlight("java", code);
myTextView.setText(Html.fromHtml(highlighted));
The Java Prettify library made my application around 50kB bigger.

- 20,536
- 18
- 103
- 149
-
1
-
3
-
@WojciechFrącz Tried already in argument.But it produced no effect.I want to know about your map's first argument. I read prettify documentations but it dead end for me. I found "public static final java.lang.String C_KEYWORDS" in documentations. By following this link http://java-prettify.googlecode.com/svn/trunk/javadoc/prettify/parser/Prettify.html#ALL_KEYWORDS – Tarun Sharma Oct 04 '14 at 16:52
-
-
-
-
Great work, but the formatting is very weird irregular tabs, new lines. – AmanSharma Mar 16 '21 at 17:43
Well, I did a open-source syntax-highlighting editor for Android:
https://github.com/markusfisch/ShaderEditor
It's quite simple and maybe only suitable for small data, but it's probably a good starting point.

- 359
- 2
- 2
-
2Very nice example. It definitely shows it's simplicity. EditText source: https://github.com/markusfisch/ShaderEditor/blob/master/src/de/markusfisch/android/shadereditor/ShaderEditor.java – JRomero Sep 26 '13 at 16:49
-
3Link above is broken [here's a new link](https://github.com/markusfisch/ShaderEditor/blob/5af7129d05a1d122923ae78d110a55a09597589f/app/src/main/java/de/markusfisch/android/shadereditor/widget/ShaderEditor.java) – Nicolas Nov 04 '16 at 01:10
For read-only syntax highlighting, you have two options:
Find a Java library that can syntax highlight and generate HTML using
<font color="">
(i.e., no CSS). You can then useHtml.fromHtml()
to create aSpanned
object which you can hand to aTextView
.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 theandroid-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.

- 986,068
- 189
- 2,389
- 2,491
-
thanks, I need it for an editor. Looks like i'll start looking through jsyntaxpane see if I can port it to android/edittext. – MindlessBarber Aug 16 '12 at 19:31
-
2@ng93 : Did you have any success in implementing syntax highlighter? If yes, can you explain a process of implementation? Thanks in advance. – rule Sep 19 '12 at 09:05
-
Have you found any Java library that does the `` magic described in the 1. point? – fracz Nov 03 '13 at 17:07
-
-
1If somebody know Russian, you can read my article about this topic here - http://habrahabr.ru/post/204248/ . Anyway there picture and code snippets that can give you a direction. If you want to check out result, it can be fount here - https://play.google.com/store/apps/details?id=org.kidinov.awd – Divers Dec 12 '13 at 19:56
-
@CommonsWare please help me. I just want this for read-only highlight. but I can't find a library for this. :( – MHSaffari Dec 21 '17 at 10:27
Simply use:
Setup build.gradle (project)
allprojects {
repositories {
...
maven {
url 'https://jitpack.io'
}
}
}
build.gradle (app)
dependencies {
...
compile 'com.github.ahmadaghazadeh:CodeEditor:1.0.15'
}
XML DataBinding <com.github.ahmadaghazadeh.editor.widget.CodeEditor bind:code="@{viewModel.code}" bind:lang="@{viewModel.lang}" android:layout_width="match_parent" android:layout_height="match_parent"/>
XML
<com.github.ahmadaghazadeh.editor.widget.CodeEditor
bind:code="<html></html>"
bind:lang="html"
android:layout_width="match_parent"
android:layout_height="match_parent"/>

- 16,571
- 12
- 101
- 98
I have created an API for text highlighting which can solve your problem.
https://github.com/nakshay/TextHighlighter
This API allows you to pass words and colours specific to words and will return the string which is formatted with html tags which you can send to Html.fromHtml() to get highlighted text. add below Gradle dependency to your module's gradle file.
compile 'com.github.akshay-naik:texthighlighterapi:1.1.0'

- 29
- 6
-
Links to potential solutions are always welcome, but please add some details for future visitors in case the link is no longer available. – Nikolay Mihaylov Aug 15 '16 at 08:44
-
1
-
I used it I can't find this function highlighter.setLanguage(); – Mohammad Tayyab Nov 15 '17 at 21:39
-
@MohammadTayyab, this was added in later versions, please try the latest version. – Akshay Naik May 10 '21 at 05:04
920 Text Editor (app on Play Store, source on GitHub) uses a combination of WebView and Ace, an embeddable code editor written in JavaScript.
I'm working on an app which is an IDE for Android, I think I'm going the same way.

- 3,068
- 1
- 27
- 22
-
PocketHub (app on [Play Store](https://play.google.com/store/apps/details?id=com.github.pockethub.android), source on [GitHub](https://github.com/pockethub/PocketHub)) uses [CodeMirror](http://codemirror.net/). – Antônio Medeiros Jun 04 '18 at 04:07
-
Also MGit (app on [Play Store](https://play.google.com/store/apps/details?id=com.manichord.mgit), source on [GitHub](https://github.com/maks/MGit)) uses CodeMirror. But I was not able to make it nor PocketHub work on my phone and tablet, Android 4.4.4 and Android 6.0. – Antônio Medeiros Jun 05 '18 at 03:12
-
CodeMirror has a bug on mobile browsers: [you cannot erase a newline using backspace](https://github.com/codemirror/CodeMirror/issues/3654), I did not face it using Ace on 920 Text Editor. – Antônio Medeiros Jun 05 '18 at 03:13
If you prefer you can create your own syntax highlight using the Highlight library.
The lib works through schemes, which are basically a combination of a rule (with regex) and a related modification. There are some ready-made, like ColorScheme, SyleScheme, OnClickScheme etc., but you can implement the Scheme interface and create your own.
The base class of the lib is Highlight, where spans are processed. You can use it directly, like in the example below where we highlight some key words of the Java language from TextView
//create Highlight
Highlight highlight = new Highlight();
//scheme color example
highlight.addScheme(
new ColorScheme(
Pattern.compile("\\b(private)|(public)|(protected)|(void)\\b"),
Color.parseColor("#CC7832")
)
);
//...
//textview example
highlight.setSpan(binding.toolbarTitle);
But to create a syntax highlight more is needed than that, and for that there is the HighlightTextWatcher class, optimized for large texts with processing by altered lines of the LinesTextWatcher class (which you can extend). See an example of using this class.
//create TextWatcher
HighlightTextWatcher highlightTextWatcher = new HighlightTextWatcher();
//scheme color example
highlightTextWatcher.addScheme(
new ColorScheme(
Pattern.compile("\\b(private)|(public)|(protected)|(void)\\b"),
Color.parseColor("#CC7832")
)
);
//...
//edittext example
binding.edittext.addTextChangedListener(highlightTextWatcher);
You can add as many schemes as you like and the lib does the rest.
implementation 'com.github.Irineu333:Highlight:1.0.1' (jitpack)

- 1
- 1