Create a simple Todo app in Android


Today, we’re gonna make a simple Todo app for Android using Android Studio 3. At first, this app only allows the user to add task without checking for completing or removing task. This will be done in Java and lately, I will make the same app using Kotlin.

Check out my full video here: (just be aware that my Englist is bad)

Create the App Layout

The first thing we need to do is lay out our application, thanks to Android studio, this hasn’t been that easy before. I suppose that all of you know how to set up an Android Studio project.

We’ll need these three main components:

  • EditText: where user types in a task they want to do
  • Button: where the user taps to add the task into the list
  • Listview: where list all the tasks of user’s

Should give every component a meaning id so that you can easily recall it while doing the Java code.

With button, don’t forget to give a value to onClick attribute, this value will be the name of the function you will define later on to be trigger when the button is hit.

You can take my layout by copying the XML code below and replace your activity_main.xml code with it. You can see the XML screen by clicking the XML tab in the bottom.

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android=""

android:inputType="textPersonName" />

android:text="@string/buttonText" />

android:layout_marginTop="107dp" />

Note that in my activity_main.xml I have 2 string variable for button name android:text="@string/buttonText" and text input placeholder android:hint="@string/placeholder". Those variables are defined in res/values/strings.xml

I also assigned the onClick attribute of the button with a function name “onClickAdd” you can name it whatever you want, and you will use it later in your code. android:onClick="onClickAdd"

<string name="app_name">TodoAppJava</string>
<string name="buttonText">Add</string>
<string name="placeholder">type here..</string>

Start to code

Now, we go to the file or whatever your main java file for the main screen is. I will show you the code first than explain later.

package com.samderlust.todoappjava;

import android.os.Bundle;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.ListView;
import android.widget.TextView;

import java.util.ArrayList;

public class MainActivity extends AppCompatActivity {

Button button;
EditText inputText;
ListView listView;
ArrayList<String> list;

public void onClickAdd(View v){

String text = inputText.getText().toString();
ArrayAdapter adapter = new ArrayAdapter<>(this,android.R.layout.simple_list_item_1,list);

protected void onCreate(Bundle savedInstanceState) {

button= findViewById(;
list = new ArrayList<>();

1. Import things:

We need to import the API and components that we need to use for making the application

2. Declare Component

  • Button button;
  • EditText inputText;
  • ListView listView;
  • ArrayList<String> list;

We declare the components in the top of the class since we want to use them in many functions inside the class. As you know, if we declared it inside a function, it couldn’t be accessed from other function. You will be more clear about this when we go on.

3. onCreate Function

This function is auto-generated with the java file, this function will be call at first when the application runs. It’s similar to the main function in a Java application.

That’s why we define all the component inside this method. Remember when we made the layout, we give each component an Id, this is the time we need it. The function findViewById allows you to assign the layout component to a Java variable. It takes one parameter, which is the Id of the component and we provide the Id follow

  • button= findViewById(;
  • inputText=findViewById(;
  • listView=findViewById(;

We use the arraylist list = new ArrayList<>(); to stored all the task that user add to the list. Later on, we will render all elements inside this arraylist to the ListView layout component and that’s how all the items will be display on the screen.

4. onClickAdd function

When we created a button, we give set the attribute onClick = onClickAdd, and we have to define this function so it will be trigger every time the button is tapped.

When user input something, this function will add it to the list, therefore we need to access some layout components inside this function.

  • String text = inputText.getText().toString(): We create a new variable to hold the text of the inputText field.
  • list.add(text); we add the text to the arraylist.
  • ArrayAdapter adapter = new ArrayAdapter<>(this,android.R.layout.simple_list_item_1,list); we need the ArrayAdapter to make data in an arraylist could be sync to the ListView component. The constructor function of ArrayAdapter takes 3 arguments: the context of the class (this), 2nd argument defines the layout of the list, the last argument is the data itself.
  • listView.setAdapter(adapter): Tell the ListView component to take the data from the array adapter and show it to the screen.

So far, we can run the application we there is an issue we need to fix.

After adding an item, users need to delete the text in the input field to make new items. we solve it by setting the text of the field to be empty at the end of the function. inputText.setText("");

A user can add an empty item: we solve this by using an if statement to check the value of the input field. If it’s not empty we will allow the function to work as normal. If it’s not, just do nothing.

So, the refactored onClickAdd function will look as below:

public void onClickAdd(View v){

String text = inputText.getText().toString();

ArrayAdapter adapter = new ArrayAdapter<>(this,android.R.layout.simple_list_item_1,list);


Please enter your comment!
Please enter your name here