0% found this document useful (0 votes)
78 views17 pages

Kotlin Code Finals

The document contains code for a todo list Android application. It includes XML layout files for the main activity, task items, and adding a new task. It also includes Java code for activities, adapters, and models. The main activity contains a recycler view to display tasks, adds and deletes tasks from the database, and allows marking tasks as complete.

Uploaded by

Nuriel Aguilar
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
78 views17 pages

Kotlin Code Finals

The document contains code for a todo list Android application. It includes XML layout files for the main activity, task items, and adding a new task. It also includes Java code for activities, adapters, and models. The main activity contains a recycler view to display tasks, adds and deletes tasks from the database, and allows marking tasks as complete.

Uploaded by

Nuriel Aguilar
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 17

ACTIVITY_MAIN.

XML

<?xml version="1.0" encoding="utf-8"?>


<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_height="match_parent"
android:layout_width="match_parent">

<TextView
android:id="@+id/tasksText"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textStyle="bold"
android:text="Tasks"
android:textColor="@android:color/black"
android:layout_marginStart="16dp"
android:layout_marginBottom="16dp"
android:layout_marginTop="16dp"
android:textSize="32sp"/>

<androidx.recyclerview.widget.RecyclerView
android:id="@+id/tasksRecyclerView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@id/tasksText"
app:layoutManager="androidx.recyclerview.widget.LinearLayoutManager"
android:backgroundTint="@android:color/holo_green_dark"

<com.google.android.material.floatingactionbutton.FloatingActionButton
android:id="@+id/fab"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignParentEnd="true"
android:layout_margin="32dp"
android:backgroundTint="@android:color/holo_green_dark"
android:src="@drawable/ic_baseline_add"/>

</RelativeLayout>
TASK_LAYOUT.XML

<androidx.cardview.widget.CardView
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_height="wrap_content"
android:layout_width="match_parent"
xmlns:tool="http://schemas.android.com/tools"
app:cardElevation="4dp"
app:cardCornerRadius="8dp"
android:layout_marginHorizontal="16dp"
android:layout_marginVertical="8dp">

<RelativeLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="8dp">

<CheckBox
android:id="@+id/todoCheck"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
tool:text="This is an Sample Task inside the application."
android:paddingStart="8dp"
android:buttonTint="@android:color/holo_green_dark"/>
</RelativeLayout>

</androidx.cardview.widget.CardView>
NEW_TASK.XML

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content">

<EditText
android:id="@+id/newTaskText"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@null"
android:hint="New Task"
android:textappearance="@style/TextAppearance.AppCompat.Medium" />

<Button
android:id="@+id/newTaskButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@id/newTaskButton"
android:layout_alignParentEnd="true"
android:textSize="16sp"
android:text="Save"
android:textAllCaps="false"
android:background="@android:color/transparent"
android:textColor="@android:color/darker_gray"/>

</RelativeLayout>
ACTIVITY_CODE.XML

<?xml version="1.0" encoding="utf-8"?>


<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@android:color/holo_green_dark"

<ImageView
android:layout_width="200dp"
android:layout_height="200dp"
android:src"@drawable/icon"
android:layout_centerInParent="true"/>

</RelativeLayout>
CODEACTIVITY.JAVA

package com.example.doit;

import androidx.appcompat.app.AppCompatActivity;

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

public class CodeActivity extends AppCompatActivity {

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_code);
getSupportActionBar().hide();

final Intent i = new Intent(CodeActivity.this, MainActivity.class);


long delayMillis;
new Handler().postDelayed(new Runnable() {
@Override
public void run() {
startActivity(i);
finish();
}
},delayMillis 1000);
}
}
MAINACITIVITY.JAVA

package com.example.doit;

import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.ItemTouchHelper;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import android.content.DialogInterface;
import android.os.Bundle;
import android.view.View;

import com.example.doit.Adapter.ToDoAdapter;
import com.example.doit.Model.ToDoModel;
import com.example.doit.Utils.DatabaseHandler;
import com.google.android.material.floatingactionbutton.FloatingActionButton;

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

public class MainActivity extends AppCompatActivity implements DialogCloseListener{

private RecyclerView tasksRecyclerView;


private ToDoAdapter tasksAdapter;
private FloatingActionButton fab;

private List<ToDoModel> taskList;


private DatabaseHandler db;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
getSupportActionBar().hide();

db = new DatabaseHandler(this);
db.openDatabase();

taskList = new ArrayList<>();

tasksRecyclerView = findViewById(R.id.tasksRecyclerView);
tasksRecyclerView.setLayoutManager(new LinearLayoutManager(this));
tasksAdapter = new ToDoAdapter(db,this);
tasksRecyclerView.setAdapter(tasksAdapter);

fab = findViewById(R.id.fab);

ItemTouchHelper itemTouchHelper = new


ItemTouchHelper(new RecyclerItemTouchHelper(tasksAdapter));
itemTouchHelper.attachToRecyclerView(tasksRecyclerView);

taskList = db.getAllTasks();
Collections.reverse(taskList);
tasksAdapter.setTasks(taskList);

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

AddNewTask.newInstance().show(getSupportFragmentManager(),AddNewTask.TAG);
}
});
}

