Task Manager App (Kotlin & SQLite)

This is a simple Task Manager application built using Kotlin, Android Studio, and SQLite for local storage. Below are the key code snippets used in the implementation.

DBHelper.kt - SQLite Database Helper
package com.example.taskmanager import android.content.ContentValues import android.content.Context import android.database.sqlite.SQLiteDatabase import android.database.sqlite.SQLiteOpenHelper class DBHelper(context: Context) : SQLiteOpenHelper(context, "TasksDB1", null, 1) { override fun onCreate(db: SQLiteDatabase) { val createTable = "CREATE TABLE Tasks (id INTEGER PRIMARY KEY, name TEXT, dueDate TEXT, priority TEXT, category TEXT)" db.execSQL(createTable) } override fun onUpgrade(db: SQLiteDatabase, oldVersion: Int, newVersion: Int) { db.execSQL("DROP TABLE IF EXISTS Tasks") onCreate(db) } // Insert task with category fun insertTask(name: String, dueDate: String, priority: String, category: String) { val db = writableDatabase val values = ContentValues().apply { put("name", name) put("dueDate", dueDate) put("priority", priority) put("category", category) // Insert category } db.insert("Tasks", null, values) } // Get tasks grouped by category fun getTasksGroupedByCategory(): List<Pair<String, List<Task>>> { val tasks = mutableListOf<Task>() val db = readableDatabase val cursor = db.rawQuery("SELECT * FROM Tasks ORDER BY priority", null) while (cursor.moveToNext()) { val task = Task( cursor.getInt(0), cursor.getString(1), cursor.getString(2), cursor.getString(3), cursor.getString(4) // Retrieve category from database ) tasks.add(task) } cursor.close() // Group tasks by category (priority) return tasks.groupBy { it.category }.map { it.key to it.value } } // Delete task fun deleteTask(id: Int) { val db = writableDatabase db.delete("Tasks", "id=?", arrayOf(id.toString())) } }
Task.kt - Data Model
package com.example.taskmanager data class Task( val id: Int, val name: String, val dueDate: String, val priority: String, val category: String // Added category as a field )
TaskAdapter.kt - RecyclerView Adapter
package com.example.taskmanager import android.view.LayoutInflater import android.view.View import android.view.ViewGroup import android.widget.Button import android.widget.TextView import androidx.recyclerview.widget.RecyclerView class TaskAdapter(private val tasks: List<Task>, private val dbHelper: DBHelper, private val refreshList: () -> Unit) : RecyclerView.Adapter<TaskAdapter.TaskViewHolder>() { inner class TaskViewHolder(val view: View) : RecyclerView.ViewHolder(view) override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): TaskViewHolder { val view = LayoutInflater.from(parent.context).inflate(R.layout.task_item, parent, false) return TaskViewHolder(view) } override fun onBindViewHolder(holder: TaskViewHolder, position: Int) { val task = tasks[position] holder.view.findViewById<TextView>(R.id.taskName).text = task.name holder.view.findViewById<TextView>(R.id.taskDueDate).text = task.dueDate holder.view.findViewById<TextView>(R.id.taskPriority).text = task.priority holder.view.findViewById<TextView>(R.id.taskCategory).text = task.category // Display category holder.view.findViewById<Button>(R.id.deleteTask).setOnClickListener { dbHelper.deleteTask(task.id) refreshList() } } override fun getItemCount() = tasks.size }
MainActivity.kt - Main Activity
package com.example.taskmanager import android.os.Bundle import android.widget.* import androidx.appcompat.app.AppCompatActivity import androidx.recyclerview.widget.LinearLayoutManager import androidx.recyclerview.widget.RecyclerView class MainActivity : AppCompatActivity() { private lateinit var dbHelper: DBHelper private lateinit var recyclerView: RecyclerView private lateinit var taskPrioritySpinner: Spinner override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) setContentView(R.layout.activity_main) dbHelper = DBHelper(this) recyclerView = findViewById(R.id.recyclerView) recyclerView.layoutManager = LinearLayoutManager(this) // Initialize Spinner and populate with categories taskPrioritySpinner = findViewById(R.id.taskPriorityInput) val categories = arrayOf("Work", "Personal", "Urgent") // Task categories val adapter = ArrayAdapter(this, android.R.layout.simple_spinner_item, categories) adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item) taskPrioritySpinner.adapter = adapter // Add Task button click listener findViewById<Button>(R.id.addTaskButton).setOnClickListener { val name = findViewById<EditText>(R.id.taskNameInput).text.toString() val dueDate = findViewById<EditText>(R.id.taskDueDateInput).text.toString() val priority = taskPrioritySpinner.selectedItem.toString() val category = taskPrioritySpinner.selectedItem.toString() // Category is the same as priority here // Insert task if name and due date are not empty if (name.isNotEmpty() && dueDate.isNotEmpty()) { dbHelper.insertTask(name, dueDate, priority, category) refreshTaskList() } } // Refresh task list refreshTaskList() } private fun refreshTaskList() { val categorizedTasks = dbHelper.getTasksGroupedByCategory() recyclerView.adapter = CategoryTaskAdapter(categorizedTasks, dbHelper, this::refreshTaskList) } }
CategoryTaskAdapter.kt - RecyclerView Adapter for Categories
package com.example.taskmanager import android.view.LayoutInflater import android.view.View import android.view.ViewGroup import android.widget.Button import android.widget.TextView import androidx.recyclerview.widget.RecyclerView class CategoryTaskAdapter( private val categorizedTasks: List<Pair<String, List<Task>>>, private val dbHelper: DBHelper, private val refreshList: () -> Unit ) : RecyclerView.Adapter<RecyclerView.ViewHolder>() { private val TYPE_CATEGORY = 0 private val TYPE_TASK = 1 private val itemList = mutableListOf<Any>() init { for ((category, tasks) in categorizedTasks) { itemList.add(category) // Add category as a header itemList.addAll(tasks) // Add tasks under that category } } override fun getItemViewType(position: Int): Int { return if (itemList[position] is String) TYPE_CATEGORY else TYPE_TASK } override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): RecyclerView.ViewHolder { return if (viewType == TYPE_CATEGORY) { val view = LayoutInflater.from(parent.context).inflate(R.layout.category_item, parent, false) CategoryViewHolder(view) } else { val view = LayoutInflater.from(parent.context).inflate(R.layout.task_item, parent, false) TaskViewHolder(view) } } override fun onBindViewHolder(holder: RecyclerView.ViewHolder, position: Int) { if (holder is CategoryViewHolder) { holder.categoryTitle.text = itemList[position] as String } else if (holder is TaskViewHolder) { val task = itemList[position] as Task holder.taskName.text = task.name holder.taskDueDate.text = task.dueDate holder.taskPriority.text = task.priority holder.deleteTask.setOnClickListener { dbHelper.deleteTask(task.id) refreshList() } } } override fun getItemCount() = itemList.size inner class CategoryViewHolder(view: View) : RecyclerView.ViewHolder(view) { val categoryTitle: TextView = view.findViewById(R.id.categoryTitle) } inner class TaskViewHolder(view: View) : RecyclerView.ViewHolder(view) { val taskName: TextView = view.findViewById(R.id.taskName) val taskDueDate: TextView = view.findViewById(R.id.taskDueDate) val taskPriority: TextView = view.findViewById(R.id.taskPriority) val deleteTask: Button = view.findViewById(R.id.deleteTask) } }
activity_main.xml - Main Layout
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" android:padding="16dp"> <EditText android:id="@+id/taskNameInput" android:layout_width="match_parent" android:layout_height="wrap_content" android:hint="Task Name" /> <EditText android:id="@+id/taskDueDateInput" android:layout_width="match_parent" android:layout_height="wrap_content" android:hint="Due Date" /> <Spinner android:id="@+id/taskPriorityInput" android:layout_width="match_parent" android:layout_height="wrap_content"/> <Button android:id="@+id/addTaskButton" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Add Task"/> <androidx.recyclerview.widget.RecyclerView android:id="@+id/recyclerView" android:layout_width="match_parent" android:layout_height="match_parent"/> </LinearLayout>
category_item.xml - Category Header Layout
<TextView xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/categoryTitle" android:layout_width="match_parent" android:layout_height="wrap_content" android:padding="10dp" android:textSize="18sp" android:textStyle="bold"/>
colors.xml - Color Resources
<resources> <color name="primaryColor">#6200EE</color> <color name="backgroundGray">#F5F5F5</color> </resources>
strings.xml - String Resources
<resources> <string name="app_name">Task Manager</string> <string-array name="priority_array"> <item>Low</item> <item>Medium</item> <item>High</item> </string-array> </resources>
task_item.xml - Task Item Layout
<?xml version="1.0" encoding="utf-8"?> <androidx.cardview.widget.CardView xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_margin="8dp"> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="vertical" android:padding="12dp"> <TextView android:id="@+id/taskName" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="Task Name" android:textSize="16sp" /> <TextView android:id="@+id/taskDueDate" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="Due Date" android:textSize="14sp" /> <TextView android:id="@+id/taskPriority" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="Priority" android:textSize="14sp" /> <Button android:id="@+id/deleteTask" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Delete Task"/> </LinearLayout> </androidx.cardview.widget.CardView>