I wanted to show the whole c program on screen which should be visible to user.
I used textView but i am getting errors as the code contains special symbols.
for example:
android:text=" #include <stdio.h>
int main()
{
int x, y, temp;
printf("Enter the value of x and y\n");
scanf("%d%d", &x, &y);
printf("Before Swapping\nx = %d\ny = %d\n",x,y);
temp = x;
x = y;
y = temp;
printf("After Swapping\nx = %d\ny = %d\n",x,y);
return 0;
}" />
I also want that the user should be able to scroll the code as the codes may be larger than the example.I am a noob so please suggest me any alternate for the textView to display the code.
Save your C code in a file in assets folder, for example “res/assets/code.c”.
Write a function that reads the content of a file to a String:
And now define a WebView (not a TextView) in your layout (the advantages are that you can show any character, and WebView provides zoom and scroll directly):
And finally I would enclose all C code in a
<pre></pre>tag and then show it inside the WebView widget: