I had the same problem. It seems the only way is to first remove the comments from the content line and then upload it to the webview.
String content = readAboveContentIntoString(); WebView webview = ...; // Add This : content = removeComment(content); webview.loadData(content, "text/html", "utf-8");
The removeComment () function removes both single-line comments and block comments.
private String removeComment(String codeString){ int pointer; int[] pos; String str = codeString; while(true) { pointer = 0; pos = new int[2]; pos[0] = str.indexOf("/*",pointer); pos[1] = str.indexOf("//",pointer); int xPos = xMin(pos); if(xPos != -1){ //========================= Pos 0 if(xPos == pos[0]){ pointer = xPos + 2; int pos2 = str.indexOf("*/", pointer); if(pos2 != -1){ str = str.substring(0,xPos) + str.substring(pos2+2,str.length()); } else{ str = str.substring(0,xPos); break; } } //========================= Pos 1 if(xPos == pos[1]){ pointer = xPos + 2; int pos2 = str.indexOf('\n', pointer); if(pos2 != -1){ str = str.substring(0,xPos) + str.substring(pos2+1,str.length()); } else{ str = str.substring(0,xPos); break; } } } else break; } return str; } private int xMin(int[] x){ int out = -1; for(int i = 0;i < x.length;i++){ if(x[i] > out)out = x[i]; } if(out == -1)return out; for(int i = 0;i < x.length;i++){ if(x[i] != -1 && x[i] < out)out = x[i]; } return out; }
source share