SQLite Kotlin - Notes App - Android Studio Tutorial
SQLite Kotlin - Notes App - Android Studio Tutorial
SQLite Kotlin - Notes App - Android Studio Tutorial
In this tutorial we will make a "Notes App" using SQLite and Kotlin.
It will contain following features.
✓Enter Data
✓Retrieve Data in ListView
✓Update/Edit Data
✓Delete Data
✓Search Data
✓Copy Data
✓Share Data
Step 03: Create new "Android Resource Directory" by clicking "res>New>Android Resource Directory",
choose menu from Resource type
build.gradle(module:App)
apply plugin: 'com.android.application'
android {
compileSdkVersion 27
defaultConfig {
applicationId "com.blogspot.atifsoftwares.sqlitecrud_notesapp_kotlin"
minSdkVersion 16
targetSdkVersion 27
versionCode 1
versionName "1.0"
testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-r
}
}
}
dependencies {
implementation fileTree(dir: 'libs', include: ['*.jar'])
implementation "org.jetbrains.kotlin:kotlin-stdlib-jre7:$kotlin_version"
implementation 'com.android.support:appcompat-v7:27.1.1'
implementation 'com.android.support:cardview-v7:27.1.1'
implementation 'com.android.support.constraint:constraint-layout:1.1.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'
}
menu_main.xml
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">
<item
android:id="@+id/action_settings"
android:title="settings" />
<item
android:id="@+id/app_bar_search"
android:icon="@drawable/ic_action_search"
android:title="Search"
app:actionViewClass="android.support.v7.widget.SearchView"
app:showAsAction="always" />
<item
android:id="@+id/addNote"
android:icon="@drawable/ic_action_add"
android:title="Add Nore"
app:showAsAction="always" />
</menu>
colors.xml
row.xml
Li L t
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="5dp"
android:gravity="end|bottom"
android:orientation="vertical">
<TextView
android:id="@+id/titleTv"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Title"
android:textColor="@color/colorPrimary"
android:textSize="22sp"
android:textStyle="bold" />
<TextView
android:id="@+id/descTv"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="There may be a very long description of the note"
android:textSize="18sp" />
<View
android:layout_width="match_parent"
android:layout_height="1dp"
android:layout_marginBottom="3dp"
android:layout_marginTop="2dp"
android:background="@color/colorPrimaryDark" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="end"
android:orientation="horizontal">
<ImageButton
android:id="@+id/deleteBtn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginEnd="5dp"
android:layout_marginRight="5dp"
android:background="@null"
android:src="@drawable/ic_action_delete" />
<ImageButton
android:id="@+id/editBtn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginEnd="5dp"
android:layout_marginRight="5dp"
android:background="@null"
android:src="@drawable/ic_action_edit" />
<ImageButton
android:id="@+id/copyBtn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginEnd="5dp"
android:layout_marginRight="5dp"
android:background="@null"
android:src="@drawable/ic_action_copy" />
<ImageButton
android:id="@+id/shareBtn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginEnd="5dp"
android:layout_marginRight="5dp"
android:background="@null"
android:src="@drawable/ic_action_share" />
</LinearLayout>
</LinearLayout>
</android.support.v7.widget.CardView>
Note.kt
package com.blogspot.atifsoftwares.sqlitecrud_notesapp_kotlin
DbManager.kt
package com.blogspot.atifsoftwares.sqlitecrud_notesapp_kotlin
import android.app.DownloadManager
import android.content.ContentValues
import android.content.Context
import android.database.Cursor
import android.database.sqlite.SQLiteDatabase
import android.database.sqlite.SQLiteOpenHelper
import android.database.sqlite.SQLiteQueryBuilder
import android.widget.Toast
class DbManager {
//database name
var dbName = "MyNotes"
//table name
var dbTable = "Notes"
//columns
var colID = "ID"
var colTitle = "Title"
var colDes = "Description"
//database version
var dbVersion = 1
//CREATE TABLE IF NOT EXISTS MyNotes (ID INTEGER PRIMARY KEY,title TEXT, Descript
val sqlCreateTable = "CREATE TABLE IF NOT EXISTS " + dbTable + " (" + colID + " I
constructor(context: Context) {
var db = DatabaseHelperNotes(context)
sqlDB = db.writableDatabase
}
</LinearLayout>
MainActivity.kt
package com.blogspot.atifsoftwares.sqlitecrud_notesapp_kotlin
import android.app.SearchManager
import android.content.Context
import android.content.Intent
import android.support.v7.app.AppCompatActivity
import android.os.Bundle
import android.support.v7.widget.SearchView
import android.text.ClipboardManager
import android.view.Menu
import android.view.MenuItem
import android.view.View
import android.view.ViewGroup
import android.widget.BaseAdapter
import android.widget.Toast
import kotlinx.android.synthetic.main.activity_main.*
import kotlinx.android.synthetic.main.row.*
import kotlinx.android.synthetic.main.row.view.*
//Load from DB
LoadQuery("%")
}
do {
val ID = cursor.getInt(cursor.getColumnIndex("ID"))
val Title = cursor.getString(cursor.getColumnIndex("Title"))
val Description = cursor.getString(cursor.getColumnIndex("Description
} while (cursor.moveToNext())
}
//adapter
var myNotesAdapter = MyNotesAdapter(this, listNotes)
//set adapter
notesLv.adapter = myNotesAdapter
//searchView
val sv: SearchView = menu!!.findItem(R.id.app_bar_search).actionView as Searc
return super.onCreateOptionsMenu(menu)
}
return myView
}
activity_add_note.xml
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<android.support.v7.widget.CardView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="5dp"
app:cardBackgroundColor="@color/white"
app:cardCornerRadius="3dp"
app:cardElevation="3dp">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<EditText
android:id="@+id/titleEt"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@null"
android:hint="Enter Title"
android:padding="10dp"
android:singleLine="true"
android:textStyle="bold" />
<EditText
android:id="@+id/descEt"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@null"
d id it "t "
android:gravity="top"
android:hint="Enter description..."
android:minHeight="100dp"
android:padding="10dp" />
</LinearLayout>
</android.support.v7.widget.CardView>
<Button
android:id="@+id/addBtn"
style="@style/Base.Widget.AppCompat.Button.Colored"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="end"
android:layout_marginEnd="5dp"
android:layout_marginRight="5dp"
android:onClick="addFunc"
android:text="Add" />
</LinearLayout>
</ScrollView>
AddNoteActivity.kt
package com.blogspot.atifsoftwares.sqlitecrud_notesapp_kotlin
import android.content.ContentValues
import android.support.v7.app.AppCompatActivity
import android.os.Bundle
import android.view.View
import android.widget.Toast
import kotlinx.android.synthetic.main.activity_add_note.*
try {
val bundle:Bundle = intent.extras
id = bundle.getInt("ID", 0)
if (id!=0){
titleEt.setText(bundle.getString("name"))
descEt.setText(bundle.getString("des"))
}
}catch (ex:Exception){}
}
fun addFunc(view:View){
var dbManager = DbManager(this)
if (id ==0){
val ID = dbManager.insert(values)
if (ID>0){
Toast.makeText(this, "Note is added", Toast.LENGTH_SHORT).show()
finish()
}
else{
Toast.makeText(this, "Error adding note...", Toast.LENGTH_SHORT).show
}
}
else{
var selectionArgs = arrayOf(id.toString())
val ID = dbManager.update(values, "ID=?", selectionArgs)
if (ID>0){
Toast.makeText(this, "Note is added", Toast.LENGTH_SHORT).show()
finish()
}
else{
Toast.makeText(this, "Error adding note...", Toast.LENGTH_SHORT).show
}
}
}
}
Step 09: Run Project
Video
android studio tutorials android tutorials Kotlin ListView SQLite Notes App SQLite
REPLY
DESCRIPTION In this tutorial we will add a back button in action bar, when it is
clicked it will go to previous activity(the app will close if this was launcher activity). We will
go from main activity to new activity by clicking button in main activity. In new activity we …
READ MORE
How to create Create AlertDialog With Custom Layout (Kotlin)? DESCRIPTION This
tutorial will show how to create and show an AlertDialog with C ustom Layout containing
…
views such as EditTexts and Buttons etc. We will show AlertDialog on Button click. Custom
READ MORE
Export an Android Studio project as a ZIP le Starting with the Android Studio 3 0 you can
Export an Android Studio project as a ZIP le Starting with the Android Studio 3.0, you can
use File | Export to Zip File... to export your project to zip or HTML easily. There is also a
great advantage of exporting the project as a zip le, that is, it exports in very small size …
READ MORE
Powered by Blogger
Atif Pervaiz©
ATIF PERVAIZ
VISIT PROFILE
Menu
Home
ActionBar
SQLite
Android Video
Tutorials
AlertDialog
ListView
Android Menu
Activities
Libraries
Firebase
Splash Screen
Toast
SnackBar
WebView
Button
Spinner
CheckBox
Recyclerview
Bottom Sheet
SharedPreferences
Date Picker
Time Picker
Labels
My Gigs
Seller
Programming & Tech