Android Todo List App

15-Dec-2024

Android Todo List App Full App Example

App Name :- Todo List


Given below is the output of the application

Now let's make this app

the first step of the app is to create an empty project in Android Studio

after creating the project


Step -01

Create A New Project .

Main Activity Xml


<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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:id="@+id/main"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity"
android:background="@color/white"
>

<FrameLayout
android:id="@+id/container"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_above="@+id/bottom_navigation" />

<com.google.android.material.bottomnavigation.BottomNavigationView
android:id="@+id/bottom_navigation"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
app:menu="@menu/nav_menu"
android:background="#199E92"
/>


</RelativeLayout>
Step-2

Create A Menu Resource File .

res>new>Directory>menu.

Menu>new>Menu resource file>nav_menu.


<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">

<item
android:id="@+id/task"
android:title="Tasks"
android:icon="@drawable/add_task"
/>
<item
android:id="@+id/complete"
android:title="Complete"
android:icon="@drawable/complete_task"
/>

</menu>
Step-03

 MainActvity.Java


package com.dailytodolist.todolist;

import android.annotation.SuppressLint;
import android.os.Bundle;
import android.view.MenuItem;

import androidx.activity.EdgeToEdge;
import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.graphics.Insets;
import androidx.core.view.ViewCompat;
import androidx.core.view.WindowInsetsCompat;
import androidx.fragment.app.FragmentManager;
import androidx.fragment.app.FragmentTransaction;

import com.dailytodolist.todolist.Fragment.CompleteFragment;
import com.dailytodolist.todolist.Fragment.TaskFragment;
import com.google.android.material.bottomnavigation.BottomNavigationView;

public class MainActivity extends AppCompatActivity {

BottomNavigationView bottom_navigation;

@SuppressLint("MissingInflatedId")
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
bottom_navigation=findViewById(R.id.bottom_navigation);
FragmentManager fmManager=getSupportFragmentManager();
FragmentTransaction fmTransaction=fmManager.beginTransaction();
fmTransaction.add(com.google.android.material.R.id.container,new TaskFragment());
fmTransaction.commit();
bottom_navigation.setOnNavigationItemSelectedListener(new BottomNavigationView.OnNavigationItemSelectedListener() {
@Override
public boolean onNavigationItemSelected(@NonNull MenuItem item) {


if (item.getItemId()==R.id.task){

FragmentManager fmManager=getSupportFragmentManager();
FragmentTransaction fmTransaction=fmManager.beginTransaction();
fmTransaction.add(com.google.android.material.R.id.container,new TaskFragment());
fmTransaction.addToBackStack(null);
fmTransaction.commit();

}else if (item.getItemId()==R.id.complete){

FragmentManager fmManager=getSupportFragmentManager();
FragmentTransaction fmTransaction=fmManager.beginTransaction();
fmTransaction.add(com.google.android.material.R.id.container,new CompleteFragment());
fmTransaction.commit();
}









return true;
}
});

}
}

Step-04

* Create  Fragment .

TaskFragment

Task Fragment Xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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"
xmlns:app="http://schemas.android.com/apk/res-auto"
tools:context=".TaskFragment">




<RelativeLayout
android:id="@+id/searchLayout"
android:layout_width="match_parent"
android:layout_height="50dp"
android:background="@drawable/edbackground"
android:layout_marginStart="50dp"
android:layout_marginEnd="20dp"
android:layout_marginTop="20dp"

>
<EditText
android:id="@+id/prioritysearch"
android:layout_width="220dp"
android:layout_height="50dp"
android:hint="Search by priority"
android:textColorHint="@color/black"
android:textAlignment="center"
android:textColor="@color/black"
android:textStyle="bold"
android:textSize="18sp"
android:background="@android:color/transparent"
/>

<ImageView
android:layout_width="50dp"
android:layout_height="200dp"
android:src="@drawable/search"
android:padding="10dp"
android:layout_margin="4dp"
android:layout_alignParentEnd="true"
android:scaleType="centerCrop"
/>



</RelativeLayout>
<com.airbnb.lottie.LottieAnimationView
android:id="@+id/no_data"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:lottie_rawRes="@raw/no_data2"
app:lottie_autoPlay="true"
app:lottie_loop="true"
android:layout_below="@id/searchLayout"
android:visibility="gone"
/>





<androidx.recyclerview.widget.RecyclerView
android:layout_marginTop="10dp"
android:layout_below="@id/searchLayout"
android:id="@+id/recyclerview"
android:layout_width="match_parent"
android:layout_height="match_parent"
/>





<com.google.android.material.floatingactionbutton.FloatingActionButton
android:id="@+id/fbBtn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_margin="30dp"
android:src="@drawable/add"
android:layout_marginBottom="50dp"
android:layout_alignParentBottom="true"
/>

</RelativeLayout>

Step-05

Task Fragment Java

package com.dailytodolist.todolist.Fragment;

import android.content.Intent;
import android.os.Bundle;

