0% found this document useful (0 votes)
11 views9 pages

To-Do Connector Source Code Overview

The document contains source code for an Android app that allows users to manage to-do lists. It includes code for the main activity, which handles displaying and editing tasks. It also includes code for a bottom sheet dialog fragment to add new tasks, and for swipe gestures to delete or edit existing tasks.
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)
11 views9 pages

To-Do Connector Source Code Overview

The document contains source code for an Android app that allows users to manage to-do lists. It includes code for the main activity, which handles displaying and editing tasks. It also includes code for a bottom sheet dialog fragment to add new tasks, and for swipe gestures to delete or edit existing tasks.
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

APPENDIX-SOURCE CODE:

Main [Link]:

package [Link];

import [Link];

import [Link];

import [Link];

import [Link];

import [Link];

import [Link];

import [Link];

import [Link];

import [Link];

import [Link];

import [Link];

import [Link];

import [Link];

import [Link];

public class MainActivity extends AppCompatActivity implements DialogCloseListener{

private DatabaseHandler db;

private RecyclerView tasksRecyclerView;

private ToDoAdapter tasksAdapter;

private FloatingActionButton fab;

private List<ToDoModel> taskList;

28

@Override

protected void onCreate(Bundle savedInstanceState) {

[Link](savedInstanceState);

setContentView([Link].activity_main);

[Link](getSupportActionBar()).hide();

db = new DatabaseHandler(this);

[Link]();
tasksRecyclerView = findViewById([Link]);

[Link](new LinearLayoutManager(this));

tasksAdapter = new ToDoAdapter(db,[Link]);

[Link](tasksAdapter);

ItemTouchHelper itemTouchHelper = new

ItemTouchHelper(new RecyclerItemTouchHelper(tasksAdapter));

[Link](tasksRecyclerView);

fab = findViewById([Link]);

taskList = [Link]();

[Link](taskList);

[Link](taskList);

[Link](new [Link]() {

@Override

public void onClick(View v) {

[Link]().show(getSupportFragmentManager(),

[Link]);

});

29

@Override

public void handleDialogClose(DialogInterface dialog){

taskList = [Link]();

[Link](taskList);

[Link](taskList);

[Link]();

Add New Task .java:

package [Link];

import [Link];
import [Link];

import [Link];

import [Link];

import [Link];

import [Link];

import [Link];

import [Link];

import [Link];

import [Link];

import [Link];

import [Link];import [Link];

import [Link];

import [Link];

30

import [Link];

import [Link];

import [Link];

import [Link].R;

import [Link];

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) {

[Link](savedInstanceState);

setStyle(STYLE_NORMAL, [Link]);
}

@Nullable

@Override

public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup

container,

@Nullable Bundle savedInstanceState) {

View view = [Link]([Link].new_task, container, false);

getDialog().getWindow().setSoftInputMode([Link].SOFT_INPUT_A

DJUST_RESIZE);

31

return view;

@Override

public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {

[Link](view, savedInstanceState);

newTaskText = [Link](getView()).findViewById([Link]);

newTaskSaveButton = getView().findViewById([Link]);

boolean isUpdate = false;

final Bundle bundle = getArguments();

if(bundle != null){

isUpdate = true;

String task = [Link]("task");

[Link](task);

assert task != null;

if([Link]()>0)

[Link]([Link]([Link](getContext

()), [Link]));

db = new DatabaseHandler(getActivity());

[Link]();

[Link](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) {

32

if([Link]().equals("")){

[Link](false);

[Link]([Link]);

else{

[Link](true);

[Link]([Link]([Link](getContext

()), [Link]));

@Override

public void afterTextChanged(Editable s) {

});

final boolean finalIsUpdate = isUpdate;

[Link](new [Link]() {

@Override

public void onClick(View v) {

String text = [Link]().toString();

if(finalIsUpdate){

[Link]([Link]("id"), text);

else {

ToDoModel task = new ToDoModel();

[Link](text);
33

[Link](0);

[Link](task);

dismiss();

});

@Override

public void onDismiss(@NonNull DialogInterface dialog){

Activity activity = getActivity();

if(activity instanceof DialogCloseListener)

((DialogCloseListener)activity).handleDialogClose(dialog);

Recycleritem [Link]:

package [Link];

import [Link];

import [Link];

import [Link];

import [Link];

import [Link];

import [Link];

import [Link];

import [Link];

34

import [Link];

import [Link];

import [Link];

import [Link];

import [Link].R;
public class RecyclerItemTouchHelper extends [Link] {

private ToDoAdapter adapter;

public RecyclerItemTouchHelper(ToDoAdapter adapter) {

super(0, [Link] | [Link]);

[Link] = adapter;

@Override

public boolean onMove(@NonNull RecyclerView recyclerView, @NonNull

[Link] viewHolder, @NonNull [Link] target) {

return false;

@Override

public void onSwiped(@NonNull final [Link] viewHolder, int direction)

final int position = [Link]();

if (direction == [Link]) {

[Link] builder = new [Link]([Link]());

[Link]("Delete Task");

[Link]("Are you sure you want to delete this Task?");

[Link]("Confirm",

new [Link]() {

35

@Override

public void onClick(DialogInterface dialog, int which) {

[Link](position);

});

[Link]([Link], new

[Link]() {

@Override

public void onClick(DialogInterface dialog, int which) {


[Link]([Link]());

});

AlertDialog dialog = [Link]();

[Link]();

} else {

[Link](position);

@Override

public void onChildDraw(@NonNull Canvas c, @NonNull RecyclerView recyclerView,

@NonNull [Link] viewHolder, float dX, float dY, int actionState, boolean

isCurrentlyActive) { [Link](c, recyclerView, viewHolder, dX, dY, actionState,

isCurrentlyActive);

Drawable icon;

ColorDrawable background;

View itemView = [Link];

int backgroundCornerOffset = 20;

36

if (dX > 0) {

icon = [Link]([Link](), [Link].ic_baseline_edit);

background = new ColorDrawable([Link]([Link](),

[Link]));

} else {

icon = [Link]([Link](),

[Link].ic_baseline_delete);

background = new ColorDrawable([Link]);

assert icon != null;

int iconMargin = ([Link]() - [Link]()) / 2;

int iconTop = [Link]() + ([Link]() - [Link]()) / 2;


int iconBottom = iconTop + [Link]();

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

int iconLeft = [Link]() + iconMargin;

int iconRight = [Link]() + iconMargin + [Link]();

[Link](iconLeft, iconTop, iconRight, iconBottom);

[Link]([Link](), [Link](),

[Link]() + ((int) dX) + backgroundCornerOffset, [Link]());

} else if (dX < 0) { // Swiping to the left

int iconLeft = [Link]() - iconMargin - [Link]();

int iconRight = [Link]() - iconMargin;

[Link](iconLeft, iconTop, iconRight, iconBottom);

[Link]([Link]() + ((int) dX) - backgroundCornerOffset,

[Link](), [Link](), [Link]());

37

} else { // view is unSwiped

[Link](0, 0, 0, 0);

[Link](c);

[Link](c);

Context appContext = [Link]().getTargetContext();

assertEquals("[Link]", [Link]());

Common questions

Powered by AI

In this Android To-Do application, the Activity and Fragment lifecycles play critical roles in managing UI states and resource allocations. MainActivity.java is primarily responsible for setting up the RecyclerView and its adapter upon creation. AddNewTask.java uses the Fragment lifecycle to handle its initial setup, such as inflating the view and configuring input listeners, during onCreateView and onViewCreated. Additionally, database connections are handled appropriately in their respective scopes to ensure resource efficiency and prevent leaks .

In RecyclerItemTouchHelper, color and drawable resources are used to provide visual feedback that enhances the user experience. During a swipe action, a ColorDrawable is set to either red for delete actions or a primary dark color for edit actions. Additionally, drawable icons are used depending on the swipe direction—a delete icon for left swipes and an edit icon for right swipes. These visual cues are drawn alongside the swiped items to immediately inform users of the action being initiated .

The RecyclerItemTouchHelper effectively enhances user experience by allowing intuitive swipe gestures to edit or delete items directly within the RecyclerView. It improves coding efficiency by centralizing swipe action handling and relying on callback methods to integrate these gestures seamlessly into the app. This approach not only simplifies the implementation of complex UI interactions but also maintains the flexibility to extend or modify behaviors without significantly altering existing code .

MainActivity.java ensures updates in the UI upon database changes by implementing the DialogCloseListener interface method handleDialogClose. This method is triggered when the AddNewTask dialog is closed, at which point it refreshes the task list by retrieving all tasks from the database, reversing the order for display, and notifying the adapter of the changes to update the RecyclerView .

The ItemTouchHelper enhances user interaction with the RecyclerView by enabling swipe gestures. It supports swiping items to the left or right, triggering actions like delete or edit. When an item is swiped to the left, an AlertDialog confirms the deletion, and the item is removed from the database. Swiping to the right initiates an edit action. These gestures improve the user interface by providing intuitive and direct manipulation of list items .

The application's architecture promotes scalability and maintainability through the separation of concerns and the use of modular components such as the ToDoAdapter, DatabaseHandler, and distinct activities and fragments. This structure allows for easy updates and extensions, like adding new features or modifying existing ones, without affecting other parts of the application. By encapsulating task management logic within the DatabaseHandler and operations related to UI updates in adapters and activities, the app is both scalable and easier to maintain .

In the To-Do application, data persistence is achieved through the use of a custom DatabaseHandler. This handler opens a connection to the SQLite database upon application startup and provides methods to retrieve, insert, update, and delete tasks. The DatabaseHandler ensures that task data is consistently stored and remains available across application sessions, maintaining the integrity and persistence of user data .

The MainActivity.java handles task additions by using a floating action button (fab), which, when clicked, triggers the creation of an AddNewTask fragment. This fragment allows users to input a new task. Once a task is entered and the save button is pressed, it either updates an existing task or inserts a new one into the database, depending on whether it is an update situation. Finally, it closes the dialog and refreshes the task list displayed in the RecyclerView .

When a task entry is attempted without valid input in AddNewTask.java, the save button remains disabled, preventing the action from proceeding. User feedback is provided through the visual change of the button's text color, which remains gray when there is no valid input (indicating it is not actionable), thus providing a clear visual cue to users about the lack of valid input .

In AddNewTask.java, task entry is validated through the use of a TextWatcher. This watches the input field and enables the save button only when the user has typed something. The button's visual state changes to indicate that an action is available. This mechanism prevents empty tasks from being saved by disabling the save button when the input field is empty .

You might also like