@Override
public void handleDialogClose(DialogInterface dialog){
taskList = db.getAllTasks();
Collections.reverse(taskList);
tasksAdapter.setTasks(taskList);
tasksAdapter.notifyDataSetChanged();
}
}
TODOADAPTER.JAVA

package com.example.doit.Adapter;

import android.content.Context;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.CheckBox;
import android.widget.CompoundButton;

import androidx.recyclerview.widget.RecyclerView;

import com.example.doit.AddNewTask;
import com.example.doit.MainActivity;
import com.example.doit.Model.ToDoModel;
import com.example.doit.R;
import com.example.doit.Utils.DatabaseHandler;

import java.util.List;

public class ToDoAdapter extends RecyclerView.Adapter<ToDoAdapter.ViewHolder> {

private List<ToDoModel> todoList;


private MainActivity activity;
private DatabaseHandler db;

public ToDoAdapter(DatabaseHandler db,MainActivity activity){


this.db = db;
this.activity = activity;
}

public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType){


View itemView = LayoutInflater.from(parent.getContext())
.inflate(R.layout.task_layout, parent, false);
return new ViewHolder(itemView);
}

public void onBindViewHolder(ViewHolder holder, int position){


db.openDatabase();
ToDoModel item = todoList.get(position);
holder.task.setText(item.getTask());
holder.task.setChecked(toBoolean(item.getStatus()));
holder.task.setOnCheckedChangeListener(new
CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton compoundButton, boolean b) {
if(isChecked){
db.updateStatus(item.getId(), 1);
}
else{
db.updateStatus(item.getId(),0);
}
}
});
}

public int getItemCount(){


return todoList.size();
}

private boolean toBoolean(int n){


return n!=0;
}

public void setTasks(List<ToDoModel> todoList){


this.todoList = todoList;
notifyDataSetChanged();
}

public Context getContext(){


return activity;
}

public void deleteItem(int position){


ToDoModel item = todoList.get(position);
db.deleteTask(item.getId());
todoList.remove(position);
}

public void editItem(int position){


ToDoModel item = todoList.get(position);
Bundle bundle = new Bundle();
bundle.putInt("id",item,getItemId());
bundle.putString("task", item.getTask());
AddNewTask fragment = new AddNewTask();
fragment.setArguments(bundle);
fragment.show(activity.getSupportFragmentManager(), AddNewTask.TAG);
}
public static class ViewHolder extends RecyclerView.ViewHolder{
CheckBox task;

ViewHolder(View view){
super(view);
task = view.findViewById(R.id.todoCheckBox);
}
}
}
TODOMODEL.JAVA

package com.example.doit.Model;

public class ToDoModel {


private int id, status;
private String task;

public int getId() {


return id;
}

public void setId(int id) {


this.id = id;
}

public int getStatus() {


return status;
}

public void setStatus(int status) {


this.status = status;
}

public String getTask() {


return task;
}

public void setTask(String task) {


this.task = task;
}
}
DARABASEHANDLER.JAVA

package com.example.doit.Utils;

import android.annotation.SuppressLint;
import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;

import com.example.doit.Model.ToDoModel;

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

public class DatabaseHandler extends SQLiteOpenHelper {

private static final int VERSION = 1;


private static final String NAME = "toDoListDatabase";
private static final String TODO_TABLE = "todo";
private static String ID = "id";
private static final String TASK = "task";
private static final String STATUS = "status";
private static final String CREATE_TODO_TABLE ="CREATE TABLE "+TODO_TABLE
+"("+ID + " INTEGER PRIMARY KEY AUTOINCREMENT, "
+TASK +"TEXT,"+ STATUS +" INTEGER)";
private SQLiteDatabase db;

private DatabaseHandler(Context context) {


super(context, NAME, null, VERSION);

@Override
public void onCreate(SQLiteDatabase db) {
db.execSQL(CREATE_TODO_TABLE);
}

@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion){
//Drop the older tables
db.execSQL("DROP TABLE IF EXISTS" + TODO_TABLE);
//Create tables again
onCreate(db);
}

public void openDatabase(){


db = this.getWritableDatabase();
}

public void insertTask(ToDoModel task){


ContentValues cv = new ContentValues();
cv.put(TASK, task.getTask());
cv.put(STATUS, 0);
db.insert(TODO_TABLE, null, cv);
}

@SuppressLint("Range")
public List<ToDoModel> getAllTasks(){
List<ToDoModel> taskList = new ArrayList<>();
Cursor cur = null;
db.beginTransaction();
try {
cur = db.query(TODO_TABLE, null, null, null, null, null, null, null);
if (cur != null) {
if (cur.moveToFirst()) {
do {
ToDoModel task = new ToDoModel();
task.setId(cur.getInt(cur.getColumnIndex(ID)));
task.setTask(cur.getString(cur.getColumnIndex(TASK)));
task.setStatus(cur.getInt(cur.getColumnIndex(STATUS)));
taskList.add(task);
} while (cur.moveToNext());
}
}
}
finally{
db.endTransaction();
cur.close();
}
return taskList;
}

public void updateStatus(int id, int status) {


ContentValues cv = new ContentValues();
cv.put(STATUS, status);
db.update(TODO_TABLE, cv, ID + "+?", new String[] {String.valueOf(id)});
}

public void updateTask(int id, int task) {


ContentValues cv = new ContentValues();
cv.put(TASK, task);
db.update(TODO_TABLE, cv, ID = "+?", new String[] {String.valueOf(id)});
}

public void deleteTask(int id){


db.delete(TODO_TABLE, ID + "=?", new String[] {String.valueOf(id)});
}
}
RECYCLERITEMTOUCHHELPER.JAVA