import androidx.fragment.app.Fragment;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import android.text.Editable;
import android.text.TextWatcher;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.EditText;

import com.dailytodolist.todolist.Adapter.TaskAdapter;
import com.dailytodolist.todolist.OtherActivity.ModelClass;
import com.dailytodolist.todolist.OtherActivity.TaskInputActivity;
import com.dailytodolist.todolist.R;
import com.dailytodolist.todolist.RoomDatabase.AdapterListener;
import com.dailytodolist.todolist.RoomDatabase.TaskDao;
import com.dailytodolist.todolist.RoomDatabase.ToDoDatabase;
import com.google.android.material.floatingactionbutton.FloatingActionButton;

import java.util.List;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;


public class TaskFragment extends Fragment implements AdapterListener {

RecyclerView recyclerview;
ToDoDatabase toDoDatabase;
TaskDao taskDao;
TaskAdapter taskAdapter;
EditText prioritysearch;
com.airbnb.lottie.LottieAnimationView no_data;
FloatingActionButton fbBtn;



@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View myView= inflater.inflate(R.layout.fragment_task, container, false);
fbBtn=myView.findViewById(R.id.fbBtn);
recyclerview=myView.findViewById(R.id.recyclerview);
prioritysearch=myView.findViewById(R.id.prioritysearch);
no_data=myView.findViewById(R.id.no_data);

toDoDatabase=ToDoDatabase.getInstance(getContext());
taskDao=toDoDatabase.getTaskDao();

taskAdapter=new TaskAdapter(getContext(),this);
fatchData();
recyclerview.setLayoutManager(new LinearLayoutManager(getContext()));
recyclerview.setAdapter(taskAdapter);

NodataAnim();
SearchData();
// LoadBtn();


fbBtn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
startActivity(new Intent(getContext(), TaskInputActivity.class));
}
});



if (container!=null){
container.removeAllViews();
}
return myView;
}


public void fatchData(){
List<ModelClass> taskList=taskDao.getAllusers();
for (int i=0;i<taskList.size();i++){
ModelClass task=taskList.get(i);
taskAdapter.addTask(task);
}
}

@Override
public void onResume() {
super.onResume();
taskAdapter.clearTask();
fatchData();



}
private void searchbyPriority(String prority) {
ExecutorService executorService = Executors.newSingleThreadExecutor();
executorService.execute(() -> {
List<ModelClass> usersList = taskDao.searchbypriority("%" + prority + "%"); // সার্চ কোয়েরি
getActivity().runOnUiThread(() -> {
taskAdapter.clearTask(); // আগের ডাটা ক্লিয়ার
for (ModelClass task : usersList) {
taskAdapter.addTask(task); // নতুন সার্চ ফলাফল যোগ করা
}
});
});


///////////////Search Method//////////////////


}




@Override
public void onDelete(int id, int pos) {

taskDao.delete(id);
taskAdapter.removeTask(pos);

}






public void NodataAnim(){

List<ModelClass> tasklist = taskDao.getAllusers(); // Fetch all users from the database

if (tasklist.isEmpty()) {
// If no data, hide RecyclerView and show LottieAnimationView
recyclerview.setVisibility(View.GONE);
no_data.setVisibility(View.VISIBLE); // Assuming no_data is your LottieAnimationView
no_data.playAnimation(); // Start Lottie animation if you want
} else {
// If data exists, show RecyclerView and hide LottieAnimationView
recyclerview.setVisibility(View.VISIBLE);
no_data.setVisibility(View.GONE);

// Update the adapter with the fetched users
taskAdapter.clearTask(); // Clear old data from the adapter
for (ModelClass task : tasklist) {
taskAdapter.addTask(task); // Add new users to the adapter
}
}


}


public void SearchData(){
prioritysearch.addTextChangedListener(new TextWatcher() {
@Override
public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {
}

@Override
public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {
}

@Override
public void afterTextChanged(Editable editable) {
String searchQuery = editable.toString();
searchbyPriority (searchQuery); // সার্চ মেথড কল করা
}
});
}


}

Step-06

*Create Fragment

CompleteFragment

CompleteFragment Xml


<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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"
xmlns:app="http://schemas.android.com/apk/res-auto"
tools:context=".Fragment.CompleteFragment">



<TextView
android:id="@+id/task"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Complete Task List"
android:textSize="25sp"
android:textStyle="bold"
android:layout_marginTop="20dp"
android:layout_marginStart="10dp"
android:textColor="#009688"
android:layout_centerHorizontal="true"
android:textAlignment="center"
/>

<com.airbnb.lottie.LottieAnimationView
android:id="@+id/no_data"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:lottie_rawRes="@raw/no_data_anim"
app:lottie_autoPlay="true"
app:lottie_loop="true"
android:layout_below="@id/task"
android:visibility="gone"
/>


