Hey, Developers! In last tutorial we learned about the basic operation of CRUD in the SQLite Database using SQLiteOpenHelper class.
Now in this tutorial we're going to create a simple Notes App with SQLite as database storage. The app will be very minimal and will have only one screen to handle the notes.
At first, we will create android application. Then, we will see how to build Notes App in the application.
Follow steps below to create new project. Please ignore the steps if you have already created a new application.
S. No. | Steps |
1 | Open Android Studio. |
2 | Click on Start a new Android Studio Project then choose your project and select Empty Activity. |
3 | Write application name as NotesApp after this write a package name you can write your own package name. |
4 | Select language Java. Select minimum SDK you need. However, we have selected 17 as minimum SDK. Then, click next button. |
5 | If you have followed above process correctly, you will get a newly created project successfully. However, you can also visit tutorial to Create a New Project to know steps in detail. |
Open build.gradle under the folder of the app and add RecyclerView dependency.
" implementation 'com.android.support:recyclerview-v7:28.0.0' "
The RecyclerView will be used to show the notes in a list format.
apply plugin: 'com.android.application'
android {
compileSdkVersion 28
defaultConfig {
applicationId "com.ukacademe.notesapp"
minSdkVersion 17
targetSdkVersion 28
versionCode 1
versionName "1.0"
testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
}
}
}
dependencies {
implementation fileTree(dir: 'libs', include: ['*.jar'])
implementation 'com.android.support:appcompat-v7:28.0.0'
implementation 'com.android.support.constraint:constraint-layout:1.1.3'
implementation 'com.android.support:recyclerview-v7:28.0.0'
testImplementation 'junit:junit:4.12'
androidTestImplementation 'com.android.support.test:runner:1.0.2'
androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.2'
}
Click on res => values folder and open the color.xml file. And include the following code.
<?xml version="1.0" encoding="utf-8"?>
<resources>
<color name="colorPrimary">#008577</color>
<color name="colorPrimaryDark">#00574B</color>
<color name="colorAccent">#D81B60</color>
<color name="msg_no_notes">#999</color>
<color name="hint_enter_note">#89c3c3c3</color>
<color name="timestamp">#858585</color>
<color name="note_list_text">#232323</color>
</resources>
Click on res => values folder and open the dimens.xml file. And include the following code.
<?xml version="1.0" encoding="utf-8"?>
<resources>
<dimen name="fab_margin">16dp</dimen>
<dimen name="activity_margin">16dp</dimen>
<dimen name="dot_margin_right">10dp</dimen>
<dimen name="msg_no_notes">26sp</dimen>
<dimen name="margin_top_no_notes">120dp</dimen>
<dimen name="lbl_new_note_title">20sp</dimen>
<dimen name="dimen_10">10dp</dimen>
<dimen name="input_new_note">20sp</dimen>
<dimen name="dot_height">30dp</dimen>
<dimen name="dot_text_size">40sp</dimen>
<dimen name="timestamp">14sp</dimen>
<dimen name="note_list_text">18sp</dimen>
</resources>
Click on res => values folder and open strings.xml file. And include the following code. This file includes all the strings that the project uses.
<resources>
<string name="app_name">Notes App</string>
<string name="action_settings">Settings</string>
<string name="activity_title_home">Notes</string>
<string name="msg_no_notes">No notes found!</string>
<string name="lbl_new_note_title">New Note</string>
<string name="lbl_edit_note_title">Edit Note</string>
<string name="hint_enter_note">Enter your note!</string>
</resources>
We need to build a class extending from SQLiteOpenHelper. This class performs CRUD activities (Create, Read, Update, and Delete) on the database.
In order to handle notes readily, we also need a model class to generate Note items.
Click on java => com.ukacademe.notesapp and create a package and write a name as model under this package create a class named Note.java. In this class we define the SQLite table name, column names and create table SQL query along with getter / setter methods.
So open the Note.java class. And include the following code.
package com.ukacademe.notesapp.model;
public class Note {
public static final String TABLE_NAME = "notes";
public static final String COLUMN_ID = "id";
public static final String COLUMN_NOTE = "note";
public static final String COLUMN_TIMESTAMP = "timestamp";
private int id;
private String note;
private String timestamp;
// Create table SQL query
public static final String CREATE_TABLE =
"CREATE TABLE " + TABLE_NAME + "("
+ COLUMN_ID + " INTEGER PRIMARY KEY AUTOINCREMENT,"
+ COLUMN_NOTE + " TEXT,"
+ COLUMN_TIMESTAMP + " DATETIME DEFAULT CURRENT_TIMESTAMP"
+ ")";
public Note() {
}
public Note(int id, String note, String timestamp) {
this.id = id;
this.note = note;
this.timestamp = timestamp;
}
public int getId() {
return id;
}
public String getNote() {
return note;
}
public void setNote(String note) {
this.note = note;
}
public String getTimestamp() {
return timestamp;
}
public void setId(int id) {
this.id = id;
}
public void setTimestamp(String timestamp) {
this.timestamp = timestamp;
}
}
Right click on model => DatabaseHelper create a class called DatabaseHelper.java under the model package and extend the SQLiteOpenHelper class. This class holds methods for performing CRUD operations related to the database.