Android Syntax Highlighting?
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.
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
Remove Listview Items in Android
How to Request Permissions from a Service in Android Marshmallow
Locationsettingsrequest Dialog to Enable Gps - Onactivityresult() Skipped
How to Display Album Art Using Mediastore.Audio.Albums.Album_Art
Detecting Whether a Headset Is Plugged into an Android Device or Not
Adt Will Not Allow Creation of Android Activity
Java.Lang.Classnotfoundexception on Working App
About "_Id" Field in Android SQLite
Cannot Load Library: Reloc_Library[1285]: Cannot Locate 'Rand'
Blue Dot and Circle Is Not Shown on Mylocation Using Android Fused Location API
How to Make an App's Background Image Repeat
Android: Binding Data from a Database to a Checkbox in a Listview
Eclipse Error "Adb Server Didn't Ack, Failed to Start Daemon"
Pop Up Window Over Android Native Incoming Call Screen Like True Caller Android App
Why Does the Google Play Store Say My Android App Is Incompatible with My Own Device