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

Genova t167 Final Activity

This document contains source code for an Android application called "My Personal Bucket List". It includes XML layout files for the main activity, splash screen, new task form, and task cards. It also includes Java source code for the main activity class and add new task class. Styles, colors, and string resources are defined. The application allows users to view and manage a list of personal bucket list tasks.

Uploaded by

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

Genova t167 Final Activity

This document contains source code for an Android application called "My Personal Bucket List". It includes XML layout files for the main activity, splash screen, new task form, and task cards. It also includes Java source code for the main activity class and add new task class. Styles, colors, and string resources are defined. The application allows users to view and manage a list of personal bucket list tasks.

Uploaded by

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

FINAL ACTIVITY

MY PERSONAL BUCKET LIST

Annerose Jimenez T158

Angelica Jimenez T158

Nuer Edsil Jr T158

Ian Duwil Genova T167

SOURCE CODE:

XML DESIGN

Activity_main.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"
android:layout_width="match_parent"
android:layout_height="match_parent">

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

<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:nestedScrollingEnabled="true" />

<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_24"
android:layout_alignParentRight="true" />

</RelativeLayout>

Activity_splash.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"
android:background="@android:color/holo_blue_dark">

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

New_task.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:padding="16dp">

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

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

</RelativeLayout>
Task_layout.xml
.cardview.widget.CardView
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"
xmlns:tools="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/todoCheckBox"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:paddingStart="8dp"
android:buttonTint="@android:color/holo_green_dark"
tools:text="Go to the market and bring vegetables immediately
today."
android:paddingLeft="8dp" />

</RelativeLayout>

</androidx.cardview.widget.CardView>

Colors.xml
<?xml version="1.0" encoding="utf-8"?>
<resources>
<color name="purple_200">#FFBB86FC</color>
<color name="purple_500">#FF6200EE</color>
<color name="purple_700">#FF3700B3</color>
<color name="teal_200">#FF03DAC5</color>
<color name="teal_700">#FF018786</color>
<color name="black">#FF000000</color>
<color name="white">#FFFFFFFF</color>
</resources>

Styles.xml
<resources xmlns:tools="http://schemas.android.com/tools">
<!-- Base application theme. -->
<style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
<!-- Customize your theme here. -->
<item name="colorPrimary">@android:color/holo_blue_bright</item>
<item name="colorPrimaryDark">@android:color/holo_blue_dark</item>
<item name="colorAccent">@android:color/holo_green_dark</item>
</style>

<style name="DialogStyle" parent="Theme.Design.Light.BottomSheetDialog">


<item name="android:windowIsFloating">false</item>
<item name="android:statusBarColor"
tools:targetApi="lollipop">@android:color/transparent</item>
<item name="android:windowSoftInputMode">adjustResize</item>
</style>

</resources>

Strings.xml
<resources>
<string name="app_name">My Personal Bucket List</string>
<string name="save">Save</string>
<string name="new_task">New Task</string>
<string name="tasks">Tasks</string>
</resources>

Java Source codes

MainActivity.java
package com.example.mypersonalbucketlist;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
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.AsyncTask;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;

import com.google.android.material.floatingactionbutton.FloatingActionButton;

import com.example.mypersonalbucketlist.Adapter.ToDoAdapter;
import com.example.mypersonalbucketlist.model.ToDoModel;
import com.example.mypersonalbucketlist.Utils.DatabaseHandler;

import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.Iterator;
import java.util.List;
import java.util.ListIterator;
import java.util.Objects;
public class MainActivity extends AppCompatActivity implements
DialogCloseListener{

private DatabaseHandler db;

private RecyclerView tasksRecyclerView;


private ToDoAdapter tasksAdapter;
private FloatingActionButton fab;

private List<ToDoModel> taskList;

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

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

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

ItemTouchHelper itemTouchHelper = new


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

fab = findViewById(R.id.fab);

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

tasksAdapter.setTasks(taskList);

fab.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
AddNewTask.newInstance().show(getSupportFragmentManager(),
AddNewTask.TAG);
}
});
}

@Override
public void handleDialogClose(DialogInterface dialog){
taskList = db.getAllTasks();
Collections.reverse(taskList);
tasksAdapter.setTasks(taskList);
tasksAdapter.notifyDataSetChanged();
}
}
AddNewTask.java
package com.example.mypersonalbucketlist;

import android.app.Activity;
import android.content.DialogInterface;
import android.graphics.Color;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.EditText;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.core.content.ContextCompat;

import com.google.android.material.bottomsheet.BottomSheetDialogFragment;

import com.example.mypersonalbucketlist.Adapter.ToDoAdapter;
import com.example.mypersonalbucketlist.model.ToDoModel;
import com.example.mypersonalbucketlist.Utils.DatabaseHandler;

import java.util.Objects;

public class AddNewTask extends BottomSheetDialogFragment {

public static final String TAG = "ActionBottomDialog";


private EditText newTaskText;
private Button newTaskSaveButton;

private DatabaseHandler db;

public static AddNewTask newInstance(){


return new AddNewTask();
}

@Override
public void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setStyle(STYLE_NORMAL, R.style.DialogStyle);
}

@Override
public View onCreateView(@NonNull LayoutInflater inflater, @Nullable
ViewGroup container,
@Nullable Bundle savedInstanceState) {

View view = inflater.inflate(R.layout.new_task, container, false);

getDialog().getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPU
T_ADJUST_RESIZE);
return view;
}