<androidx.recyclerview.widget.RecyclerView
android:layout_marginTop="10dp"
android:layout_below="@id/task"
android:id="@+id/recyclerview"
android:layout_width="match_parent"
android:layout_height="match_parent"
/>


</RelativeLayout>

Step-07

CompleteFragment Java

package com.dailytodolist.todolist.Fragment;

import android.content.DialogInterface;
import android.graphics.Paint;
import android.os.Bundle;

import androidx.fragment.app.Fragment;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.EditText;
import android.widget.TextView;

import com.airbnb.lottie.LottieAnimationView;
import com.dailytodolist.todolist.Adapter.FinshedAdapter;
import com.dailytodolist.todolist.Adapter.TaskAdapter;
import com.dailytodolist.todolist.OtherActivity.FinishedModel;
import com.dailytodolist.todolist.OtherActivity.ModelClass;
import com.dailytodolist.todolist.R;
import com.dailytodolist.todolist.RoomDatabase.AdapterListener;
import com.dailytodolist.todolist.RoomDatabase.FinishDao;
import com.dailytodolist.todolist.RoomDatabase.FinishedListner;
import com.dailytodolist.todolist.RoomDatabase.TaskDao;
import com.dailytodolist.todolist.RoomDatabase.ToDoDatabase;

import java.util.List;


public class CompleteFragment extends Fragment {
RecyclerView recyclerview;
ToDoDatabase toDoDatabase;
FinishDao finishDao;
FinshedAdapter finshedAdapter;
EditText prioritysearch;
LottieAnimationView no_data;
TextView task;

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View myView= inflater.inflate(R.layout.fragment_complete, container, false);

recyclerview=myView.findViewById(R.id.recyclerview);
task=myView.findViewById(R.id.task);
no_data=myView.findViewById(R.id.no_data);
toDoDatabase=ToDoDatabase.getInstance(getContext());
finishDao=toDoDatabase.getFinishedDao();
finshedAdapter=new FinshedAdapter(getContext());
fatchData();
recyclerview.setLayoutManager(new LinearLayoutManager(getContext()));
recyclerview.setAdapter(finshedAdapter);
NodataAnim();

task.setPaintFlags(task.getPaintFlags()|
Paint.ANTI_ALIAS_FLAG);




















if (container!=null){
container.removeAllViews();
}
return myView;
}

public void fatchData(){
List<FinishedModel> finisList=finishDao.getAllfinish();
for (int i=0;i<finisList.size();i++){
FinishedModel finish=finisList.get(i);
finshedAdapter.addFinished(finish);
}
}

@Override
public void onResume() {
super.onResume();
finshedAdapter.FinishedclearTask();
fatchData();
}



public void NodataAnim(){

List<FinishedModel> finishlist = finishDao.getAllfinish(); // Fetch all users from the database

if (finishlist.isEmpty()) {
// If no data, hide RecyclerView and show LottieAnimationView
recyclerview.setVisibility(View.GONE);
no_data.setVisibility(View.VISIBLE); // Assuming no_data is your LottieAnimationView
no_data.playAnimation(); // Start Lottie animation if you want
} else {
// If data exists, show RecyclerView and hide LottieAnimationView
recyclerview.setVisibility(View.VISIBLE);
no_data.setVisibility(View.GONE);

// Update the adapter with the fetched users
finshedAdapter.FinishedclearTask(); // Clear old data from the adapter
for (FinishedModel task : finishlist) {
finshedAdapter.addFinished(task); // Add new users to the adapter
}
}


}



}

Step-08

Create Room DataBase Class

ToDoDatabase

package com.dailytodolist.todolist.RoomDatabase;

import android.content.Context;

import androidx.room.Room;
import androidx.room.RoomDatabase;

import com.dailytodolist.todolist.OtherActivity.FinishedModel;
import com.dailytodolist.todolist.OtherActivity.ModelClass;

@androidx.room.Database(entities = {ModelClass.class, FinishedModel.class},version = 1)
public abstract class ToDoDatabase extends RoomDatabase {
public abstract TaskDao getTaskDao();
public abstract FinishDao getFinishedDao();


public static ToDoDatabase INSTANCE;


public static ToDoDatabase getInstance(Context context){

if (INSTANCE==null){

INSTANCE= Room.databaseBuilder(context,ToDoDatabase.class,"ToDoDatabase")
.allowMainThreadQueries()
.fallbackToDestructiveMigration()
.build();
}
return INSTANCE;
}
}

Step-09

Create a  Interface

TaskDao


package com.dailytodolist.todolist.RoomDatabase;

import androidx.room.Dao;
import androidx.room.Insert;
import androidx.room.Query;
import androidx.room.Update;

import com.dailytodolist.todolist.OtherActivity.ModelClass;

import java.util.List;

@Dao
public interface TaskDao {
@Insert
void insert(ModelClass modelClass);

@Update
void update(ModelClass modelClass);


@Query("delete from ModelClass where id=:id")
void delete(int id);

@Query("SELECT * FROM ModelClass WHERE priority LIKE :priority")
List<ModelClass> searchbypriority(String priority);

@Query("select* from ModelClass order by id desc ")
List<ModelClass> getAllusers();


}



