You are viewing our Forum Archives. To view or take place in current topics click here.
App Source Code Template (needed)
Posted:

App Source Code Template (needed)Posted:

Lizzi
  • Wise One
Status: Offline
Joined: Nov 03, 20158Year Member
Posts: 537
Reputation Power: 33
Status: Offline
Joined: Nov 03, 20158Year Member
Posts: 537
Reputation Power: 33
Hey there,

I have to give my Students a App Template (Source Code) tomorrow.

For Android Studio or Visual Studio.

If you ot any Website that got any app source code to download, link me that down below.

Thank you
#2. Posted:
-Deano
  • Rated Awesome
Status: Offline
Joined: Aug 19, 201013Year Member
Posts: 5,238
Reputation Power: 532
Status: Offline
Joined: Aug 19, 201013Year Member
Posts: 5,238
Reputation Power: 532
Well that's dreadful preparation. I'm quite worried for your students if you are asking for random templates online instead of preparing it for them.

Either way, here's an example application I created that handles saving text to a file using AsyncTask:


package com.example.fileioapp;

import android.content.Context;
import android.os.AsyncTask;
import android.os.Bundle;
import android.os.Environment;
import android.support.v7.app.AlertDialog;
import android.support.v7.app.AppCompatActivity;
import android.view.Menu;
import android.view.MenuItem;
import android.view.MenuInflater;
import android.widget.EditText;

import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.PrintWriter;

public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
    }

    public boolean onCreateOptionsMenu(Menu menu) {
        MenuInflater inflater = getMenuInflater();
        inflater.inflate(R.menu.menu_options, menu);
        return true;
    }

    public boolean onOptionsItemSelected(MenuItem item) {
        if (item.getItemId() == R.id.save) {
            // Save to file
            try {
                // Get text to save
                EditText txt = (EditText) findViewById(R.id.textbox);
                String textToSave = txt.getText().toString();

                // Save in background
                SaveFile save = new SaveFile();
                save.execute(textToSave);
            } catch (NullPointerException e) {
                new AlertDialog
                        .Builder(this)
                        .setTitle("ERROR")
                        .setMessage("NullPointerException:\n" + e)
                        .setPositiveButton("OK", null).show();
            } catch (Exception e) {
                new AlertDialog
                        .Builder(this)
                        .setTitle("ERROR")
                        .setMessage("Unexpected error:\n" + e)
                        .setPositiveButton("OK", null)
                        .show();
            }
            return true;
        }

        if (item.getItemId() == R.id.load) {
            // Load from file
            try {
                LoadFile load = new LoadFile();
                load.execute();
            } catch (Exception e) {
                new AlertDialog.Builder(this).setTitle("ERROR").setMessage("Unknown error:\n" + e).
                        setPositiveButton("OK", null).show();
            }
            return true;
        }
        return false;
    }



    class SaveFile extends AsyncTask<String,Void,String> {
        protected String doInBackground(String... params) {
            String message = "Successfully saved to file!";
            try {
                String filename = Environment.getExternalStorageDirectory().getAbsolutePath() + "/edittext.txt";

                PrintWriter pw = new PrintWriter(new FileWriter(filename));
                pw.println(params[0]);
                pw.close();
            } catch (IOException e) {
                message = e.toString();
            }

            return message;
        }

        public void onPostExecute(String message) {
            EditText et = (EditText) findViewById(R.id.textbox);
            et.setText("");

            new AlertDialog
                    .Builder(MainActivity.this)
                    .setMessage(message)
                    .setPositiveButton("OK", null)
                    .show();
        }
    }

    class LoadFile extends AsyncTask<Void,Void,String> {
        private String text = "";

        protected String doInBackground(Void... unused) {
            String message = "File successfully loaded!";
            try {
                FileReader fr = new FileReader(Environment.getExternalStorageDirectory()
                        .getAbsolutePath() + "/edittext.txt");
                BufferedReader reader = new BufferedReader(fr);
                String line;
                while ((line = reader.readLine()) != null) {
                    text += line;
                }
            } catch(IOException e) {
                System.out.println(e.toString());
                message = e.toString();
            }
            System.out.println(message);
            return message;
        }

        @Override
        protected void onPostExecute(String message) {
            System.out.println("PostExecute");
            EditText et = (EditText) findViewById(R.id.textbox);
            et.setText(text);

            new AlertDialog
                    .Builder(MainActivity.this)
                    .setMessage(message)
                    .setPositiveButton("OK", null)
                    .show();
        }
    }
}
#3. Posted:
ProJimmyRustler
  • TTG Senior
Status: Offline
Joined: Jul 14, 20149Year Member
Posts: 1,720
Reputation Power: 71
Status: Offline
Joined: Jul 14, 20149Year Member
Posts: 1,720
Reputation Power: 71
-Deano wrote Well that's dreadful preparation. I'm quite worried for your students if you are asking for random templates online instead of preparing it for them.

