I am using java Prettify for syntax highlighting of java code in android app. I followed this example Android Syntax Highlighting?
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", "000000"); // black
map.put("kwd", "760098"); // violet
map.put("lit", "001ab7"); // dark blue
map.put("com", "999999"); // grey, comments
map.put("str", "ff4500"); // dark orange
map.put("pun", "333333"); // 90% black
map.put("pln", "156f15"); // dark green
return map;
}
}
and using it in the textview as
PrettifyHighlighter highlighter = new PrettifyHighlighter();
String code="public class Example { \n" +
"public static void main(String args[]) { \n" +
" for(int i=1; i<=5; i++){ \n" +
" if(i==3){ \n" +
" break; \n" +
" } \n" +
" System.out.println(i); \n" +
" } \n" +
"} \n" +
"} ";
String highlighted = highlighter.highlight("java", code);
textView.setText(Html.fromHtml(highlighted.replace("\n","<br/>"),Html.FROM_HTML_MODE_COMPACT));
xml
<TextView
android:id="@+id/textView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/ivImage3"
android:layout_alignStart="@+id/ivImage3"
android:layout_below="@+id/ivImage3"
android:scrollbars = "vertical"
android:layout_marginTop="@dimen/program_margin_top"
android:background="@drawable/textlines"
android:textSize="16dp"
android:text="" />
as you can see <= is missing from the output, it is just showing i5 instead of i<=5
What is the reason that < is not shown in the textview, how can I display < in the syntax highlighted texview? Any help is appreciated
Try this one
< for <
> for >
& for &
Hope this will help out to display the escape characters in an XML file.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.