1

I'm currently working on an android app to help my fellow classmates to learn C language. I'm pretty much done with the design and layout of the app, and have even managed to put explanation with example. But I really don't know how to display the C programs inside the app.

I've read about displaying HTML code and reading/displaying from a text file in raw folder but I don't think that'd prove helpful here. Plus I'm new to Java/Android Studio stuff so I request you all to post a bit detailed answer with little explanation.

I don't necessarily want the C code to be color highlighted or prettified, it would be enough if I could just somehow display it.

halfer
  • 19,824
  • 17
  • 99
  • 186
d02d33pak
  • 673
  • 3
  • 8
  • 22
  • http://stackoverflow.com/questions/31837840/paginating-text-in-android/32096884#32096884 – Onik Mar 08 '17 at 00:34

1 Answers1

0

An easy way to achieve this would be with storing the code in a file ( http://developer.android.com/training/basics/data-storage/files.html ) and reading from there every time the user opens the activity where the code can be seen.

Remember :

  1. The permissions :

    <manifest ...>
    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
    <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
    </manifest>
    
  2. I recommend saving the files in the internal storage. This way, when the user uninstalls the app, the files are deleted. You can do something like this to read :

    File dir = getFilesDir();
    File file = new File(dir, "File.txt");
    
    try {
        //Attaching BufferedReader to the FileInputStream by the help of InputStreamReader
        BufferedReader bufferedReader = new BufferedReader(new FileReader(new
                File(getFilesDir()+File.separator+"File.txt")));
        String inputString;
    
        s = "";
        //Reading data line by line and storing it into the stringbuffer
        while ((inputString = bufferedReader.readLine()) != null) {
    
            s+=inputString + "\n";
    
        }
    
        bufferedReader.close();
    
    } catch (IOException e) {
        e.printStackTrace();
    }
    

And this to write :

    try {
            FileOutputStream fos = openFileOutput("File.txt", Context.MODE_APPEND);

            for (int l = 0;l<=10;l++) {
                fos.write("something".getBytes());
                fos.write("\n".getBytes());
            }

            fos.close();

            //display file saved message

        } catch (Exception e) {
            e.printStackTrace();
        }
    }

See more details in the link provided. Happy coding!

Stefan Ionescu
  • 129
  • 1
  • 8
  • Well I somehow managed to do what was suggested (your answer didn't work though, I had to look for another way to achieve the same), but now I want to turn that piece of code into **a function/method** so that I can call it from different activities and achieve a text view that displays the content of a particular file. Now I know the id and file are going to change for every text view so I think I need to pass those as arguments to the function/method , right? So will someone please help me achieve that (if it's attainable). – d02d33pak Mar 22 '16 at 15:19