Step-10

Create a Interface

FinishDao

package com.dailytodolist.todolist.RoomDatabase;

import androidx.room.Dao;
import androidx.room.Insert;
import androidx.room.Query;
import androidx.room.Update;

import com.dailytodolist.todolist.OtherActivity.FinishedModel;
import com.dailytodolist.todolist.OtherActivity.ModelClass;

import java.util.List;

@Dao
public interface FinishDao {
@Insert
void insert(FinishedModel finishedModel);

@Update
void update(FinishedModel finishedModel);


@Query("delete from FinishedModel where id=:id")
void delete(int id);

@Query("SELECT * FROM FinishedModel WHERE Fpriority LIKE :priority")
List<FinishedModel> searchbypriority(String priority);

@Query("select* from FinishedModel order by id desc ")
List<FinishedModel> getAllfinish();



}


Step-11

*Create Class

ModelClass

package com.dailytodolist.todolist.OtherActivity;

import androidx.room.Entity;
import androidx.room.PrimaryKey;

@Entity
public class ModelClass {
@PrimaryKey(autoGenerate = true)
private int id;
String task;
String priority;
String date;
String time;
String note;

public ModelClass(int id, String task, String priority, String date, String time, String note) {
this.id = id;
this.task = task;
this.priority = priority;
this.date = date;
this.time = time;
this.note = note;
}

public int getId() {
return id;
}

public void setId(int id) {
this.id = id;
}

public String getTask() {
return task;
}

public void setTask(String task) {
this.task = task;
}

public String getPriority() {
return priority;
}

public void setPriority(String priority) {
this.priority = priority;
}

public String getDate() {
return date;
}

public void setDate(String date) {
this.date = date;
}

public String getTime() {
return time;
}

public void setTime(String time) {
this.time = time;
}

public String getNote() {
return note;
}

public void setNote(String note) {
this.note = note;
}
}


Step-12

*Create a Class

FinishedModel


package com.dailytodolist.todolist.OtherActivity;

import androidx.room.Entity;
import androidx.room.PrimaryKey;

@Entity
public class FinishedModel {
@PrimaryKey(autoGenerate = true)
int id;
String Ftask;
String Fpriority;
String Fnote;
String finisheTimed;
public FinishedModel(){}

public FinishedModel(int id, String ftask, String fpriority, String fnote, String finisheTimed) {
this.id = id;
Ftask = ftask;
Fpriority = fpriority;
Fnote = fnote;
this.finisheTimed = finisheTimed;
}

public int getId() {
return id;
}

public void setId(int id) {
this.id = id;
}

public String getFtask() {
return Ftask;
}

public void setFtask(String ftask) {
Ftask = ftask;
}

public String getFpriority() {
return Fpriority;
}

public void setFpriority(String fpriority) {
Fpriority = fpriority;
}

public String getFnote() {
return Fnote;
}

public void setFnote(String fnote) {
Fnote = fnote;
}

public String getFinisheTimed() {
return finisheTimed;
}

public void setFinisheTimed(String finisheTimed) {
this.finisheTimed = finisheTimed;
}
}

Step-13

Create Adapter Class

TaskAdapter



package com.dailytodolist.todolist.Adapter;

import android.annotation.SuppressLint;
import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.cardview.widget.CardView;
import androidx.recyclerview.widget.RecyclerView;

import com.dailytodolist.todolist.Fragment.TaskFragment;
import com.dailytodolist.todolist.OtherActivity.FinishedModel;
import com.dailytodolist.todolist.OtherActivity.ModelClass;
import com.dailytodolist.todolist.R;
import com.dailytodolist.todolist.RoomDatabase.AdapterListener;
import com.dailytodolist.todolist.RoomDatabase.FinishDao;
import com.dailytodolist.todolist.RoomDatabase.FinishedListner;
import com.dailytodolist.todolist.RoomDatabase.ToDoDatabase;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.List;
import java.util.Locale;

public class TaskAdapter extends RecyclerView.Adapter<TaskAdapter.mViewHolder> {
Context context;
AdapterListener adapterListener;
List<ModelClass> tasksList;
ToDoDatabase todoDb;
FinishDao finishDao;
Calendar calendar;
FinshedAdapter finshedAdapter;



public TaskAdapter(Context context, AdapterListener listener) {
this.context = context;
this.adapterListener=listener;
tasksList=new ArrayList<>();


todoDb=ToDoDatabase.getInstance(context);
finishDao=todoDb.getFinishedDao();


}

public void addTask(ModelClass task){
tasksList.add(task);
notifyDataSetChanged();
}
public void removeTask(int positon){
tasksList.remove(positon);
notifyDataSetChanged();


}
public void clearTask() {
tasksList.clear();
notifyDataSetChanged();
}


@NonNull
@Override
public mViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
View myview= LayoutInflater.from(parent.getContext()).inflate(R.layout.task_layout,parent,false);
return new mViewHolder(myview);
}