Either way, here's an example application I created that handles saving text to a file using AsyncTask:


package com.example.fileioapp;

import android.content.Context;
import android.os.AsyncTask;
import android.os.Bundle;
import android.os.Environment;
import android.support.v7.app.AlertDialog;
import android.support.v7.app.AppCompatActivity;
import android.view.Menu;
import android.view.MenuItem;
import android.view.MenuInflater;
import android.widget.EditText;

import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.PrintWriter;

public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
    }

    public boolean onCreateOptionsMenu(Menu menu) {
        MenuInflater inflater = getMenuInflater();
        inflater.inflate(R.menu.menu_options, menu);
        return true;
    }

    public boolean onOptionsItemSelected(MenuItem item) {
        if (item.getItemId() == R.id.save) {
            // Save to file
            try {
                // Get text to save
                EditText txt = (EditText) findViewById(R.id.textbox);
                String textToSave = txt.getText().toString();

                // Save in background
                SaveFile save = new SaveFile();
                save.execute(textToSave);
            } catch (NullPointerException e) {
                new AlertDialog
                        .Builder(this)
                        .setTitle("ERROR")
                        .setMessage("NullPointerException:\n" + e)
                        .setPositiveButton("OK", null).show();
            } catch (Exception e) {
                new AlertDialog
                        .Builder(this)
                        .setTitle("ERROR")
                        .setMessage("Unexpected error:\n" + e)
                        .setPositiveButton("OK", null)
                        .show();
            }
            return true;
        }

        if (item.getItemId() == R.id.load) {
            // Load from file
            try {
                LoadFile load = new LoadFile();
                load.execute();
            } catch (Exception e) {
                new AlertDialog.Builder(this).setTitle("ERROR").setMessage("Unknown error:\n" + e).
                        setPositiveButton("OK", null).show();
            }
            return true;
        }
        return false;
    }



    class SaveFile extends AsyncTask<String,Void,String> {
        protected String doInBackground(String... params) {
            String message = "Successfully saved to file!";
            try {
                String filename = Environment.getExternalStorageDirectory().getAbsolutePath() + "/edittext.txt";

                PrintWriter pw = new PrintWriter(new FileWriter(filename));
                pw.println(params[0]);
                pw.close();
            } catch (IOException e) {
                message = e.toString();
            }

            return message;
        }

        public void onPostExecute(String message) {
            EditText et = (EditText) findViewById(R.id.textbox);
            et.setText("");

            new AlertDialog
                    .Builder(MainActivity.this)
                    .setMessage(message)
                    .setPositiveButton("OK", null)
                    .show();
        }
    }

    class LoadFile extends AsyncTask<Void,Void,String> {
        private String text = "";

        protected String doInBackground(Void... unused) {
            String message = "File successfully loaded!";
            try {
                FileReader fr = new FileReader(Environment.getExternalStorageDirectory()
                        .getAbsolutePath() + "/edittext.txt");
                BufferedReader reader = new BufferedReader(fr);
                String line;
                while ((line = reader.readLine()) != null) {
                    text += line;
                }
            } catch(IOException e) {
                System.out.println(e.toString());
                message = e.toString();
            }
            System.out.println(message);
            return message;
        }

        @Override
        protected void onPostExecute(String message) {
            System.out.println("PostExecute");
            EditText et = (EditText) findViewById(R.id.textbox);
            et.setText(text);

            new AlertDialog
                    .Builder(MainActivity.this)
                    .setMessage(message)
                    .setPositiveButton("OK", null)
                    .show();
        }
    }
}


I agree with you on the dreadful preparation. I don't feel like a college level graduate would leave finding something like this up to a forum website. I mean if you don't want to put the effort into it, then you probably should not be teaching.

If you want some examples, google offers an application maker here:
[ Register or Signin to view external links. ]
#4. Posted:
Lizzi
  • Wise One
Status: Offline
Joined: Nov 03, 20158Year Member
Posts: 537
Reputation Power: 33
Status: Offline
Joined: Nov 03, 20158Year Member
Posts: 537
Reputation Power: 33
oh no, i forgot to say that my mac just broke and made me really nervous because there I got all of my projects. and etc... but thanks. if u need any help just message me.
#5. Posted:
-Deano
  • Spooky Poster
Status: Offline
Joined: Aug 19, 201013Year Member
Posts: 5,238
Reputation Power: 532
Status: Offline
Joined: Aug 19, 201013Year Member
Posts: 5,238
Reputation Power: 532
I strongly advise you backup your work in the future.
#6. Posted:
Lizzi
  • Wise One
Status: Offline
Joined: Nov 03, 20158Year Member
Posts: 537
Reputation Power: 33
Status: Offline
Joined: Nov 03, 20158Year Member
Posts: 537
Reputation Power: 33
-Deano wrote I strongly advise you backup your work in the future.


yea
Jump to:
You are viewing our Forum Archives. To view or take place in current topics click here.