2

I have to show Java code snippets into my Android APP. What I want: Save te snippet in database in plain String format. Like:

"public class Whatever {public static void main(String[] args) {new Whatever().print(3);}void print (int x){  System.out.println(x); }}"

After I import the snippet from database I have to show it on Android screen well-formated/indented, like here in StackOverflow:

public class Whatever {

    public static void main(String[] args) {
        new Whatever().print(3);
    }

    void print (int x){
        System.out.println(x);
    }    
}

Is there a way to do it in Android without create/implement my own Java Code Formatter algorithm?


EDIT

Some more information:

I need only format the code. I don't want a syntax highlighting because I need something that is not dependent of code editor. So I will retrieve the code from database and apply the format and it will run in any simply text editor.

I looked through other questions, but most of then requires to embed some external code or requires some container, like Swing (java-prettify), WebKit (android-codepad, etc). Eclipse have a code formatter, but It depends on a "TextEdit" component. But I will test it and try to extract plain text from it.

I cannot store the source code formatted with spaces/tabs, because most of time I will not touch the code.

I need that it recognize java 8 code blocks.


EDIT

Here are some other options I will try as suggest in the answeres. But most are outdated.


This is exactly what I need, but I need free :\

Community
  • 1
  • 1
alexpfx
  • 6,412
  • 12
  • 52
  • 88
  • 1
    This looks like a duplicate of [http://stackoverflow.com/questions/11987660/android-syntax-highlighting](http://stackoverflow.com/questions/11987660/android-syntax-highlighting). – Doug Stevenson Feb 09 '16 at 04:51
  • I want more a code formatter/indenter than a syntax highlighting as the question you pointed asks. – alexpfx Feb 09 '16 at 04:58
  • The doc for the library suggests that it's a "pretty printer" which normally implies that it will format the code as well. – Doug Stevenson Feb 09 '16 at 05:05
  • try this ansewer http://stackoverflow.com/a/19787125/5235032 – Jayanth Feb 09 '16 at 05:07
  • @DougStevenson It's not the same. I need a standalone solution that I pass the unformatted code and receives a String with the formated code, without an external container/editor. – alexpfx Feb 09 '16 at 13:11
  • Why not saving it well formatted (with tabs and new lines) ? – Enissay Feb 09 '16 at 20:53
  • @Enissay will not save the most of source code. – alexpfx Feb 11 '16 at 21:13

3 Answers3

1

You can check This comment. There are some discussions about the libraries which serve the need you want.

Community
  • 1
  • 1
shourav
  • 946
  • 9
  • 15
1

Use escape codes to save the code into the database. There is no API in Java that will automatically format the application code into a string and print it to the UI.

1

Yes Using Eclipse code formatter it's possible with below url have the example and api using which you are able to do this.

http://help.eclipse.org/juno/index.jsp?topic=%2Forg.eclipse.jdt.doc.isv%2Fguide%2Fjdt_api_codeformatter.htm

Rajan Bhavsar
  • 1,977
  • 11
  • 25