@Override
public void onViewCreated(@NonNull View view, @Nullable Bundle
savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
newTaskText = requireView().findViewById(R.id.newTaskText);
newTaskSaveButton = getView().findViewById(R.id.newTaskButton);

boolean isUpdate = false;

final Bundle bundle = getArguments();


if(bundle != null){
isUpdate = true;
String task = bundle.getString("task");
newTaskText.setText(task);
assert task != null;
if(task.length()>0) {

newTaskSaveButton.setTextColor(ContextCompat.getColor(requireContext(),
R.color.black));
}
}

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

newTaskText.addTextChangedListener(new TextWatcher() {
@Override
public void beforeTextChanged(CharSequence s, int start, int
count, int after) {
}

@Override
public void onTextChanged(CharSequence s, int start, int before,
int count) {
if(s.toString().equals("")){
newTaskSaveButton.setEnabled(false);
newTaskSaveButton.setTextColor(Color.GRAY);
}
else{
newTaskSaveButton.setEnabled(true);

newTaskSaveButton.setTextColor(ContextCompat.getColor(requireContext(),
R.color.black));
}
}

@Override
public void afterTextChanged(Editable s) {
}
});

final boolean finalIsUpdate = isUpdate;


newTaskSaveButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
String text = newTaskText.getText().toString();
if(finalIsUpdate){
db.updateTask(bundle.getInt("id"), text);
}
else {
ToDoModel task = new ToDoModel();
task.setTask(text);
task.setStatus(0);
db.insertTask(task);
}
dismiss();
}
});
}

@Override
public void onDismiss(@NonNull DialogInterface dialog){
Activity activity = getActivity();
if(activity instanceof DialogCloseListener)
((DialogCloseListener)activity).handleDialogClose(dialog);
}
}

RecyclerItemTouchHelper.java
package com.example.mypersonalbucketlist;

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.annotation.NonNull;
import androidx.appcompat.app.AlertDialog;
import androidx.core.content.ContextCompat;
import androidx.recyclerview.widget.ItemTouchHelper;
import androidx.recyclerview.widget.RecyclerView;

import com.example.mypersonalbucketlist.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(@NonNull RecyclerView recyclerView, @NonNull
RecyclerView.ViewHolder viewHolder, @NonNull RecyclerView.ViewHolder target)
{
return false;
}

@Override
public void onSwiped(@NonNull 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 dialog, int which) {

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

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

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.black));
} else {
icon = ContextCompat.getDrawable(adapter.getContext(),
R.drawable.ic_baseline_delete);
background = new ColorDrawable(Color.RED);
}

assert icon != null;


int iconMargin = (itemView.getHeight() - icon.getIntrinsicHeight()) /
2;
int iconTop = itemView.getTop() + (itemView.getHeight() -
icon.getIntrinsicHeight()) / 2;
int iconBottom = iconTop + icon.getIntrinsicHeight();

if (dX > 0) { // Swiping to the right


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) { // Swiping to the left
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 { // view is unSwiped
background.setBounds(0, 0, 0, 0);
}

background.draw(c);
icon.draw(c);
}
}

SplashActivity.java
package com.example.mypersonalbucketlist;

import androidx.appcompat.app.AppCompatActivity;

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

public class SplashActivity extends AppCompatActivity {

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

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


new Handler().postDelayed(new Runnable() {
@Override
public void run() {
startActivity(i);
finish();
}
}, 2000);
}
}

DialogCloseListener.java
package com.example.mypersonalbucketlist;

import android.content.DialogInterface;

public interface DialogCloseListener {


public void handleDialogClose(DialogInterface dialog);
}

DatabaseHandler.java
package com.example.mypersonalbucketlist.Utils;

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.mypersonalbucketlist.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 final 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;

public 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 older table if existed
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);
}

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();
assert cur != null;
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, String 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)});
}
}

ToDoModel.java
package com.example.mypersonalbucketlist.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;
}
}
ToDoAdapter.java
package com.example.mypersonalbucketlist.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.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.example.mypersonalbucketlist.AddNewTask;
import com.example.mypersonalbucketlist.MainActivity;
import com.example.mypersonalbucketlist.model.ToDoModel;
import com.example.mypersonalbucketlist.R;
import com.example.mypersonalbucketlist.Utils.DatabaseHandler;

import java.util.List;

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


{

private List<ToDoModel> todoList;


private DatabaseHandler db;
private MainActivity activity;

public ToDoAdapter(DatabaseHandler db, MainActivity activity) {


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

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

@Override
public void onBindViewHolder(@NonNull final ViewHolder holder, int
position) {
db.openDatabase();

final 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 buttonView, boolean
isChecked) {
if (isChecked) {
db.updateStatus(item.getId(), 1);
} else {
db.updateStatus(item.getId(), 0);
}
}
});
}

private boolean toBoolean(int n) {


return n != 0;
}

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

public Context getContext() {


return activity;
}

public void setTasks(List<ToDoModel> todoList) {


this.todoList = todoList;
notifyDataSetChanged();
}

public void deleteItem(int position) {


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

public void editItem(int position) {


ToDoModel item = todoList.get(position);
Bundle bundle = new Bundle();
bundle.putInt("id", item.getId());
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);
}
}
}
OUTPUT:

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy