Friday, July 22, 2022

C-Programming-Android-App















MainActivity.java


import androidx.appcompat.app.AppCompatActivity;

import android.content.res.AssetManager;
import android.graphics.Color;
import android.graphics.Typeface;
import android.os.Bundle;
import android.text.Spannable;
import android.text.SpannableString;
import android.text.Spanned;
import android.text.TextUtils;
import android.text.method.ScrollingMovementMethod;
import android.text.style.ForegroundColorSpan;
import android.util.TypedValue;
import android.widget.TextView;
import java.io.IOException;
import java.io.InputStream;

public class MainActivity extends AppCompatActivity {
TextView tv;
Typeface face;
Spannable raw;
TextView tvOutput;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
tv = (TextView) findViewById(R.id.tvDisplay);
face = Typeface.createFromAsset(getAssets(), "Fonts/Consolas.ttf");
tv.setMovementMethod(new ScrollingMovementMethod());
tv.setTextSize(TypedValue.COMPLEX_UNIT_SP, getResources().getDimension(R.dimen.textsize3));
tv.setTypeface(face);
openFile();
syntaxHighlight();

tvOutput = (TextView) findViewById(R.id.tvOutputDisplay);
tvOutput.setMovementMethod(new ScrollingMovementMethod());
openOutput();
tvOutput.setTextSize(TypedValue.COMPLEX_UNIT_SP, getResources().getDimension(R.dimen.textsize3));
}


private void openFile() {
// Using AssetManager and InputStream to open file selected by the user.
Bundle extras = getIntent().getExtras();
// temp = extras.getString("KEY");
//loc = extras.getString("LOC");
final AssetManager am = getAssets();
try {
InputStream is = am.open("Programs/" + "Hello World.c");
int size = is.available();
byte[] buffer = new byte[size];
is.read(buffer);
is.close();
String text = new String(buffer);
tv.setText(text);
} catch (IOException e) {
e.printStackTrace();
}
}

private void openOutput() {
Bundle extras = getIntent().getExtras();
final AssetManager am = getAssets();
try {
InputStream is = am.open("Outputs/" + "Hello World output.txt");
int size = is.available();
byte[] buffer = new byte[size];
is.read(buffer);
is.close();
String text = new String(buffer);
tvOutput.setText(text);
} catch (IOException e) {
e.printStackTrace();
}
}


private void syntaxHighlight() {

String [] green = {"#define", "#include<stdio.h>", "#include<conio.h>", "#include<stdlib.h>", "#include<math.h>",
"#include<graphics.h>", "#include<string.h>", "#include<malloc.h>", "#include<time.h>", "#include<ctype.h>"};
for(String key: green){
fontcolor(key, 0xFF458b00);
}

doColor();

String[] purple = {"int", "float", "char", "signed", "long", "double", "NULL", "1", "2", "3", "4", "5", "6", "7", "8", "9", "0"};
for(String key: purple){
fontcolor(key, 0xFFc133a4);
}

String [] red = {"printf", "scanf", "if", "else", "for", "while", "switch", "case", "break", "default", "goto", "typedef", "struct", "return", "FILE", "exit", "fopen", "fprintf", "fscanf", "fclose", "(", ")"};
for(String key: red){
fontcolor(key, 0xFFDD2626);
}

String [] orange = {"main()", "getch()", "void"};
for(String key: orange){
fontcolor(key, 0xFFee7621);
}

printColor();
commentColor();
}



private void fontcolor(String key, int color) {
raw = new SpannableString(tv.getText());
int index = TextUtils.indexOf(raw, key);
while (index >= 0)
{
raw.setSpan(new ForegroundColorSpan(color), index, index + key.length(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
index=TextUtils.indexOf(raw, key, index + key.length());
}
tv.setText(raw);
}

private void doColor() {
raw = new SpannableString(tv.getText());
int index = TextUtils.indexOf(raw, "do");
while(index >= 0){
raw.setSpan(new ForegroundColorSpan(Color.parseColor("#DD2626")), index, index + 2, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
index=TextUtils.indexOf(raw, "do", index + 2);
}
tv.setText(raw);
}

private void printColor() {
raw = new SpannableString(tv.getText());
int index1 =TextUtils.indexOf(raw, '"');
int index2 = TextUtils.indexOf(raw, '"', index1+1);
while(index1 >= 0){
raw.setSpan(new ForegroundColorSpan(Color.BLUE), index1, index2+1, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
index1 = TextUtils.indexOf(raw, '"', index2+1);
index2 = TextUtils.indexOf(raw, '"', index1+1);
}
tv.setText(raw);
}

private void commentColor() {
raw = new SpannableString(tv.getText());
int index=TextUtils.indexOf(raw, "*/");
raw.setSpan(new ForegroundColorSpan(Color.parseColor("#198CFF")), 0, index+2, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
tv.setText(raw);
}

}

activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"

xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">


<TextView
android:id="@+id/tvDisplay"
android:background="#324065"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop = "true"
android:layout_centerHorizontal = "true"
/>
<TextView
android:layout_width = "wrap_content"
android:layout_height = "wrap_content"
android:text = "Output:"
android:id = "@+id/textView"
android:layout_below = "@+id/tvDisplay"
android:layout_centerHorizontal = "true"
android:textColor = "#ff7aff24"
android:textSize = "30dp" />
<TextView
android:id="@+id/tvOutputDisplay"
android:background="#4CAF50"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop = "46dp"
android:layout_below = "@+id/tvDisplay"
android:layout_centerHorizontal = "true"
/>

</RelativeLayout>

res/values/dimens.xml
<resources>

<!-- Default screen margins, per the Android Design guidelines. -->
<dimen name="activity_horizontal_margin">16dp</dimen>
<dimen name="activity_vertical_margin">16dp</dimen>
<dimen name="textsize3">8sp</dimen>

</resources>


assets/Programs/Hello World.c
/* Simple C Program to print "Hello World!" */
#include<stdio.h>
#include<conio.h>
main()
{
printf("Hello world!");
getch();
}


assets/Outputs/Hello World output.txt
Hello World!


Reference

Download

Reference

No comments:

Post a Comment

Audio/ Sound Project

 ..Mainfest.xml <? xml version ="1.0" encoding ="utf-8" ?> < manifest xmlns: android ="http://schemas.andr...