@SuppressLint("ResourceAsColor")
@Override
public void onBindViewHolder(@NonNull mViewHolder holder, @SuppressLint("RecyclerView") int position) {


ModelClass modelClass=tasksList.get(position);

holder.task.setText("Task : "+modelClass.getTask());
holder.dateandtime.setText("Work time : "+modelClass.getDate()+". At :- "+modelClass.getTime());
holder.task_note.setText("Task Note : "+modelClass.getNote());


if (modelClass.getPriority().equals("Emergency")){
holder.priorityflag.setImageResource(R.drawable.veryimportant);
holder.priority.setText("Priority : Very Emergency");

}else if (modelClass.getPriority().equals("Important")){
holder.priorityflag.setImageResource(R.drawable.medium);
holder.priority.setText("Priority : Important");
}else if (modelClass.getPriority().equals("Average")){
holder.priorityflag.setImageResource(R.drawable.low);
holder.priority.setText("Priority : Average");

}

holder.contentLay.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {



new AlertDialog.Builder(context)
.setTitle("Finished Task")
.setMessage("Are you finish task?")
.setIcon(R.drawable.finished)
.setPositiveButton("Yes", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialogInterface, int i) {
adapterListener.onDelete(modelClass.getId(),position);
dialogInterface.cancel();

String Ftask=modelClass.getTask();
String Fpriority=modelClass.getPriority();
String Fnote=modelClass.getNote();
calendar = Calendar.getInstance();
SimpleDateFormat sdf = new SimpleDateFormat("dd/mm/yyyy hh:mm", Locale.getDefault());
String finisheTimed = sdf.format(calendar.getTime());
finshedAdapter=new FinshedAdapter(context);
FinishedModel finishedModel=new FinishedModel(0,Ftask,Fpriority,Fnote,finisheTimed);
finshedAdapter.addFinished(finishedModel);
finishDao.insert(finishedModel);







}
})
.setNegativeButton("No", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialogInterface, int i) {

}
})
.setCancelable(false)

.show();


}
});





}

@Override
public int getItemCount() {
return tasksList.size();
}

public class mViewHolder extends RecyclerView.ViewHolder{

TextView task,priority,dateandtime,task_note;
ImageView priorityflag;
CardView contentLay;

public mViewHolder(@NonNull View itemView) {
super(itemView);

task=itemView.findViewById(R.id.task);
priority=itemView.findViewById(R.id.priority);
priorityflag=itemView.findViewById(R.id.priorityflag);
dateandtime=itemView.findViewById(R.id.dateandtime);
task_note=itemView.findViewById(R.id.task_note);
contentLay=itemView.findViewById(R.id.contentLay);
}
}


}

Step-14

Create a xml layout

task_layout


<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
xmlns:app="http://schemas.android.com/apk/res-auto">
<androidx.cardview.widget.CardView
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:cardCornerRadius="8dp"
app:cardElevation="4dp"
app:cardUseCompatPadding="true"
android:background="@drawable/iteamback"
android:id="@+id/contentLay"

>

<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="05dp"
android:orientation="vertical"
android:background="@color/white"
>

<TextView
android:id="@+id/task"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="@color/black"
android:textSize="20sp"
android:textStyle="bold"
android:layout_marginStart="05dp"
android:layout_marginTop="10dp"
/>
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginStart="05dp"

>

<TextView
android:id="@+id/priority"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="20sp"
android:textStyle="bold"
android:textColor="@color/black"

/>
<ImageView
android:id="@+id/priorityflag"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="20dp"
android:layout_alignParentRight="true"
android:layout_marginEnd="20dp"

/>


</RelativeLayout>

<TextView
android:id="@+id/dateandtime"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="20sp"
android:textStyle="bold"
android:textColor="@color/black"
android:layout_marginStart="05dp"

/>
<TextView
android:id="@+id/task_note"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="20sp"
android:textStyle="bold"
android:textColor="@color/black"
android:layout_marginStart="05dp"
android:layout_marginBottom="10dp"

/>

</LinearLayout>


</androidx.cardview.widget.CardView>


</LinearLayout>

Step-15

Create a Adapter Class

FinshedAdapter


package com.dailytodolist.todolist.Adapter;
import android.annotation.SuppressLint;
import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.graphics.Paint;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.cardview.widget.CardView;
import androidx.recyclerview.widget.RecyclerView;

import com.dailytodolist.todolist.OtherActivity.FinishedModel;
import com.dailytodolist.todolist.R;

import com.dailytodolist.todolist.RoomDatabase.FinishedListner;

import java.util.ArrayList;
import java.util.List;