package com.example.doit;

import android.app.AlertDialog;
import android.content.DialogInterface;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.graphics.drawable.Drawable;
import android.view.View;

import androidx.core.content.ContextCompat;
import androidx.recyclerview.widget.ItemTouchHelper;
import androidx.recyclerview.widget.RecyclerView;

import com.example.doit.Adapter.ToDoAdapter;

public class RecyclerItemTouchHelper extends ItemTouchHelper.SimpleCallback {

private ToDoAdapter adapter;

public RecyclerItemTouchHelper(ToDoAdapter adapter){


super(0, ItemTouchHelper.LEFT | ItemTouchHelper.RIGHT);
this.adapter = adapter;
}

@Override
public boolean onMove(RecyclerView recyclerView, RecyclerView.ViewHolder viewHolder,
RecyclerView.ViewHolder target){
return false;
}

@Override
public void onSwiped(final RecyclerView.ViewHolder viewHolder, int direction){
final int position = viewHolder.getAdapterPosition();
if(direction == ItemTouchHelper.LEFT){
AlertDialog.Builder builder = new AlertDialog.Builder(adapter.getContext());
builder.setTitle("Delete Task");
builder.setMessage("Are you sure you want to delete this Task?");
builder.setPositiveButton("Confirm",
new DialogInterface.OnClickListener(){
@Override
public void onClick(DialogInterface dialog, int which){
adapter.deleteItem(position);
}
});
builder.setNegativeButton(android.R.string.cancel, new
DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialogInterface, int which) {
adapter.notifyItemChanged(viewHolder.getAdapterPosition());

}
});
AlertDialog dialog = builder.create();
dialog.show();
}
else{
adapter.editItem(position);
}
}

@Override
public void onChildDraw(Canvas c, RecyclerView recyclerView, RecyclerView.ViewHolder
viewHolder, float dX, float dY, int actionState, boolean isCurretlyActive){
super.onChildDraw(c, recyclerView, viewHolder, dX, dY, actionState, isCurretlyActive);

Drawable icon;
ColorDrawable background;

View itemView = viewHolder.itemView;


int backgroundCornerOffset = 20;

if(dX>0) {
icon = ContextCompat.getDrawable(adapter.getContext(), R.drawable.ic_baseline_edit);
background = new ColorDrawable(ContextCompat.getColor(adapter.getContext(),
R.color.colorPrimaryDark));
} else{
icon = ContextCompat.getDrawable(adapter.getContext(),
R.drawable.ic_baseline_delete);
background = new ColorDrawable(Color.RED);
}

int iconMargin = (itemView.getHeight() - icon.getIntrinsicHeight()) /2;


int iconTop = itemView.getTop() + (itemView.getHeight() - icon.getIntrinsicHeight()) /2;
int iconBottom = iconTop + icon.getIntrinsicHeight();
if(dX>0){
int iconLeft = itemView.getLeft() + iconMargin;
int iconRight = itemView.getLeft() + iconMargin + icon.getIntrinsicWidth();
icon.setBounds(iconLeft, iconTop, iconRight, iconBottom);

background.setBounds(itemView.getLeft(), itemView.getTop(),
itemView.getLeft() + ((int)dX) + backgroundCornerOffset, itemView.getBottom());
}
else if(dX<0){
int iconLeft = itemView.getRight() - iconMargin - icon.getIntrinsicWidth();
int iconRight = itemView.getRight() - iconMargin;
icon.setBounds(iconLeft, iconTop, iconRight, iconBottom);

background.setBounds(itemView.getRight() + ((int)dX) -backgroundCornerOffset,


itemView.getTop(),
itemView.getRight(), itemView.getBottom());
}else {
background.setBounds(0, 0, 0,0);
}
}
background.draw(c);
icon.draw(c);
}
}

You might also like