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 1:

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.

Solution 2:

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.

Solution 3:

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.

Solution 4:

Hi you can use my CodeEditor Android Arsenal Android Arsenal

Play Video

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"/>