public class FinshedAdapter extends RecyclerView.Adapter<FinshedAdapter.mViewHolder> {

Context context;
List<FinishedModel> finishedModelList;

public FinshedAdapter(Context context) {
this.context = context;

finishedModelList=new ArrayList<>();
}



public void addFinished(FinishedModel finishedModel){
finishedModelList.add(finishedModel);
notifyDataSetChanged();
}
public void removeTask(int positon){
finishedModelList.remove(positon);
notifyDataSetChanged();


}
public void FinishedclearTask() {
finishedModelList.clear();
notifyDataSetChanged();
}



@NonNull
@Override
public mViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {

View myview= LayoutInflater.from(parent.getContext()).inflate(R.layout.finished_layout,parent,false);
return new mViewHolder(myview);
}

@SuppressLint("ResourceAsColor")
@Override
public void onBindViewHolder(@NonNull mViewHolder holder, @SuppressLint("RecyclerView") int position) {
FinishedModel finishedModel=finishedModelList.get(position);

holder.task.setText("Task : "+finishedModel.getFtask());
holder.dateandtime.setText("Finished : "+finishedModel.getFinisheTimed());
holder.task_note.setText("Task Note : "+finishedModel.getFnote());
//////////////Cross Line///////////////
holder. task.setPaintFlags(holder.task.getPaintFlags()|
Paint.STRIKE_THRU_TEXT_FLAG);

holder. task_note.setPaintFlags(holder.task_note.getPaintFlags()|
Paint.STRIKE_THRU_TEXT_FLAG);

holder. dateandtime.setPaintFlags(holder.dateandtime.getPaintFlags()|
Paint.STRIKE_THRU_TEXT_FLAG);
holder. priority.setPaintFlags(holder.priority.getPaintFlags()|
Paint.STRIKE_THRU_TEXT_FLAG);
//////////////Cross Line///////////////


if (finishedModel.getFpriority().equals("Emergency")){
holder.priorityflag.setImageResource(R.drawable.veryimportant);
holder.priority.setText("Priority : Very Emergency");

}else if (finishedModel.getFpriority().equals("Important")){
holder.priorityflag.setImageResource(R.drawable.medium);
holder.priority.setText("Priority : Important");
}else if (finishedModel.getFpriority().equals("Average")){
holder.priorityflag.setImageResource(R.drawable.low);
holder.priority.setText("Priority : Average");

}

holder.contentLay.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {


new AlertDialog.Builder(context)
.setTitle("Remove History")
.setMessage("Are you remove history?")
.setIcon(R.drawable.finished)
.setPositiveButton("Yes", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialogInterface, int i) {
// finishedListner.onDelete(finishedModel.getId(),position);
dialogInterface.cancel();



}
})
.setNegativeButton("No", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialogInterface, int i) {

}
})
.setCancelable(false)

.show();

}
});

}

@Override
public int getItemCount() {
return finishedModelList.size();
}

public class mViewHolder extends RecyclerView.ViewHolder{

TextView task,priority,dateandtime,task_note;
ImageView priorityflag;
CardView contentLay;
RelativeLayout LineLayout;

public mViewHolder(@NonNull View itemView) {
super(itemView);

task=itemView.findViewById(R.id.task);
priority=itemView.findViewById(R.id.priority);
priorityflag=itemView.findViewById(R.id.priorityflag);
dateandtime=itemView.findViewById(R.id.dateandtime);
task_note=itemView.findViewById(R.id.task_note);
contentLay=itemView.findViewById(R.id.contentLay);
LineLayout=itemView.findViewById(R.id.LineLayout);
}
}
}

Step-16

Create a xml layout

finished_layout


<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
xmlns:app="http://schemas.android.com/apk/res-auto">

<androidx.cardview.widget.CardView
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:cardCornerRadius="8dp"
app:cardElevation="4dp"
app:cardUseCompatPadding="true"
android:background="@drawable/iteamback"
android:id="@+id/contentLay"

>

<RelativeLayout
android:id="@+id/LineLayout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="05dp"
android:orientation="vertical"
android:background="@color/white"
>


<TextView
android:id="@+id/task"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="@color/black"
android:textSize="20sp"
android:textStyle="bold"
android:layout_marginStart="05dp"
android:layout_marginTop="10dp"
android:text="Task : "
/>
<RelativeLayout
android:id="@+id/priorityLay"
android:layout_below="@id/task"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginStart="05dp"

>


<TextView
android:id="@+id/priority"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="20sp"
android:textStyle="bold"
android:textColor="@color/black"
android:text="Priority : "



/>
<ImageView
android:id="@+id/priorityflag"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="20dp"
android:layout_alignParentRight="true"
android:layout_marginEnd="20dp"

/>


</RelativeLayout>


<TextView
android:id="@+id/dateandtime"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="20sp"
android:textStyle="bold"
android:textColor="@color/black"
android:layout_marginStart="05dp"
android:layout_below="@id/priorityLay"
android:text="Finished : "


/>

