Skip to content

Instantly share code, notes, and snippets.

@spences10
Created January 4, 2013 12:08
Show Gist options
  • Save spences10/4452164 to your computer and use it in GitHub Desktop.
Save spences10/4452164 to your computer and use it in GitHub Desktop.
ActivityAdd
package com.spences10apps.SScript;
import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.os.Environment;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.widget.EditText;
import android.widget.Toast;
import java.io.BufferedWriter;
import java.io.FileWriter;
import java.io.IOException;
public class ActivityAdd extends Activity {
/**
* Called when the activity is first created.
*/
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.layout_add_script);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.menu_add, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem menuItem) {
// Handle item selection
switch (menuItem.getItemId()) {
case R.id.menu_save:
addItem(menuItem);
return true;
default:
return super.onOptionsItemSelected(menuItem);
}
}
public void addItem(MenuItem menuItem) {
EditText textName = (EditText) findViewById(R.id.txtScriptName);
EditText textBody = (EditText) findViewById(R.id.txtScriptBody);
String sdcard = Environment.getExternalStorageDirectory().getAbsolutePath();
try {
FileWriter filenew = new FileWriter(sdcard + "/" + textName.getText().toString() + ".txt");
BufferedWriter bw = new BufferedWriter(filenew);
bw.write(textBody.getText().toString());
bw.close();
} catch (IOException e) {
//You'll need to add proper error handling here
Context context = getApplicationContext();
CharSequence textToast = e.toString();
int duration = Toast.LENGTH_LONG;
Toast toast = Toast.makeText(context, textToast, duration);
toast.show();
} finally {
Context context = getApplicationContext();
CharSequence textToast = textName.getText().toString() + " saved to sdcard.";
int duration = Toast.LENGTH_SHORT;
Toast toast = Toast.makeText(context, textToast, duration);
toast.show();
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment