Share
10 August 2023
1

To Do Note App in Android (SQLiteDatabase)

A To-Do Note App is a practical and versatile application that helps users organize tasks, set reminders, and keep track of important notes.

activity_main.xml.

Change color.xml or create a file in values folder for the color requirements in the app.

Add recyclerview and ImageButton in activity_main.xml.

Create Vector asset of plus sign give color FFFFFF(white) and add  in the activity_main.xml

Customize ImageButton

Gradient color

Create drawable resource file background_gradient as below

activity_main Design

Creating NotHolder

Create note_holder.xml in Layout.

Put ConstraintLayout inside Cardview with required view.

Create Vector asset for Edit image(pen)

android:maxlines=”1”  is used to show the text lines in our view.

android:ellipsize=”end”

It shortens the text using an ellipsize i.e. three dots.

Create NoteAdapter

Declare id for note,it is used while storing the note in SQLdatabase.

Create getter and setter methods.

Create Constructor without id parameter.

SQLite Database

Create a new class DatabaseHelper.java

It extends class SQLiteOpenHelper.

Override implements methods of SQLiteOpenHelper.

Create a constructor which accepts context as an attribute and called a constructor of super class i.e. SQLitedatabaseHelper which will accept context and other things.

Define DATABASE_VERSION,DATABASE_NAME and pass this to super.

onCreate()

When we create our database in the onCreate method create a table where we store data about something, in this case, we store notes.

Doing operations on the database we used SQL language.

To execute sqlQuery in the database use

sqliteDatabase.execSQL(sqlQuery)

If the query is executed properly then the string color is changed and it recognizes SQL keyword.

onUpgrade()

Database upgrade will be called when we changed DATABASE_VERSION.

Write a query that deletes the table if exits and executes the query and calls onCreate().

If the structure of the database is changed, we need to change the database version onUpgrtader automatically deletes the database & will create a new one by calling onCreate().

In Android, the SQLite database is stored in the internal storage of the app’s sandboxed directory structure. The specific path to the database file depends on the context and structure of your app, but it generally follows a standard format.

The path to the SQLite database file in Android’s internal storage(standard format):

Where:

  • package_name is the package name of your app.
  • database_name is the name you provided when creating the SQLite database.

NoteHandler

We need a class to handle transactions or the saving, updating & delete logic of the Note object.

Need to extend DatabaseHelper.

Create methods for CRUD operation.

Create()

public boolean creates (), type is Boolean bcoz we want to know is a note is created or not.

It will take Note as a parameter.

To insert Data into Database we have to pass the ContenValues object to the insert() method.

Called the database and insert values into it.

Parameter inside insert() methods

nullColumnHack parameter is for accepting empty rows in the database. If we do not pass this parameter then we probably wouldn’t be able to put empty rows in our database.

To know whether our operations successful or not use

Boolean isSuccessfull=db.insrt(not,null,vslues)>0;

If the return value>0 then the note is inserted successfully & if less than 0 note is not inserted properly.

Close the database & return isSuccessfull.

Creating Methods for Reading

Write s method readNotes return ArrayList of note.

Get the database by following the code line

SQLDatabase db=this.getWritableDatabase();

Use the cursor for navigating Database.

cursor.moveToFirst() to move the cursor first.

The close cursor for a memory leak.

Code to fetch readSingleNote.

Creating Methods for delete n Update.

Creating Dialog in Android -Getting input data for note

Create read note methods to read the note in MainActivity.

Create Dialog in Android

Create note_input layout

On click, we have to cancel dialog in order to remove the dialog on the screen.

Deletion on Swapped

To be able to swipe Left and Right specify the direction in the Constructor ItemTouchHelper.SimpleCallback().

That is ItemTouchHelper.LEFT|ItemTouchHelper.RIGHT

When we swiped we have to call NoteHandler

  • Pass context
  • Call delete method to delete a Note we need the id of the note so first need to access the notes ArrayList.
  • Get position with viewholder.getAdapterPosition()

Notify the adapter that the item is remove on viewholder getAdapterPosition()

Remove note from ArrayList

notes.remove(viewHolder.getAdapterPosition())

Create ItemTouchHelper object with the itemTouchCallback & attach this Helper to recyclerView.

Creating EditActivity in purpose of editing existing Note

  • Use CardView to add required attributes & Constraint
  • Give gradient background to ConstraintLayout
  • For CardView background is color primary.

Inside CardView add constraintLayout to Hold Button &Edittext.

Add linear layout inside this constraint layout to hold the Button

View Space is used for adding Space between two views.

Create a button Drawable resource file for a rounded Button

Animating Edit Entrance & Exit

Define interface ItemClicked called method onClick() with the position of the Data that is clicked & View to achieve Animation.

Declare interface in NoteAdapter class & initialize in Constructor so that we communicate from the adapter with mainactivity.

setOnclickListner on  imgEdit call itemClick,onClick(getAdapterPosition,itemView) itemView bcoz we cant initialise view

When itemView is click(means note row) we want to expand it and collapsed

  • To do this we have to change attribute maxlines.

TransactionManager.beginDelayedTransaction(parent) needs a parent bcoz it needs to know what is basically the main layout on which animation will be done.

Go to the main activity & implement the interface.

ActivityOptionCompact to achieving animation when opening other activity.

In note_holder specify the transition name for the card also specify in activity_edit_note

To get data into edit_note used get Intent

To update data

We want to know somehow onclick back or save or cancel button we are back from this edit_note activity for that use startActivityResult & pass request _code

Overide onActivity Result & check request_code

Set listener on cancel & save button

For visibility of button declared linearLayout & initialize with btn_holder layout.

Pass this layout to transactionManager.

GitHub Link: rishizni/android-todo (github.com)