<TextView
android:id="@+id/task_note"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="20sp"
android:textStyle="bold"
android:textColor="@color/black"
android:layout_marginStart="05dp"
android:layout_marginBottom="10dp"
android:layout_below="@id/dateandtime"
android:text="Task Note : "



/>

</RelativeLayout>


</androidx.cardview.widget.CardView>

</LinearLayout>

Step-17

Create a Activity

TaskInputActivity

xml Code



<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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:id="@+id/main"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".TaskInputActivity"
android:orientation="vertical"
android:background="@color/white"
>
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="100dp"
android:orientation="horizontal"
android:background="@drawable/toolbar"
>

<ImageView
android:id="@+id/back"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/backarrow"
android:layout_marginTop="35dp"
android:layout_marginStart="20dp"

/>

<TextView
android:id="@+id/textView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="View and Edit"
android:textSize="25sp"
android:textStyle="bold"
android:layout_centerInParent="true"
/>


</RelativeLayout>


<com.google.android.material.textfield.TextInputLayout
android:id="@+id/filledTextField"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginStart="10dp"
android:layout_marginTop="10dp"
android:layout_marginEnd="10dp"
android:hint="What needs to be done?"
app:hintTextColor="@color/black"
android:textColorHint="@color/black"
app:boxStrokeColor="@color/black"
app:cursorColor="@color/black"

>
<com.google.android.material.textfield.TextInputEditText
android:id="@+id/todo_task"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textColorHint="@color/black"
android:textColor="@color/black"
android:textSize="20sp"
android:textStyle="bold"


/>

</com.google.android.material.textfield.TextInputLayout>




<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Priority"
android:textSize="25sp"
android:textStyle="bold"
android:layout_marginTop="15dp"
android:layout_marginStart="10dp"
android:textColor="#009688"
/>

<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:layout_marginTop="15dp"
android:layout_marginStart="10dp"
android:layout_marginEnd="10dp"
android:background="#0C000000"
>


<CheckBox
android:id="@+id/phigh"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Emergency"
android:textColor="#0B2FF6"
android:textStyle="bold"
android:textSize="20sp"
/>
<CheckBox
android:id="@+id/pmedium"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Important"
android:textColor="#0B2FF6"
android:textStyle="bold"
android:textSize="20sp"
android:layout_centerInParent="true"
/>
<CheckBox
android:id="@+id/plow"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Average"
android:textColor="#0B2FF6"
android:textStyle="bold"
android:textSize="20sp"
android:layout_alignParentRight="true"
/>

</RelativeLayout>

<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Due Date"
android:textSize="25sp"
android:textStyle="bold"
android:layout_marginTop="15dp"
android:layout_marginStart="10dp"
android:textColor="#009688"
/>



<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="15dp"
>

<ImageView
android:id="@+id/date"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/calender"
android:layout_marginStart="10dp"
/>

<TextView
android:id="@+id/datedisplay"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text=""
android:textSize="20sp"
android:textStyle="bold"
android:layout_marginStart="10dp"
android:textColor="#3F51B5"
android:layout_gravity="center"
/>

</LinearLayout>

<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="15dp"
>

<ImageView
android:id="@+id/time"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/time"
android:layout_marginStart="10dp"
android:layout_gravity="center"

/>

<TextView
android:id="@+id/timedisplay"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text=""
android:textSize="20sp"
android:textStyle="bold"
android:layout_marginStart="10dp"
android:textColor="#3F51B5"
android:layout_gravity="center"
/>

</LinearLayout>



<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Reminder"
android:textSize="25sp"
android:textStyle="bold"
android:layout_marginTop="15dp"
android:layout_marginStart="10dp"
android:textColor="#009688"
/>

<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="15dp"
>

<ImageView
android:id="@+id/alarm"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/alarm"
android:layout_marginStart="10dp"
android:layout_gravity="center"
/>

<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Set reminder"
android:textSize="20sp"
android:textStyle="bold"
android:layout_marginStart="10dp"
android:textColor="#3F51B5"
android:layout_gravity="center"
/>

</LinearLayout>



<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="15dp"

>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Notes"
android:textSize="25sp"
android:textStyle="bold"
android:layout_marginStart="10dp"
android:textColor="#009688"
/>



<com.google.android.material.floatingactionbutton.FloatingActionButton
android:id="@+id/savefbBtn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:src="@drawable/save"
android:layout_marginEnd="20dp"
android:layout_marginBottom="5dp"
/>

</RelativeLayout>

<EditText
android:id="@+id/note"
android:layout_margin="10dp"
android:layout_width="match_parent"
android:layout_height="120dp"
android:hint=" Write Your Note"
android:background="@drawable/edbackground"
android:textColorHint="@color/black"
android:textColor="@color/black"
android:textSize="18sp"
android:textStyle="italic|bold"
android:gravity="top|start"
app:cursorColor="@color/black"

/>

</LinearLayout>

Step-18

TaskInputActivity Java Code


package com.dailytodolist.todolist.OtherActivity;

