This is a sample application, having the features as the notepad like cut, copy, paste and save. You can also edit the Note by opening it again.
Underlying Algorithm:
Basic description of algorithm in step by step form:
1.) Create a Project MyNoteEditer.
2.) Create a class NotesDbAdapter.java, in this class we will use SQLiteDatabase and SQLiteOpenHelper to handle the database, to save and edit the data.
1.) Create a Project MyNoteEditer.
2.) Create a class NotesDbAdapter.java, in this class we will use SQLiteDatabase and SQLiteOpenHelper to handle the database, to save and edit the data.
package com.app.MyNoteEditer;
import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.SQLException;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.util.Log;
import android.content.Context;
import android.database.Cursor;
import android.database.SQLException;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.util.Log;
public class NotesDbAdapter
{
public static final String KEY_TITLE = "title";
public static final String KEY_BODY = "body";
public static final String KEY_ROWID = "_id";
{
public static final String KEY_TITLE = "title";
public static final String KEY_BODY = "body";
public static final String KEY_ROWID = "_id";
private static final String TAG = "NotesDbAdapter";
private DatabaseHelper mDbHelper;
private SQLiteDatabase mDb;
private DatabaseHelper mDbHelper;
private SQLiteDatabase mDb;
private static final String DATABASE_CREATE = "create table notes (_id integer primary key autoincrement, " + "title text not null, body text not null);";
private static final String DATABASE_NAME = "data";
private static final String DATABASE_TABLE = "notes";
private static final int DATABASE_VERSION = 2;
private static final String DATABASE_TABLE = "notes";
private static final int DATABASE_VERSION = 2;
private static class DatabaseHelper extends SQLiteOpenHelper {
DatabaseHelper(Context context) {
super(context, DATABASE_NAME, null, DATABASE_VERSION);
}
DatabaseHelper(Context context) {
super(context, DATABASE_NAME, null, DATABASE_VERSION);
}
@Override
public void onCreate(SQLiteDatabase db) {
db.execSQL(DATABASE_CREATE);
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion)
{
Log.w(TAG, "Upgrading database from version " + oldVersion + " to " + newVersion + ", which will destroy all old data");
db.execSQL("DROP TABLE IF EXISTS notes");
onCreate(db);
}
}
public void onCreate(SQLiteDatabase db) {
db.execSQL(DATABASE_CREATE);
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion)
{
Log.w(TAG, "Upgrading database from version " + oldVersion + " to " + newVersion + ", which will destroy all old data");
db.execSQL("DROP TABLE IF EXISTS notes");
onCreate(db);
}
}
public NotesDbAdapter open() throws SQLException {
mDbHelper = new DatabaseHelper(mCtx);
mDb = mDbHelper.getWritableDatabase();
return this;
}
mDbHelper = new DatabaseHelper(mCtx);
mDb = mDbHelper.getWritableDatabase();
return this;
}
public void close() {
mDbHelper.close();
}
mDbHelper.close();
}
public long createNote(String title, String body) {
ContentValues initialValues = new ContentValues();
initialValues.put(KEY_TITLE, title);
initialValues.put(KEY_BODY, body);
return mDb.insert(DATABASE_TABLE, null, initialValues);
}
ContentValues initialValues = new ContentValues();
initialValues.put(KEY_TITLE, title);
initialValues.put(KEY_BODY, body);
return mDb.insert(DATABASE_TABLE, null, initialValues);
}
public boolean deleteNote(long rowId) {
return mDb.delete(DATABASE_TABLE, KEY_ROWID + "=" + rowId, null) > 0;
}
return mDb.delete(DATABASE_TABLE, KEY_ROWID + "=" + rowId, null) > 0;
}
public void deleteAllNote() {
mDb.delete(DATABASE_TABLE, null, null);
}
public Cursor fetchAllNotes() {
return mDb.query(DATABASE_TABLE, new String[] {KEY_ROWID, KEY_TITLE, KEY_BODY}, null, null, null, null, null);
}
mDb.delete(DATABASE_TABLE, null, null);
}
public Cursor fetchAllNotes() {
return mDb.query(DATABASE_TABLE, new String[] {KEY_ROWID, KEY_TITLE, KEY_BODY}, null, null, null, null, null);
}
public Cursor fetchNote(long rowId) throws SQLException {
Cursor mCursor = mDb.query(true, DATABASE_TABLE, new String[] {KEY_ROWID, KEY_TITLE, KEY_BODY}, KEY_ROWID + "=" + rowId, null, null, null, null, null);
if (mCursor != null) {
mCursor.moveToFirst();
}
return mCursor;
}
Cursor mCursor = mDb.query(true, DATABASE_TABLE, new String[] {KEY_ROWID, KEY_TITLE, KEY_BODY}, KEY_ROWID + "=" + rowId, null, null, null, null, null);
if (mCursor != null) {
mCursor.moveToFirst();
}
return mCursor;
}
3.) Create a class NewText.java having save, cut, copy and paste functionality:
package com.app.MyNoteEditer;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.text.Editable;
import android.text.Spannable;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.content.Intent;
import android.os.Bundle;
import android.text.Editable;
import android.text.Spannable;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
public class NewText extends Activity implements OnClickListener
{
EditText editbox1;
EditText editbox2;
Button cutb;
Button copyb;
Button pasteb;
Button saveb,backb;
Editable s1,s2;
Spannable str;
Spannable str2;
{
EditText editbox1;
EditText editbox2;
Button cutb;
Button copyb;
Button pasteb;
Button saveb,backb;
Editable s1,s2;
Spannable str;
Spannable str2;
private Long mRowId;
public void onCreate(Bundle icicle) {
super.onCreate(icicle);
setContentView(R.layout.new_text);
editbox1 =(EditText) findViewById(R.id.title);
editbox2 =(EditText) findViewById(R.id.insertdata);
mRowId = null;
Bundle extras = getIntent().getExtras();
if (extras != null) {
String title = extras.getString(NotesDbAdapter.KEY_TITLE);
String body = extras.getString(NotesDbAdapter.KEY_BODY);
mRowId = extras.getLong(NotesDbAdapter.KEY_ROWID);
if (title != null) {
editbox1.setText(title);
}
if (body != null) {
editbox2.setText(body);
}
}
cutb =(Button) findViewById(R.id.cut);
cutb.setOnClickListener(this);
copyb =(Button)findViewById(R.id.copy);
copyb.setOnClickListener(this);
pasteb =(Button)findViewById(R.id.paste);
pasteb.setOnClickListener(this);
saveb =(Button) findViewById(R.id.save);
saveb.setOnClickListener(this);
backb =(Button) findViewById(R.id.back);
backb.setOnClickListener(this);
}
public void onBackPressed() {
moveTaskToBack(true);
return;
}
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
switch(v.getId())
{
case R.id.save :
Bundle bundle = new Bundle();
bundle.putString(NotesDbAdapter.KEY_TITLE, editbox1.getText().toString());
bundle.putString(NotesDbAdapter.KEY_BODY, editbox2.getText().toString());
if (mRowId != null) {
bundle.putLong(NotesDbAdapter.KEY_ROWID, mRowId);
}
Intent mIntent = new Intent();
mIntent.putExtras(bundle);
setResult(RESULT_OK, mIntent);
finish();
break;
case R.id.copy :
//String selectedText = editbox2.getText().substring(editbox2.getSelectionStart(), editbox2.getSelectionEnd());
if(editbox2.getSelectionEnd() > editbox2.getSelectionStart())
{
s1 = (Editable)editbox2.getText().subSequence(editbox2.getSelectionStart(), editbox2.getSelectionEnd());
}else
{
s1 = (Editable)editbox2.getText().subSequence(editbox2.getSelectionEnd(), editbox2.getSelectionStart());
}
break;
case R.id.cut :
if(editbox2.getSelectionEnd() > editbox2.getSelectionStart())
{
s1 = (Editable)editbox2.getText().subSequence(editbox2.getSelectionStart(), editbox2.getSelectionEnd());
}else
{
s1 = (Editable)editbox2.getText().subSequence(editbox2.getSelectionEnd(), editbox2.getSelectionStart());
}
editbox2.getText().replace(Math.min(editbox2.getSelectionStart(), editbox2.getSelectionEnd()), Math.max(editbox2.getSelectionStart(), editbox2.getSelectionEnd()),"", 0, 0);
break;
case R.id.paste :
editbox2.getText().replace(Math.min(editbox2.getSelectionStart(), editbox2.getSelectionEnd()), Math.max(editbox2.getSelectionStart(), editbox2.getSelectionEnd()),s1, 0, s1.length());
break;
case R.id.back :
Intent mIntent2 = new Intent(this,OpenNotes.class);
startActivity(mIntent2);
finish();
break;
}
}
}
public void onCreate(Bundle icicle) {
super.onCreate(icicle);
setContentView(R.layout.new_text);
editbox1 =(EditText) findViewById(R.id.title);
editbox2 =(EditText) findViewById(R.id.insertdata);
mRowId = null;
Bundle extras = getIntent().getExtras();
if (extras != null) {
String title = extras.getString(NotesDbAdapter.KEY_TITLE);
String body = extras.getString(NotesDbAdapter.KEY_BODY);
mRowId = extras.getLong(NotesDbAdapter.KEY_ROWID);
if (title != null) {
editbox1.setText(title);
}
if (body != null) {
editbox2.setText(body);
}
}
cutb =(Button) findViewById(R.id.cut);
cutb.setOnClickListener(this);
copyb =(Button)findViewById(R.id.copy);
copyb.setOnClickListener(this);
pasteb =(Button)findViewById(R.id.paste);
pasteb.setOnClickListener(this);
saveb =(Button) findViewById(R.id.save);
saveb.setOnClickListener(this);
backb =(Button) findViewById(R.id.back);
backb.setOnClickListener(this);
}
public void onBackPressed() {
moveTaskToBack(true);
return;
}
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
switch(v.getId())
{
case R.id.save :
Bundle bundle = new Bundle();
bundle.putString(NotesDbAdapter.KEY_TITLE, editbox1.getText().toString());
bundle.putString(NotesDbAdapter.KEY_BODY, editbox2.getText().toString());
if (mRowId != null) {
bundle.putLong(NotesDbAdapter.KEY_ROWID, mRowId);
}
Intent mIntent = new Intent();
mIntent.putExtras(bundle);
setResult(RESULT_OK, mIntent);
finish();
break;
case R.id.copy :
//String selectedText = editbox2.getText().substring(editbox2.getSelectionStart(), editbox2.getSelectionEnd());
if(editbox2.getSelectionEnd() > editbox2.getSelectionStart())
{
s1 = (Editable)editbox2.getText().subSequence(editbox2.getSelectionStart(), editbox2.getSelectionEnd());
}else
{
s1 = (Editable)editbox2.getText().subSequence(editbox2.getSelectionEnd(), editbox2.getSelectionStart());
}
break;
case R.id.cut :
if(editbox2.getSelectionEnd() > editbox2.getSelectionStart())
{
s1 = (Editable)editbox2.getText().subSequence(editbox2.getSelectionStart(), editbox2.getSelectionEnd());
}else
{
s1 = (Editable)editbox2.getText().subSequence(editbox2.getSelectionEnd(), editbox2.getSelectionStart());
}
editbox2.getText().replace(Math.min(editbox2.getSelectionStart(), editbox2.getSelectionEnd()), Math.max(editbox2.getSelectionStart(), editbox2.getSelectionEnd()),"", 0, 0);
break;
case R.id.paste :
editbox2.getText().replace(Math.min(editbox2.getSelectionStart(), editbox2.getSelectionEnd()), Math.max(editbox2.getSelectionStart(), editbox2.getSelectionEnd()),s1, 0, s1.length());
break;
case R.id.back :
Intent mIntent2 = new Intent(this,OpenNotes.class);
startActivity(mIntent2);
finish();
break;
}
}
}
No comments:
Post a Comment