Android Syntax Highlighting

Android Syntax Highlighting?

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.

Android: EditText with syntax highlighting


  editText.addTextChangedListener(new TextWatcher() {
final String FUNCTION = "function";
@Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) {}

@Override
public void onTextChanged(CharSequence s, int start, int before, int count) {}

@Override
public void afterTextChanged(Editable s) {
int index = s.toString().indexOf(FUNCTION);
if (index >= 0) {
s.setSpan(
new ForegroundColorSpan(Color.CYAN),
index,
index + FUNCTION.length(),
Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
}
}
});

Refered from

Text in an android app with Java syntax highlighting

You might want to try this.

Android Syntax Highlighting?

Looks like it might do what you are looking for.

Syntax highlighting not working for iosApp

You'll need to use Xcode or (possibly) AppCode. Android Studio and the KMM plugin do not understand or highlight Swift code directly.

Syntax highlighting on android EditText using Span?

Since the start position for finding index always the same after each for loop, so I add a startIndex to record the changed start position, here the sample code:

public void afterTextChanged(Editable editable) {
String string = editable.toString();
String[] split = string.split("\\s");

int startIndex = 0;
for(int i = 0 ; i < split.length ; i++){
String s = split[i];
if(map.containsKey(s)){

int index = string.indexOf(s, startIndex);
int color = map.get(s);
editable.setSpan(new ForegroundColorSpan(color),
index,
index + s.length(),
Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);

startIndex = index + s.length();
}

}
}

Android Studio no syntax highlighting

Note that Android Studio does not have support for JavaScript. You may want to try IntelliJ IDEA Ultimate Edition or WebStorm if you do not need to work with Java.

How to show language specific syntax highlighted code inside activity in an android app?

You can try this library out here:

https://github.com/kbiakov/CodeView-Android



Related Topics



Leave a reply



Submit