In this tutorial, we'll show you how to create a simple contact list app in Android Studio using Kotlin and SQLite. By the end of this tutorial, you'll have a working app that allows users to create, read, update, and delete contacts. follow the below steps to create an app.
Click here to Watch Video on Youtube
Step 1: Create a New android studio project with an empty activity.
Step 2: Add color code in the colors.xml file as follow
colors.xml
<color name="grey">#8BC34A</color>
Step 3: Add Create a new layout resource file in the layout folder name table_row.xml. and add the following code to it.
table_row.xml
<?xml version="1.0" encoding="utf-8"?>
<TableRow xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/removeButton">
<TextView
android:id="@+id/numberTextView"
android:layout_width="0dp"
android:layout_height="22dp"
android:layout_weight="1"
android:gravity="center"
android:textColor="#000"
android:background="@drawable/cell_shape"
android:textSize="13sp" />
<TextView
android:id="@+id/nameTextView"
android:layout_width="0dp"
android:layout_height="22dp"
android:layout_weight="1"
android:gravity="center"
android:textColor="#000"
android:background="@drawable/cell_shape"
android:textSize="13sp" />
<TextView
android:id="@+id/emailTextView"
android:layout_width="0dp"
android:layout_height="22dp"
android:layout_weight="2"
android:gravity="center"
android:textColor="#000"
android:background="@drawable/cell_shape"
android:textSize="13sp" />
</TableRow>
Step 4: Add activity_main.xml code as follow.
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent">
<ScrollView
android:id="@+id/scrollview"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<TableLayout
android:id="@+id/tableLayout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="30dp"
android:layout_marginRight="30dp"
android:layout_marginTop="30dp"
android:stretchColumns="*">
<TableRow
android:id="@+id/removeButton"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<TextView
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="Name"
android:gravity="center"
android:textSize="16sp"
android:background="@drawable/header_shape"
android:textStyle="bold" />
<TextView
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="Phone"
android:gravity="center"
android:background="@drawable/header_shape"
android:textSize="16sp"
android:textStyle="bold" />
<TextView
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="2"
android:text="Email"
android:gravity="center"
android:textSize="16sp"
android:background="@drawable/header_shape"
android:textStyle="bold" />
</TableRow>
</TableLayout>
</ScrollView>
<LinearLayout
android:id="@+id/box"
android:visibility="gone"
android:layout_width="250dp"
android:layout_height="188dp"
android:layout_marginTop="230dp"
android:orientation="vertical"
android:background="@drawable/header_shape"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="@+id/scrollview">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="3"
android:orientation="horizontal">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textSize="25dp"
android:textStyle="bold"
android:gravity="center"
android:background="@drawable/header_shape"
android:text="Contact Details">
</TextView>
<EditText
android:id="@+id/numberEditText"
android:layout_width="250dp"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_weight="1"
android:hint="Name"
android:background="@drawable/cell_shape"
android:textAlignment="center" />
<EditText
android:id="@+id/nameEditText"
android:layout_width="250dp"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_weight="1"
android:hint="Number"
android:background="@drawable/cell_shape"
android:inputType="phone"
android:textAlignment="center" />
<EditText
android:id="@+id/emailEditText"
android:layout_width="250dp"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_weight="1"
android:hint="Email"
android:background="@drawable/cell_shape"
android:textAlignment="center" />
</LinearLayout>
</LinearLayout>
<Button
android:id="@+id/addButton"
android:background="@color/grey"
android:layout_width="match_parent"
android:layout_height="40dp"
android:layout_gravity="center"
android:text="Add" />
</LinearLayout>
<com.google.android.material.floatingactionbutton.FloatingActionButton
android:id="@+id/floatingActionButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="36dp"
android:clickable="true"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.498"
app:layout_constraintStart_toStartOf="parent"
app:srcCompat="@android:drawable/ic_input_add" />
</androidx.constraintlayout.widget.ConstraintLayout>
Step 5: Add MainActivity.kt code as follows
MainActivity.kt
}
import android.app.Activity
import android.content.ContentValues
import android.database.sqlite.SQLiteDatabase
import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import android.widget.Button
import android.widget.EditText
import android.widget.LinearLayout
import android.widget.TableLayout
import android.widget.TableRow
import android.widget.TextView
import com.google.android.material.floatingactionbutton.FloatingActionButton
class MainActivity : Activity() {
private lateinit var dbHelper: ContactsDatabaseHelper
private lateinit var db: SQLiteDatabase
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
dbHelper = ContactsDatabaseHelper(this)
db = dbHelper.writableDatabase
val cursor1 = db.rawQuery("SELECT name FROM sqlite_master WHERE type='table' AND name='contacts'", null)
val tableExists = cursor1.moveToFirst()
cursor1.close()
if (tableExists) {
// Table exists, do something
} else {
// Table does not exist, do something else
val CREATE_CONTACTS_TABLE = "CREATE TABLE contacts (" +
"_id INTEGER PRIMARY KEY," +
"name TEXT," +
"number TEXT," +
"email TEXT" +
");"
db.execSQL(CREATE_CONTACTS_TABLE)
}
val addButton = findViewById<Button>(R.id.addButton)
val nameEditText = findViewById<EditText>(R.id.nameEditText)
val numberEditText = findViewById<EditText>(R.id.numberEditText)
val emailEditText = findViewById<EditText>(R.id.emailEditText)
val tableLayout = findViewById<TableLayout>(R.id.tableLayout)
val box = findViewById<LinearLayout>(R.id.box)
val new: FloatingActionButton = findViewById(R.id.floatingActionButton)
var isRotated = false
new.setOnClickListener {
if (isRotated) {
new.rotation = 0.0f
isRotated = false
box.visibility = View.GONE
} else {
new.rotation = 45.0f
isRotated = true
box.visibility = View.VISIBLE
}
}
addButton.setOnClickListener {
val name = nameEditText.text.toString()
val number = numberEditText.text.toString()
val email = emailEditText.text.toString()
val values = ContentValues().apply {
put("name", name)
put("number", number)
put("email", email)
}
db.insert("contacts", null, values)
updateTableView()
}
val cursor = db.query("contacts", arrayOf("_id", "name", "number", "email"), null, null, null, null, null)
while (cursor.moveToNext()) {
val id = cursor.getLong(0)
val name = cursor.getString(1)
val number = cursor.getString(2)
val email = cursor.getString(3)
val tableRow = LayoutInflater.from(this).inflate(R.layout.table_row, null) as TableRow
tableRow.findViewById<TextView>(R.id.nameTextView).text = name
tableRow.findViewById<TextView>(R.id.numberTextView).text = number
tableRow.findViewById<TextView>(R.id.emailTextView).text = email
val removeButton = tableRow.findViewById<TableRow>(R.id.removeButton)
removeButton.setOnClickListener {
db.delete("contacts", "_id = ?", arrayOf(id.toString()))
tableLayout.removeView(tableRow)
}
tableLayout.addView(tableRow)
}
cursor.close()
}
override fun onDestroy() {
super.onDestroy()
db.close()
}
private fun updateTableView() {
val tableLayout = findViewById<TableLayout>(R.id.tableLayout)
// Clear the current rows from the table view
for (i in tableLayout.childCount - 1 downTo 1) {
tableLayout.removeView(tableLayout.getChildAt(i))
}
// Query the database to get all rows
val cursor = db.rawQuery("SELECT * FROM contacts", null)
// Iterate over the rows in the cursor and add them to the table view
while (cursor.moveToNext()) {
val name = cursor.getString(cursor.getColumnIndexOrThrow("name"))
val number = cursor.getString(cursor.getColumnIndexOrThrow("number"))
val email = cursor.getString(cursor.getColumnIndexOrThrow("email"))
val tableRow = LayoutInflater.from(this).inflate(R.layout.table_row, null) as TableRow
tableRow.findViewById<TextView>(R.id.nameTextView).text = name
tableRow.findViewById<TextView>(R.id.numberTextView).text = number
tableRow.findViewById<TextView>(R.id.emailTextView).text = email
val removeButton = tableRow.findViewById<TableRow>(R.id.removeButton)
removeButton.setOnClickListener {
// Remove the row from the database
db.delete("contacts", "name = ?", arrayOf(name))
// Remove the row from the table view
tableLayout.removeView(tableRow)
}
tableLayout.addView(tableRow)
}
// Close the cursor to free up resources
cursor.close()
}
Step 6: Now create a new Kotlin class/file name as ContactsDatabaseHelper and add the following code to it.
ContactsDatabaseHelper
import android.content.Context
import android.database.sqlite.SQLiteDatabase
import android.database.sqlite.SQLiteOpenHelper
class ContactsDatabaseHelper(context: Context) : SQLiteOpenHelper(context, "contacts.db", null, 1) {
override fun onCreate(db: SQLiteDatabase) {
val CREATE_TABLE_QUERY = "CREATE TABLE my_table (id INTEGER PRIMARY KEY AUTOINCREMENT, name TEXT, number TEXT, email TEXT)"
db.execSQL(CREATE_TABLE_QUERY) }
override fun onUpgrade(db: SQLiteDatabase, oldVersion: Int, newVersion: Int) {
// Handle database upgrades
}
}
Step 7: Now create a drawable resource file and give the name cell_shape.xml and add the following code to it.
cell_shape.xml
<?xml version="1.0" encoding="utf-8"?>
<shape
xmlns:android="http://schemas.android.com/apk/res/android"
android:shape= "rectangle" >
<solid android:color="@color/white"/>
<stroke android:width="1dp" android:color="#000000"/>
</shape>
Step 8: Now create a drawable resource file and give the name header_shape.xml and add the following code to it.
header_shape
<?xml version="1.0" encoding="utf-8"?>
<shape
xmlns:android="http://schemas.android.com/apk/res/android"
android:shape= "rectangle" >
<solid android:color="@color/white"/>
<stroke android:width="1dp" android:color="#000000"/>
</shape>
And done. Now run your app
Conclusion:
Congratulations, you have now learned how to use SQLite in Android Studio with Kotlin to create a simple note-taking app! With this foundation, you can now expand your knowledge and build even more complex apps that utilize SQLite to store and retrieve data.