import android.annotation.SuppressLint;
import android.app.DatePickerDialog;
import android.app.TimePickerDialog;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.CheckBox;
import android.widget.DatePicker;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.TimePicker;
import android.widget.Toast;

import androidx.appcompat.app.AppCompatActivity;
import androidx.fragment.app.FragmentManager;
import androidx.fragment.app.FragmentTransaction;

import com.dailytodolist.todolist.Adapter.TaskAdapter;
import com.dailytodolist.todolist.Fragment.TaskFragment;
import com.dailytodolist.todolist.R;
import com.dailytodolist.todolist.RoomDatabase.AdapterListener;
import com.dailytodolist.todolist.RoomDatabase.TaskDao;
import com.dailytodolist.todolist.RoomDatabase.ToDoDatabase;
import com.google.android.material.floatingactionbutton.FloatingActionButton;
import com.google.android.material.textfield.TextInputEditText;

import java.util.Calendar;

public class TaskInputActivity extends AppCompatActivity implements AdapterListener {

ImageView back,date,time,alarm;
CheckBox phigh,pmedium,plow;
FloatingActionButton savefbBtn;
TextInputEditText todo_task;
TextView datedisplay,timedisplay;
EditText note;
TaskAdapter taskAdapter;

ToDoDatabase toDoDatabase;
TaskDao taskDao;

@SuppressLint("MissingInflatedId")
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_task_input);

back=findViewById(R.id.back);
date=findViewById(R.id.date);
time=findViewById(R.id.time);
alarm=findViewById(R.id.alarm);
phigh=findViewById(R.id.phigh);
pmedium=findViewById(R.id.pmedium);
plow=findViewById(R.id.plow);
savefbBtn=findViewById(R.id.savefbBtn);
todo_task=findViewById(R.id.todo_task);
note=findViewById(R.id.note);
datedisplay=findViewById(R.id.datedisplay);
timedisplay=findViewById(R.id.timedisplay);

taskAdapter=new TaskAdapter(this, this);
toDoDatabase=ToDoDatabase.getInstance(this);
taskDao=toDoDatabase.getTaskDao();



DatePicker();
TimePicker();
SaveData();

back.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {

finish();
}
});



}


public void DatePicker(){

date.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {



final Calendar c = Calendar.getInstance();

// on below line we are getting
// our day, month and year.
int year = c.get(Calendar.YEAR);
int month = c.get(Calendar.MONTH);
int day = c.get(Calendar.DAY_OF_MONTH);

// on below line we are creating a variable for date picker dialog.
DatePickerDialog datePickerDialog = new DatePickerDialog(
// on below line we are passing context.
TaskInputActivity.this,
new DatePickerDialog.OnDateSetListener() {
@Override
public void onDateSet(DatePicker view, int year,
int monthOfYear, int dayOfMonth) {
// on below line we are setting date to our text view.
datedisplay.setText(dayOfMonth + "-" + (monthOfYear + 1) + "-" + year);

}
},
// on below line we are passing year,
// month and day for selected date in our date picker.
year, month, day);
// at last we are calling show to
// display our date picker dialog.
datePickerDialog.show();
}
});





}

public void TimePicker(){

time.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
final Calendar c = Calendar.getInstance();

// on below line we are getting our hour, minute.
int hour = c.get(Calendar.HOUR);
int minute = c.get(Calendar.MINUTE);

// on below line we are initializing our Time Picker Dialog
TimePickerDialog timePickerDialog = new TimePickerDialog(TaskInputActivity.this,
new TimePickerDialog.OnTimeSetListener() {
@Override
public void onTimeSet(TimePicker view, int hourOfDay,
int minute) {
// on below line we are setting selected time
// in our text view.
timedisplay.setText(hourOfDay + ":" + minute);
}
}, hour, minute,true);
// at last we are calling show to
// display our time picker dialog.
timePickerDialog.show();
}
});



}


public void SaveData(){

savefbBtn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {

String checkdData="";
if (phigh.isChecked())
{
checkdData = phigh.getText().toString();

}


if (pmedium.isChecked())
{
checkdData= pmedium.getText().toString();
}


if (plow.isChecked())
{
checkdData= plow.getText().toString();
}


String stask =todo_task.getText().toString();
String sdate=datedisplay.getText().toString();
String stime=timedisplay.getText().toString();
String snote=note.getText().toString();

ModelClass md=new ModelClass(0,stask,checkdData,sdate,stime,snote);
taskAdapter.addTask(md);
taskDao.insert(md);



Toast.makeText(TaskInputActivity.this, "Saved", Toast.LENGTH_SHORT).show();







}
});
}




@Override
public void onDelete(int id, int pos) {

}


}


Step-19

Create a Interface

AdapterListener


package com.dailytodolist.todolist.RoomDatabase;

import com.dailytodolist.todolist.OtherActivity.ModelClass;

public interface AdapterListener {
void onDelete(int id,int pos);
}




Comments