Room is a persistence library that is part of the Android Jetpack. The room provides an abstraction layer over the SQLite to allow fluent DB access while harnessing the full power of SQLite. With the help of RoomDb, we can quickly create an SQLite database and perform the crud operations very easily.
We will be building a simple to-do task application in which we will perform a crud operation. Let’s start to build a to-do app with the help of Room Db.
Before starting the work on the app, let’s have a quick overview of the three main components of Room Db.
Entity: Entity is a model class that is annotated with @Entity annotation. This class is having variables that will be our columns and the class is our table.
DAO: Stands for Data Access Object. It is an interface that defines all the operations that we need to perform in our database e.g insert, delete update and fetch the records.
Database: It is an abstract class where we define all our entities.
1- Open the Android Studio and create a new project
2- Go to the inside app and open the build.gradle.xml file and add the below dependencies and sync your project
3- Navigate to res->values and open the strings.xml file and add the below strings that will use in the demo.
<resources>
<string name="app_name">RoomDbExample</string>
<string name="str_demo">Room Db Example</string>
<string name="str_add_task">Add Task</string>
<string name="str_view_task">View Tasks</string>
<string name="str_dev_msg">Thanks for reading my tutorial, ProCodeGuru always try to give simple and smart solution to the learners.</string>
<string name="website">procodeguru.com</string>
<string name="hint_task_title">Task title</string>
<string name="hint_task_description">Task description</string>
<string name="str_save">Save</string>
<string name="str_edit">Edit</string>
<string name="str_del">Delete</string>
</resources>
4- After that, open the colors.xml file and add the below code
<?xml version="1.0" encoding="utf-8"?>
<resources>
<color name="purple_200">#FFBB86FC</color>
<color name="purple_500">#212020</color>
<color name="purple_700">#131313</color>
<color name="teal_200">#FF03DAC5</color>
<color name="teal_700">#FF018786</color>
<color name="black">#FF000000</color>
<color name="white">#FFFFFFFF</color>
<!-- App colors which will use for demo -->
<color name="color_bg">#212020</color>
<color name="color_separator">#909090</color>
<color name="color_btn">#CE6A07</color>
<color name="color_card_bg">#2C2A2A</color>
</resources>
5- Create a DB structure for the demo. Create an EntityTask.java class and annotated it with @Entity. EntityTask is a table of our DB. Id (Autogenerated), Title, and description variables are defined in this class as columns of the table. We have more annotations that we can use. But for now, we’re going to use @Column and @Primaykey. I will explain other annotations in my next tutorial on Room Db. Add the below code.
package com.example.roomdbexample;
import androidx.room.ColumnInfo;
import androidx.room.Entity;
import androidx.room.PrimaryKey;
@Entity
public class EntityTask {
@PrimaryKey(autoGenerate = true)
public int id;
@ColumnInfo(name = "task_title")
public String taskTitle;
@ColumnInfo(name = "task_description")
public String taskDescription;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getTaskTitle() {
return taskTitle;
}
public void setTaskTitle(String taskTitle) {
this.taskTitle = taskTitle;
}
public String getTaskDescription() {
return taskDescription;
}
public void setTaskDescription(String taskDescription) {
this.taskDescription = taskDescription;
}
}
6- Create a DAO component that will responsible to perform CRUD operation. Create a new Java class TaskDao.java. In this class, you can add more SQL queries according to your requirements. Add the below code.
package com.example.roomdbexample;
import androidx.room.Dao;
import androidx.room.Delete;
import androidx.room.Insert;
import androidx.room.Query;
import androidx.room.Update;
import java.util.List;
@Dao
public interface TaskDao {
@Query("SELECT * FROM entitytask")
List<EntityTask> getAll();
@Query("SELECT * FROM entitytask WHERE id =:id")
EntityTask getTaskById(int id);
@Insert
void insert(EntityTask entityTask);
@Delete
void delete(EntityTask entityTask);
@Update
void update(EntityTask entityTask);
}
7- Last Room Db component is a Database component. In this abstract class, we will add all the entities classes and will define an abstract method that will return the TaskDao. Create an AppDatabase.java class and add the below code. In the future, if you do any schema change e.g add or remove a column in any entity class then you have to increase the DB version.
package com.example.roomdbexample;
import androidx.room.Database;
import androidx.room.RoomDatabase;
@Database(entities = {EntityTask.class}, version = 1)
public abstract class AppDatabase extends RoomDatabase {
public abstract TaskDao taskDao();
}
8- Create a new java class called AppController.java. This class is primarily will use for the initialization of the Room DB instance. With the help of this class, we can use the Room DB instance throughout the project. Add the below code.
package com.example.roomdbexample;
import androidx.room.Database;
import androidx.room.RoomDatabase;
@Database(entities = {EntityTask.class}, version = 1)
public abstract class AppDatabase extends RoomDatabase {
public abstract TaskDao taskDao();
}
9- Create a new interface class called CallbackTask.java. Add the below code
package com.example.roomdbexample;
public interface CallbackTask {
void onDelete(int position);
void onEdit(int position);
}
10- We will start design on add a task layout. Go to res->layout and create a layout resource file called activity_add_task.xml. Add the below code in it. If you’re not familiar with the Adapter or RecyclerView, you can read the RecyclerView series.
<?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:background="@color/color_bg"
android:layout_height="match_parent">
<TextView
android:id="@+id/tvDemo"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="20dp"
android:text="@string/str_add_task"
android:textColor="@color/white"
android:inputType="text"
android:imeOptions="actionNext"
android:textSize="22sp"
app:layout_constraintEnd_toEndOf="@id/rightGuideline"
app:layout_constraintStart_toStartOf="@+id/leftGuideline"
app:layout_constraintTop_toTopOf="parent" />
<View
android:id="@+id/viewSeparator"
android:layout_width="0dp"
android:layout_height="1dp"
android:layout_marginTop="10dp"
android:background="@color/color_separator"
app:layout_constraintEnd_toEndOf="@id/rightGuideline"
app:layout_constraintStart_toStartOf="@+id/leftGuideline"
app:layout_constraintTop_toBottomOf="@+id/tvDemo" />
<com.google.android.material.textfield.TextInputLayout
android:id="@+id/txtInputTitle"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginTop="30dp"
app:layout_constraintEnd_toEndOf="@+id/rightGuideline"
app:layout_constraintStart_toStartOf="@+id/leftGuideline"
app:layout_constraintTop_toBottomOf="@+id/viewSeparator">
<EditText
android:id="@+id/edtTaskTitle"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@drawable/bg_edt_txt"
android:hint="@string/hint_task_title">
</EditText>
</com.google.android.material.textfield.TextInputLayout>
<com.google.android.material.textfield.TextInputLayout
android:id="@+id/txtInputDescription"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
app:layout_constraintEnd_toEndOf="@+id/rightGuideline"
app:layout_constraintStart_toStartOf="@+id/leftGuideline"
app:layout_constraintTop_toBottomOf="@+id/txtInputTitle">
<EditText
android:id="@+id/edtTaskDescription"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@drawable/bg_edt_txt"
android:gravity="top"
android:hint="@string/hint_task_description"
android:minLines="5">
</EditText>
</com.google.android.material.textfield.TextInputLayout>
<androidx.appcompat.widget.AppCompatButton
android:id="@+id/btnSave"
android:layout_width="150dp"
android:layout_height="wrap_content"
android:layout_marginTop="20dp"
android:background="@drawable/bg_btn"
android:text="@string/str_save"
android:textAllCaps="false"
android:textColor="@color/white"
android:textSize="16sp"
android:onClick="saveTask"
app:layout_constraintEnd_toEndOf="@+id/rightGuideline"
app:layout_constraintStart_toStartOf="@id/leftGuideline"
app:layout_constraintTop_toBottomOf="@+id/txtInputDescription" />
<androidx.constraintlayout.widget.Guideline
android:id="@+id/leftGuideline"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical"
app:layout_constraintGuide_begin="20dp" />
<androidx.constraintlayout.widget.Guideline
android:id="@+id/rightGuideline"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical"
app:layout_constraintGuide_end="20dp" />
</androidx.constraintlayout.widget.ConstraintLayout>
11- Create a new java class called AddTaskActivity.java and add the below code. I applied validation to the input fields. you can customize it according to your logic or requirements. Read comments to understand the code.
package com.example.roomdbexample;
import android.os.Bundle;
import android.view.View;
import android.widget.EditText;
import android.widget.Toast;
import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;
public class AddTaskActivity extends AppCompatActivity {
private EditText edtTaskTitle;
private EditText edtTaskDescription;
private boolean isEdt = false;
private EntityTask entityTask;
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_add_task);
isEdt = getIntent().getBooleanExtra("forEdt", false);
initComponents();
}
/**
* init components and also check if this screen opened for
* edit the task then get the task id from put extra value
*/
private void initComponents() {
edtTaskTitle = findViewById(R.id.edtTaskTitle);
edtTaskDescription = findViewById(R.id.edtTaskDescription);
if (isEdt) {
int taskId = getIntent().getIntExtra("taskId", 0);
if (taskId > 0) {
entityTask = AppController.getInstance().appDatabase.taskDao().getTaskById(taskId);
edtTaskDescription.setText(entityTask.getTaskDescription());
edtTaskTitle.setText(entityTask.getTaskTitle());
}
}
}
/**
* validate the values if these values are validated true then save or edit the result in room db.
* @param view button
*/
public void saveTask(View view) {
if (edtTaskTitle.getText().toString().trim().isEmpty()) {
Toast.makeText(this, "Task title required", Toast.LENGTH_LONG).show();
return;
}
if (edtTaskTitle.getText().toString().trim().length() < 6) {
Toast.makeText(this, "Task title length should be greater than 5", Toast.LENGTH_LONG).show();
return;
}
if (edtTaskDescription.getText().toString().trim().isEmpty()) {
Toast.makeText(this, "Task description required", Toast.LENGTH_LONG).show();
return;
}
if (edtTaskDescription.getText().toString().trim().length() < 6) {
Toast.makeText(this, "Task description length should be greater than 5", Toast.LENGTH_LONG).show();
return;
}
if (!isEdt) {
entityTask = new EntityTask();
}
entityTask.setTaskTitle(edtTaskTitle.getText().toString().trim());
entityTask.setTaskDescription(edtTaskDescription.getText().toString().trim());
if (isEdt) {
AppController.getInstance().appDatabase.taskDao().update(entityTask);
Toast.makeText(this, "Congratulation, Task successfully updated", Toast.LENGTH_LONG).show();
finish();
} else {
AppController.getInstance().appDatabase.taskDao().insert(entityTask);
Toast.makeText(this, "Congratulation, Task successfully saved", Toast.LENGTH_LONG).show();
edtTaskTitle.getText().clear();
edtTaskDescription.getText().clear();
}
}
}
12- Go to res->layout folder and create a new layout resource file called adapter_task.xml and add the below code.
<?xml version="1.0" encoding="utf-8"?>
<androidx.cardview.widget.CardView 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="wrap_content"
app:cardBackgroundColor="@color/color_card_bg"
app:cardCornerRadius="5dp"
app:cardUseCompatPadding="true">
<androidx.constraintlayout.widget.ConstraintLayout
android:layout_width="match_parent"
android:layout_margin="10dp"
android:minHeight="120dp"
android:layout_height="match_parent">
<TextView
android:id="@+id/tvTitle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:ellipsize="end"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintTop_toTopOf="parent"
android:maxLines="2"
android:text="@string/app_name"
android:textColor="@color/white"
android:textSize="18sp" />
<TextView
android:id="@+id/tvDescription"
android:layout_width="0dp"
android:layout_height="0dp"
android:ellipsize="end"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintTop_toBottomOf="@+id/tvTitle"
app:layout_constraintBottom_toTopOf="@+id/btnEdt"
android:maxLines="2"
android:text="@string/app_name"
android:textColor="@color/white"
android:textSize="16sp" />
<androidx.appcompat.widget.AppCompatButton
android:id="@+id/btnEdt"
android:text="@string/str_edit"
android:background="@android:color/transparent"
android:textStyle="bold"
android:textSize="16sp"
android:textColor="@color/color_btn"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintStart_toStartOf="parent"
android:textAllCaps="false"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
<androidx.appcompat.widget.AppCompatButton
android:id="@+id/btnDel"
android:text="@string/str_del"
android:background="@android:color/transparent"
android:textStyle="bold"
android:textSize="16sp"
android:textColor="@color/white"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintStart_toEndOf="@+id/btnEdt"
android:textAllCaps="false"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
<TextView
android:id="@+id/tvCounter"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintTop_toTopOf="@+id/btnDel"
app:layout_constraintBottom_toBottomOf="@+id/btnDel"
android:text="1"
android:background="@drawable/bg_counter"
android:gravity="center"
android:textColor="@color/white"
android:textSize="18sp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
</androidx.constraintlayout.widget.ConstraintLayout>
</androidx.cardview.widget.CardView>
13- Create an AdapterTask.java class and add the below code
package com.example.roomdbexample;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
import androidx.annotation.NonNull;
import androidx.appcompat.widget.AppCompatButton;
import androidx.recyclerview.widget.RecyclerView;
import java.util.List;
public class AdapterTask extends RecyclerView.Adapter<RecyclerView.ViewHolder> {
private List<EntityTask> entityTaskList;
private CallbackTask callbackTask;
AdapterTask(List<EntityTask> entityTaskList, CallbackTask callbackTask) {
this.entityTaskList = entityTaskList;
this.callbackTask = callbackTask;
}
private class TaskViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener {
private TextView tvTitle;
private TextView tvDescription;
private TextView tvCounter;
private AppCompatButton btnEdt;
private AppCompatButton btnDel;
public TaskViewHolder(View itemView) {
super(itemView);
tvTitle = itemView.findViewById(R.id.tvTitle);
tvDescription = itemView.findViewById(R.id.tvDescription);
tvCounter = itemView.findViewById(R.id.tvCounter);
btnEdt = itemView.findViewById(R.id.btnEdt);
btnDel = itemView.findViewById(R.id.btnDel);
btnDel.setOnClickListener(this);
btnEdt.setOnClickListener(this);
}
@Override
public void onClick(View v) {
if (v.getId() == R.id.btnDel)
callbackTask.onDelete(getAdapterPosition());
else if (v.getId() == R.id.btnEdt)
callbackTask.onEdit(getAdapterPosition());
}
}
@NonNull
@Override
public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
View v1 = LayoutInflater.from(parent.getContext()).inflate(R.layout.adapter_task, parent, false);
return new TaskViewHolder(v1);
}
@Override
public void onBindViewHolder(@NonNull RecyclerView.ViewHolder holder, int position) {
TaskViewHolder taskViewHolder = (TaskViewHolder) holder;
EntityTask entityTask = entityTaskList.get(position);
taskViewHolder.tvTitle.setText(entityTask.getTaskTitle());
taskViewHolder.tvDescription.setText(entityTask.getTaskDescription());
taskViewHolder.tvCounter.setText(String.format("%d", (position + 1)));
}
@Override
public int getItemCount() {
return entityTaskList.size();
}
public void setDataToAdapter(List<EntityTask> entityTaskList) {
this.entityTaskList = entityTaskList;
notifyDataSetChanged();
}
}
14- Go to res->layout folder and create a new layout resource file called activity_view_task.xml and add the below code
<?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:background="@color/color_bg"
android:layout_height="match_parent">
<TextView
android:id="@+id/tvDemo"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="20dp"
android:text="@string/str_view_task"
android:textColor="@color/white"
android:textSize="22sp"
app:layout_constraintEnd_toEndOf="@id/rightGuideline"
app:layout_constraintStart_toStartOf="@+id/leftGuideline"
app:layout_constraintTop_toTopOf="parent" />
<View
android:id="@+id/viewSeparator"
android:layout_width="0dp"
android:layout_height="1dp"
android:layout_marginTop="10dp"
android:background="@color/color_separator"
app:layout_constraintEnd_toEndOf="@id/rightGuideline"
app:layout_constraintStart_toStartOf="@+id/leftGuideline"
app:layout_constraintTop_toBottomOf="@+id/tvDemo" />
<androidx.recyclerview.widget.RecyclerView
android:id="@+id/rcvTaskListing"
android:layout_width="0dp"
android:layout_height="0dp"
android:layout_marginTop="20dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="@id/rightGuideline"
app:layout_constraintStart_toStartOf="@id/leftGuideline"
app:layout_constraintTop_toBottomOf="@+id/viewSeparator" />
<androidx.constraintlayout.widget.Guideline
android:id="@+id/leftGuideline"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical"
app:layout_constraintGuide_begin="20dp" />
<androidx.constraintlayout.widget.Guideline
android:id="@+id/rightGuideline"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical"
app:layout_constraintGuide_end="20dp" />
</androidx.constraintlayout.widget.ConstraintLayout>
15- Create a new ViewTaskActivity.java file and add the below code. I added comments that will help to understand the code.
package com.example.roomdbexample;
import android.content.Intent;
import android.os.Bundle;
import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import java.util.ArrayList;
import java.util.List;
public class ViewTaskActivity extends AppCompatActivity implements CallbackTask {
private RecyclerView rcvTaskListing;
private AdapterTask adapterTask;
private List<EntityTask> entityTaskList;
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_view_task);
initComponents();
initRcv();
}
/**
* init the components
*/
private void initComponents() {
entityTaskList = new ArrayList<>();
rcvTaskListing = findViewById(R.id.rcvTaskListing);
adapterTask = new AdapterTask(entityTaskList, this);
rcvTaskListing.setAdapter(adapterTask);
}
/**
* init the recyclerview and adapter class. Create the object of layout manager
* and set the orientation of the recyclerview
*/
private void initRcv() {
LinearLayoutManager linearLayoutManager = new LinearLayoutManager(this);
linearLayoutManager.setOrientation(LinearLayoutManager.VERTICAL);
rcvTaskListing.setLayoutManager(linearLayoutManager);
rcvTaskListing.setAdapter(adapterTask);
}
/**
* query for local db saved tasks.
*/
private void fetchLocalTaskList() {
entityTaskList = AppController.getInstance().appDatabase.taskDao().getAll();
adapterTask.setDataToAdapter(entityTaskList);
}
@Override
public void onDelete(int position) {
EntityTask entityTask = entityTaskList.get(position);
AppController.getInstance().appDatabase.taskDao().delete(entityTask);
entityTaskList.remove(entityTask);
adapterTask.notifyItemRemoved(position);
}
@Override
public void onEdit(int position) {
Intent intent = new Intent(new Intent(this, AddTaskActivity.class));
intent.putExtra("forEdt",true);
intent.putExtra("taskId",entityTaskList.get(position).getId());
startActivity(intent);
}
@Override
protected void onResume() {
super.onResume();
fetchLocalTaskList();
}
}
16- Go to res->layout folder and open the activity_main.xml file and add the below code
<?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"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:background="@color/color_bg"
android:layout_height="match_parent"
tools:context=".MainActivity">
<TextView
android:id="@+id/tvDemo"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="20dp"
android:text="@string/str_demo"
android:textColor="@color/white"
android:textSize="22sp"
app:layout_constraintEnd_toEndOf="@id/rightGuideline"
app:layout_constraintStart_toStartOf="@+id/leftGuideline"
app:layout_constraintTop_toTopOf="parent" />
<View
android:id="@+id/viewSeparator"
android:layout_width="0dp"
android:layout_height="1dp"
android:layout_marginTop="10dp"
android:background="@color/color_separator"
app:layout_constraintEnd_toEndOf="@id/rightGuideline"
app:layout_constraintStart_toStartOf="@+id/leftGuideline"
app:layout_constraintTop_toBottomOf="@+id/tvDemo" />
<androidx.appcompat.widget.AppCompatButton
android:id="@+id/btnAddTask"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginTop="30dp"
android:background="@drawable/bg_btn"
android:text="@string/str_add_task"
android:onClick="openAddTask"
android:textAllCaps="false"
android:textColor="@color/white"
android:textSize="16sp"
app:layout_constraintEnd_toEndOf="@+id/rightGuideline"
app:layout_constraintStart_toStartOf="@id/leftGuideline"
app:layout_constraintTop_toBottomOf="@+id/viewSeparator" />
<androidx.appcompat.widget.AppCompatButton
android:id="@+id/btnViewTask"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:background="@drawable/bg_btn"
android:text="@string/str_view_task"
android:textAllCaps="false"
android:onClick="openViewTask"
android:textColor="@color/white"
android:textSize="16sp"
app:layout_constraintEnd_toEndOf="@+id/rightGuideline"
app:layout_constraintStart_toStartOf="@id/leftGuideline"
app:layout_constraintTop_toBottomOf="@+id/btnAddTask" />
<TextView
android:id="@+id/tvDevMsg"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginTop="20dp"
android:text="@string/str_dev_msg"
android:textColor="@color/white"
android:textSize="18sp"
app:layout_constraintEnd_toEndOf="@id/rightGuideline"
app:layout_constraintStart_toStartOf="@+id/leftGuideline"
app:layout_constraintTop_toBottomOf="@+id/btnViewTask" />
<TextView
android:id="@+id/tvWebsite"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="20dp"
android:text="@string/website"
android:textColor="@color/white"
android:layout_marginBottom="20dp"
android:textSize="22sp"
app:layout_constraintEnd_toEndOf="@id/rightGuideline"
app:layout_constraintStart_toStartOf="@+id/leftGuideline"
app:layout_constraintBottom_toBottomOf="parent" />
<androidx.constraintlayout.widget.Guideline
android:id="@+id/leftGuideline"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical"
app:layout_constraintGuide_begin="20dp" />
<androidx.constraintlayout.widget.Guideline
android:id="@+id/rightGuideline"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical"
app:layout_constraintGuide_end="20dp" />
</androidx.constraintlayout.widget.ConstraintLayout>
17- Open the MainActivity.java file and add the below code. Added comments to understand the code.
package com.example.roomdbexample;
import androidx.appcompat.app.AppCompatActivity;
import androidx.room.Room;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
public void openAddTask(View view){
startActivity(new Intent(this,AddTaskActivity.class));
}
public void openViewTask(View view){
startActivity(new Intent(this,ViewTaskActivity.class));
}
}
18- All resources are included in the download folder. Download the source code.
Thanks for reading the tutorial. Subscribe to my YouTube channel. Like and Share my Facebook page with your friends.