how to enable syntax highlighting for c code in a textview on android studio?

503 Views Asked by At

I created an application for students to learn software with examples. I have published it in play store. I'm continuing developemnt on this application. I want show syntax highlighting in all c codes but I haven't found a way to do it . I tried a lot of way to add syntax highlighting but It is not possible.

1

There are 1 best solutions below

2
Ismail Iqbal On

If I got your requirements right, correct me if I'm wrong. You need to show syntax highlighting for code snippets (c language) on a text view, which your already exiting play store app only shows in black and white.

To get the highlighted html of the code snippet you could use this site and then you could get the html of the highlighted code snippet from the HTML Code section of the above linked site. After which you have two options to show it on the android application.

  1. Using the TextView

    Using java you could populate the text view with the following util method

    myTextView.setTextsetText(Html.fromHtml("<pre style='color:#000000;background:#ffffff;'><span style='color:#004a43; '>#</span><span style='color:#004a43; '>include </span><span style='color:#800000; '>&lt;</span><span style='color:#40015a; '>stdio.h</span><span style='color:#800000; '>></span>\n" +
            "<span style='color:#800000; font-weight:bold; '>int</span> <span style='color:#400000; '>main</span><span style='color:#808030; '>(</span><span style='color:#808030; '>)</span>\n" +
            "<span style='color:#800080; '>{</span>\n" +
            "   <span style='color:#696969; '>// printf() displays the string inside quotation</span>\n" +
            "   <span style='color:#603000; '>printf</span><span style='color:#808030; '>(</span><span style='color:#800000; '>\"</span><span style='color:#0000e6; '>Hello, World!</span><span style='color:#800000; '>\"</span><span style='color:#808030; '>)</span><span style='color:#800080; '>;</span>\n" +
            "   <span style='color:#800000; font-weight:bold; '>return</span> <span style='color:#008c00; '>0</span><span style='color:#800080; '>;</span>\n" +
            "<span style='color:#800080; '>}</span>\n" +
            "</pre>"));
    
  2. Using WebViews Once again using java you could populate a webView with the html code you obtained from the above mentioned site.

    WebView webView  = (WebView) findViewById(R.id.webView);
    webView.loadData("<pre style=\"color:#000000;background:#ffffff;\"><span style=\"color:#004a43; \">#</span><span style=\"color:#004a43; \">include </span><span style=\"color:#800000; \">&lt;</span><span style=\"color:#40015a; \">stdio.h</span><span style=\"color:#800000; \">&gt;</span>\n" +
            "<span style=\"color:#800000; font-weight:bold; \">int</span> <span style=\"color:#400000; \">main</span><span style=\"color:#808030; \">(</span><span style=\"color:#808030; \">)</span>\n" +
            "<span style=\"color:#800080; \">{</span>\n" +
            "   <span style=\"color:#696969; \">// printf() displays the string inside quotation</span>\n" +
            "   <span style=\"color:#603000; \">printf</span><span style=\"color:#808030; \">(</span><span style=\"color:#800000; \">\"</span><span style=\"color:#0000e6; \">Hello, World!</span><span style=\"color:#800000; \">\"</span><span style=\"color:#808030; \">)</span><span style=\"color:#800080; \">;</span>\n" +
            "   <span style=\"color:#800000; font-weight:bold; \">return</span> <span style=\"color:#008c00; \">0</span><span style=\"color:#800080; \">;</span>\n" +
            "<span style=\"color:#800080; \">}</span>\n" +
            "</pre>","text/html", "UTF-8");