
In this article, we will learn how to perform CRUD (Create Read Update Delete) operations SQLite using Kotlin, the official first-class programming language for Android development. It is very easy and similar to implement like Java. We will learn SQLite implementation by building a simple TODO application.
SQLite
SQLite is an open-source database-based SQL Language. It is widely used and Android has it by default to store data locally.
Steps
I have split this article into four steps as follows.
- Step 1. Creating a new Android Project with Kotlin in Android Studio.
- Step 2. Adding User Interface in your layout file.
- Step 3 . Create a Database and tables using Kotlin.
- Step 4. Implementation of CRUD Operations in Android applications.
Step 1. Creating a new Android project with Kotlin in Android Studio
By default, Android Studio 3.0 has the checkbox for Kotlin Support for your Android Application. Create a new project in Android Studio, check the Kotlin support, and start as usual with Android Studio 3.0.

For migrating Java Android Project to Kotlin Android Project, you can do the following processes.
Configuring Kotlin
Kotlin support can be configured by selecting Tools - Kotlin - Configure Kotlin. Then, click “Sync Now”. This step is applicable to the Android Studio pre-versions of 3.0. The best way is you update your Android Studio.
In Android Studio 3.0, by default, you have Kotlin Activity. Those who have Android Studio with Version less than 3.0, can convert their Java activity into Kotlin Activity.
Open Quick Search or Click Ctrl + Shift + A for Windows Users and Search and Select “Convert Java to Kotlin” or simply Select Ctrl + Shift + Alt + K.
Step 2. Adding a user interface in your Layout file
In this step, we will add the user interfaces for the TODO application. The application has the following screens.
- Tasks List Screen
- List Item Layout for Custom Adapter
- Add Task Screen
- Update/Delete Screen
Tasks List screen
I have used RecyclerView to list the tasks saved in SQLite DB. To learn how to use RecyclerView implementation with Kotlin click here. Create a layout file named as per your wish and paste the following.
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layout_behavior="@string/appbar_scrolling_view_behavior"
tools:context="com.androidmads.kotlinsqlite.MainActivity"
tools:showIn="@layout/activity_main">
<android.support.v7.widget.RecyclerView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/recycler_view"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent"/>
</android.support.constraint.ConstraintLayout>
Then, create a list item for your RecyclerView. Create a layout file and add the following lines.
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@color/colorAccent"
android:orientation="vertical">
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="5dp"
android:background="@android:color/white"
android:orientation="vertical">
<TextView
android:id="@+id/tvName"
android:fontFamily="sans-serif-smallcaps"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:paddingLeft="5dp"
android:paddingRight="5dp"
android:paddingTop="5dp"
android:text="Name"
android:textSize="16sp"
android:textStyle="bold"
tools:targetApi="jelly_bean" />
<TextView
android:id="@+id/tvDesc"
android:fontFamily="sans-serif-smallcaps"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:padding="5dp"
android:text="Desc"
tools:targetApi="jelly_bean" />
</LinearLayout>
</LinearLayout>
Add or Update the screen
Here, we will design the screens for adding/updating the tasks in your TODO Application. Here, I have used the same screen for both Adding and Updating the tasks. Create a layout file and name it as you wish and add the following files.
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:padding="10dp">
<android.support.design.widget.TextInputLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:fontFamily="sans-serif-smallcaps"
tools:targetApi="jelly_bean">
<EditText
android:id="@+id/input_name"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Task Name"
android:fontFamily="sans-serif-smallcaps"
android:inputType="text"
tools:ignore="TextFields"
tools:targetApi="jelly_bean" />
</android.support.design.widget.TextInputLayout>
<android.support.design.widget.TextInputLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:fontFamily="sans-serif-smallcaps"
tools:targetApi="jelly_bean">
<EditText
android:id="@+id/input_desc"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Task Description"
android:fontFamily="sans-serif-smallcaps"
android:inputType="text"
tools:targetApi="jelly_bean"
tools:ignore="TextFields" />
</android.support.design.widget.TextInputLayout>
<android.support.v7.widget.SwitchCompat
android:id="@+id/swt_completed"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Completed "
android:textSize="16sp"
android:textColor="@android:color/darker_gray"
android:fontFamily="sans-serif-smallcaps"
tools:targetApi="jelly_bean" />
<android.support.v7.widget.AppCompatButton
android:id="@+id/btn_save"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:padding="12dp"
android:text="Save"
android:fontFamily="sans-serif-smallcaps"
tools:targetApi="jelly_bean" />
<android.support.v7.widget.AppCompatButton
android:id="@+id/btn_delete"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:padding="12dp"
android:text="Delete"
android:fontFamily="sans-serif-smallcaps"
tools:targetApi="jelly_bean" />
</LinearLayout> 
Join the conversation! Your thoughts help the community grow.