Created
January 4, 2013 12:08
-
-
Save spences10/4452164 to your computer and use it in GitHub Desktop.
ActivityAdd
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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