This course will teach you how to develop a simple To-Do List app using Java. It is aimed at individuals with basic knowledge of Android application development and will cover various concepts such as classes, lists, and databases. By the end of the course, you will be able to complete a basic To-Do List app.
1. Setting Up the Android App Development Environment
To develop Android apps, you need to install an integrated development environment (IDE) called Android Studio. Android Studio is officially supported by Google and optimized for Android app development. Here are the basic steps to set up the development environment:
- Visit the official Android Studio website and download the latest version.
- Run the downloaded file to install it.
- Once the installation is complete, run Android Studio.
- Install the SDK (Software Development Kit).
2. Creating a Project
To create a new project in Android Studio, follow these steps:
- Run Android Studio and select “Start a new Android Studio project.”
- Select “Empty Activity” and click “Next.”
- Specify the project name and set the package name, storage location, etc. Select “Java” as the language.
- Click “Finish” to create the project.
3. Designing the UI
Now let’s design the UI. The basic UI components of the To-Do List app are as follows:
- EditText for inputting tasks
- Button to add tasks
- ListView to display the list of tasks
3.1 Modifying the XML Layout File
Open the res/layout/activity_main.xml file in Android Studio and modify it as follows:
<?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"> <EditText android:id="@+id/editTextTask" android:layout_width="match_parent" android:layout_height="wrap_content" android:hint="Please enter a task" /> <Button android:id="@+id/buttonAdd" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Add" android:layout_below="@id/editTextTask" /> <ListView android:id="@+id/listViewTasks" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_below="@id/buttonAdd" /> </RelativeLayout>
4. Writing Java Code
After creating the layout, let’s write the Java code to add functionality. Open the MainActivity.java file, which is the main activity, and implement it as follows:
package com.example.todolist; import android.os.Bundle; import android.view.View; import android.widget.ArrayAdapter; import android.widget.Button; import android.widget.EditText; import android.widget.ListView; import androidx.appcompat.app.AppCompatActivity; import java.util.ArrayList; public class MainActivity extends AppCompatActivity { private EditText editTextTask; private Button buttonAdd; private ListView listViewTasks; private ArrayList<String> tasks; private ArrayAdapter<String> adapter; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); editTextTask = findViewById(R.id.editTextTask); buttonAdd = findViewById(R.id.buttonAdd); listViewTasks = findViewById(R.id.listViewTasks); tasks = new ArrayList<>(); adapter = new ArrayAdapter<>(this, android.R.layout.simple_list_item_1, tasks); listViewTasks.setAdapter(adapter); buttonAdd.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { String task = editTextTask.getText().toString(); if (!task.isEmpty()) { tasks.add(task); adapter.notifyDataSetChanged(); editTextTask.setText(""); } } }); } }
5. Running the App
After writing the code, follow these steps to run the app:
- Click the “Run” button in the toolbar of Android Studio.
- Select an emulator or a physical device.
- Once the app runs successfully, enter a task in the input field and click the “Add” button to confirm that the task is added to the list.
6. Implementing Additional Features
The basic To-Do List app is now complete. However, you can enhance the app by implementing additional features. Here are some features you can add:
- Functionality to delete items from the task list
- Functionality to mark items as completed
- Implementing a database so that data persists even after exiting the app
6.1 Implementing the Delete Functionality
You can add functionality to delete an item when long-clicking an item in the ListView. Please add the following code to MainActivity.java:
listViewTasks.setOnItemLongClickListener(new AdapterView.OnItemLongClickListener() { @Override public boolean onItemLongClick(AdapterView<?> parent, View view, int position, long id) { tasks.remove(position); adapter.notifyDataSetChanged(); return true; } });
6.2 Implementing the Completed Marking Feature
It is also useful to add a check mark to tasks when completed. Using RecyclerView makes it easier to manage a checklist, and you can add checkboxes for each item.
6.3 Integrating SQLite Database
To ensure that tasks persist even after exiting the app, you can use an SQLite database. You can create a SQLiteOpenHelper class to manage the database.
Conclusion
In this course, you learned how to develop an Android To-Do List app using Java. We covered not only how to create a basic app but also how to implement additional features. These basic components and concepts will serve as a foundation for developing more complex apps in the future. Continuous learning is essential in Android app development, so please try various projects.