Introduction
In this tutorial, you’ll use Code on the Go (CoGo™) to write, build, and test an Android app with Java and XML. This includes the following:
- A specification for the app
- Step-by-step instructions for building the app in CoGo
- An explanation of how the code works
Specifically, you’ll build a to-do list app that lets you add tasks, view your list, and mark items as complete. Your tasks are saved automatically, so they will still be there when you close and reopen the app.
This tutorial uses a traditional approach to Android development, including Java for the app’s logic and XML for the user interface. The app contains five files. If you are new to programming, some of the concepts may be unfamiliar. That is fine. The complete code is provided for you to copy and paste, and each file is explained. To help you along, we’ve also published a video version of this tutorial:
Parts of the app
The finished app features the following:
- Add a task: The user types a task description into a text field and taps the Add button to add it to the list.
- View tasks: All added tasks are displayed in a scrollable list below the input area.
- Mark as complete: The user taps a checkbox next to a task to mark it as complete. Completed tasks are displayed with a strikethrough.
- Persistence: Tasks are saved automatically. When the user closes and reopens the app, all tasks and their completion status are preserved.
When you install it, the app will look like the image below.

Start the project
When you open CoGo, the main menu is displayed.
- On the main menu, tap Create a new project. The New project page is displayed. It contains templates with starter code for different kinds of projects.
- Tap the Empty Activity template. A configuration page for the project is displayed. (The Empty Activity template creates a standard Android project with a Java activity and an XML layout file.)
- In the App name field, enter ToDoApp. To use this tutorial’s sample code, you must enter the name exactly as shown here: one word, with a capital T, a capital D, and a capital A. (CoGo creates the project’s package name from the app name, and the code in this tutorial uses that package name.)
- In the Project language menu, make sure Java is selected.
- Leave the save location and the Minimum SDK menu at their default values.
- In the lower-right corner, tap the Create a new project button. CoGo will initialize the project.

What happens during initialization?
As part of project initialization, CoGo creates files and directories for the template you selected. During initialization, the top of the page displays a moving progress bar, while the bottom of the page displays status messages. For detailed information about the initialization tasks, swipe up from the bottom to display the output panel. Swipe down to hide the panel when you are done viewing it.
When the project has fully initialized, the “Project initialized” message is displayed at the bottom of the page. The Build icon (a green triangle) is displayed at the top of the page near the project’s name.
Your project files
This tutorial uses five files. Two of them already exist in the project, and you will create the other three. This process will be fully explained in the sections below.
| File | Location | Purpose | Action |
|---|---|---|---|
activity_main.xml | app → src → main → res → layout | The layout of the main screen | Already exists. Replace its contents. |
item_task.xml | app → src → main → res → layout | The layout of one row in the task list | Create it. |
MainActivity.java | app → src → main → java → com → example → todoapp | The main activity: sets up the screen, adds tasks, and saves and loads them | Already exists. Replace its contents. |
TaskAdapter.java | app → src → main → java → com → example → todoapp | Displays the list of tasks in each row | Create it. |
Task.java | app → src → main → java → com → example → todoapp | Represents a single to-do item | Create it. |
Open the existing files
Like most IDEs, CoGo provides a file browser to navigate the contents of your project. To use the file browser:
- Tap the Project menu, which is represented by the hamburger icon in the top left corner.
- Tap any of the folder icons to see the contents.
Use the locations in the table above to find MainActivity.java and activity_main.xml.
Create the new files
To create each new file, follow these steps:
- In the file browser, long-press the folder that will contain the file.
- Tap New File.
- Enter the file name exactly as shown in the table, and confirm.
Create TaskAdapter.java and Task.java in the todoapp folder, and create item_task.xml in the layout folder. In each case, follow the file paths in the table above.
Note: Some of the code you add first refers to the Task class, which you add in the last step. Because of this, you may see error underlines in the editor until all five files are complete.
Add the code
In each of the following steps, open the file, select all of its existing content (if there is any) and delete it. Then, paste in the code shown for that file (each section below contains code that you can copy and paste). Save each file when you are done.
Step 1: item_task.xml
Open item_task.xml and paste in the following code (for your convenience, simply tap the “Copy” button just below):
<?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"
android:orientation="horizontal"
android:padding="8dp"
android:gravity="center_vertical">
<CheckBox
android:id="@+id/checkboxTask"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<TextView
android:id="@+id/textViewTask"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:textSize="16sp"
android:paddingStart="8dp" />
</LinearLayout>
This layout defines a single row in the task list. It contains a CheckBox on the left and a TextView on the right. The TextView fills the remaining width of the row. The IDs checkboxTask and textViewTask allow the Java code to find these two elements. Save the file.
Step 2: activity_main.xml
Open activity_main.xml and paste in the following code.
<?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="match_parent"
android:orientation="vertical"
android:padding="16dp">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="My To-Do List"
android:textSize="24sp"
android:textStyle="bold"
android:paddingBottom="16dp" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<EditText
android:id="@+id/editTextTask"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:hint="Enter a task..."
android:inputType="text" />
<Button
android:id="@+id/buttonAdd"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Add"
android:layout_marginStart="8dp" />
</LinearLayout>
<androidx.recyclerview.widget.RecyclerView
android:id="@+id/recyclerViewTasks"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:layout_marginTop="16dp" />
</LinearLayout>
This layout has three parts:
- A title at the top of the screen.
- A horizontal row containing the text field (
editTextTask) and the Add button (buttonAdd). - A RecyclerView (
recyclerViewTasks) that fills the rest of the screen. A RecyclerView is Android’s component for displaying a scrollable list.
Save the file.
Step 3: TaskAdapter.java
Open TaskAdapter.java and paste in the following code.
package com.example.todoapp;
import android.graphics.Paint;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.CheckBox;
import android.widget.TextView;
import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;
import java.util.List;
public class TaskAdapter extends RecyclerView.Adapter<TaskAdapter.TaskViewHolder> {
public interface OnTaskChangedListener {
void onTaskChanged();
}
private final List<Task> tasks;
private final OnTaskChangedListener listener;
public TaskAdapter(List<Task> tasks, OnTaskChangedListener listener) {
this.tasks = tasks;
this.listener = listener;
}
@NonNull
@Override
public TaskViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
View view = LayoutInflater.from(parent.getContext())
.inflate(R.layout.item_task, parent, false);
return new TaskViewHolder(view);
}
@Override
public void onBindViewHolder(@NonNull TaskViewHolder holder, int position) {
Task task = tasks.get(position);
holder.textView.setText(task.getText());
holder.checkBox.setOnCheckedChangeListener(null);
holder.checkBox.setChecked(task.isCompleted());
if (task.isCompleted()) {
holder.textView.setPaintFlags(
holder.textView.getPaintFlags() | Paint.STRIKE_THRU_TEXT_FLAG);
} else {
holder.textView.setPaintFlags(
holder.textView.getPaintFlags() & ~Paint.STRIKE_THRU_TEXT_FLAG);
}
holder.checkBox.setOnCheckedChangeListener((buttonView, isChecked) -> {
task.setCompleted(isChecked);
if (isChecked) {
holder.textView.setPaintFlags(
holder.textView.getPaintFlags() | Paint.STRIKE_THRU_TEXT_FLAG);
} else {
holder.textView.setPaintFlags(
holder.textView.getPaintFlags() & ~Paint.STRIKE_THRU_TEXT_FLAG);
}
listener.onTaskChanged();
});
}
@Override
public int getItemCount() {
return tasks.size();
}
static class TaskViewHolder extends RecyclerView.ViewHolder {
CheckBox checkBox;
TextView textView;
TaskViewHolder(@NonNull View itemView) {
super(itemView);
checkBox = itemView.findViewById(R.id.checkboxTask);
textView = itemView.findViewById(R.id.textViewTask);
}
}
}
A RecyclerView does not know how to display your data on its own. An adapter connects the list of tasks to the RecyclerView and describes how each row looks. This is the most complex file in the project. The TaskAdapter class has three main responsibilities:
onCreateViewHolderinflates theitem_task.xmllayout, which creates a row.onBindViewHolderfills a row with data from the matching task. It sets the checkbox state and the task text, and it applies a strikethrough when the task is complete. When the user taps a checkbox, it updates the task and calls the listener’sonTaskChangedmethod. This tells the main activity to save the updated list.TaskViewHolderis an inner class that holds references to the checkbox and the text view in each row, so they do not need to be looked up repeatedly.
Save the file.
Step 4: MainActivity.java
Open MainActivity.java and paste in the following code.
package com.example.todoapp;
import android.content.Context;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.widget.Button;
import android.widget.EditText;
import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import org.json.JSONArray;
import org.json.JSONObject;
import java.util.ArrayList;
import java.util.List;
public class MainActivity extends AppCompatActivity {
private List<Task> tasks = new ArrayList<>();
private TaskAdapter adapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
loadTasks();
RecyclerView recyclerView = findViewById(R.id.recyclerViewTasks);
recyclerView.setLayoutManager(new LinearLayoutManager(this));
adapter = new TaskAdapter(tasks, this::saveTasks);
recyclerView.setAdapter(adapter);
EditText editText = findViewById(R.id.editTextTask);
Button addButton = findViewById(R.id.buttonAdd);
addButton.setOnClickListener(v -> {
String text = editText.getText().toString().trim();
if (!text.isEmpty()) {
tasks.add(new Task(text));
adapter.notifyItemInserted(tasks.size() - 1);
saveTasks();
editText.setText("");
}
});
}
private void saveTasks() {
SharedPreferences prefs = getSharedPreferences("todo_prefs", Context.MODE_PRIVATE);
JSONArray jsonArray = new JSONArray();
try {
for (Task task : tasks) {
JSONObject obj = new JSONObject();
obj.put("id", task.getId());
obj.put("text", task.getText());
obj.put("isCompleted", task.isCompleted());
jsonArray.put(obj);
}
prefs.edit().putString("tasks", jsonArray.toString()).apply();
} catch (Exception e) {
e.printStackTrace();
}
}
private void loadTasks() {
SharedPreferences prefs = getSharedPreferences("todo_prefs", Context.MODE_PRIVATE);
String json = prefs.getString("tasks", "[]");
try {
JSONArray jsonArray = new JSONArray(json);
for (int i = 0; i < jsonArray.length(); i++) {
JSONObject obj = jsonArray.getJSONObject(i);
Task task = new Task(obj.getString("text"));
task.setId(obj.getLong("id"));
task.setCompleted(obj.getBoolean("isCompleted"));
tasks.add(task);
}
} catch (Exception e) {
e.printStackTrace();
}
}
}
The main activity does four things:
- In
onCreate, it loads any saved tasks. - It sets up the RecyclerView with a layout manager and the adapter.
- It attaches a click listener to the Add button. When the button is tapped, the listener reads the text from the text field and, if the text is not empty, adds a new task to the list. It then notifies the adapter, saves the list, and clears the text field.
- The
saveTasksmethod converts the task list to a JSON string and stores it using SharedPreferences, which is Android’s built-in system for storing small amounts of data. TheloadTasksmethod reads the stored string and rebuilds the task list when the app starts.
Save the file.
Step 5: Task.java
Open Task.java and paste in the following code.
package com.example.todoapp;
public class Task {
private long id;
private String text;
private boolean completed;
public Task(String text) {
this.id = System.currentTimeMillis();
this.text = text;
this.completed = false;
}
public long getId() { return id; }
public void setId(long id) { this.id = id; }
public String getText() { return text; }
public boolean isCompleted() { return completed; }
public void setCompleted(boolean completed) { this.completed = completed; }
}
The Task class represents a single to-do item. Each task has an id generated from the current time, a text description, and a completed status. The getter and setter methods allow other classes to read and update these values. Save the file.
How the files work together
Now that all five files are in place, here is how they fit together:
Task.javaholds the information for one to-do item.MainActivity.javakeeps the list of tasks, handles the Add button, and saves and loads the list.activity_main.xmldefines the main screen, including the RecyclerView.TaskAdapter.javadisplays each task in the RecyclerView, using theitem_task.xmllayout for each row.- When the user taps a checkbox, the adapter updates the task and tells
MainActivity.java, which saves the list.
Build the app
When you have entered all of the code, tap the green Build icon in the action bar. When the build finishes, tap to install the app.

After the app is installed and launched, you can test it:
- Type a task description in the text field and tap Add. The task appears in the list.
- Add a few more tasks.
- Tap the checkbox next to a task to mark it as complete. The task text is displayed with a strikethrough.
- Close the app completely and reopen it. Your tasks are still there, exactly as you left them.
If the build fails
Swipe up from the bottom of the page to display the output panel, and use the Build Output tab to look for the error messages. Each message identifies the file and, in most cases, the line number. The most common causes are a typo, a missing line when pasting the code, or a file name that does not match exactly. Compare your files with the code on this page, and confirm that all five files have been saved.

Congratulations!
Using CoGo, you built a complete, fully usable to-do app with Java and XML.
From here, you could extend the app with additional features, such as:
- Task deletion: Allow the user to remove tasks from the list.
- Due dates: Add a date field to each task.
- Categories or priorities: Organize tasks into groups.
- Room database: For apps with larger amounts of data, Room is Android’s recommended solution for more robust persistence.