Loading, please wait...

A to Z Full Forms and Acronyms

How to create a Todo List in Android | Android Tutorials

Oct 20, 2020 Android | Android Studio | todoList, 12263 Views
Creating a Todolist application in Android

How to create your own todo list in android? 

In this article, you will learn how you can create your own application. You will get in touch with many concepts like Toast and AleartDialog. You'll learn how to use all these concepts in one application.  

PREREQUISITE:

  • Installation of Android Studio. 
  • Must be aware of Java language. 
  • Basic concepts of android.

WHAT IS todo list?

Before making an application a todo list we must know about it. It an application where we write can create the list such as shopping list, book list, and many more. If you are an Android user, you can see in your play store there are so many todo list application exist such as TickTick, Anydo, Todoist, and more. Why we use the other's application when we can create our application with the help of android. 

PRINCIPAL ELEMENTS USED IN MAKING OF A todo list:

  • LinearLayout: It is used as a Layout. It helps in aligning the elements properly. You can give the orientation either horizontally and vertically with the help of 
    android:orientation="vertical/horizontal" 
  • EditText: EditText element block is used to take input from the user. The main work of EditText is to provide the space to the user through which the user will enter the input. 
  • Button: The Button is used to add the user's input into the list. You can use other things also in the place of Button. But it must function like a button i.e it should add the value of an item. (In the given code, ImageView is used n place of the button but you will see it will perform the same operation. You can replace the ImageView with the Button just to check whether it is working fine.)
  • ListView: The listView Widget is used to display the Item or Value entered by the user with the EditText and added through the button. ListView is a predefined Widget in Android. It is designed to show the input in the form of a list.  
  • <?xml version="1.0" encoding="utf-8"?>
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:app="http://schemas.android.com/apk/res-auto"
        xmlns:tools="http://schemas.android.com/tools"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical"
        tools:context=".MainActivity">
    
        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="63dp"
            android:layout_marginTop="10dp"
            android:layout_marginBottom="10dp"
            android:orientation="horizontal">
    
            <EditText
                android:id="@+id/text_edit"
                android:layout_width="262dp"
                android:layout_height="match_parent"
                android:hint="Enter the item" />
    
            <ImageView
                android:id="@+id/add_text"
                android:layout_width="127dp"
                android:layout_height="67dp"
                android:src="@drawable/plus" />
        </LinearLayout>
    
        <ListView
            android:id="@+id/listview"
            android:layout_width="434dp"
            android:layout_height="wrap_content"
            android:layout_marginBottom="12dp" />
    
    </LinearLayout>​
package com.example.todo_list;

import androidx.appcompat.app.AlertDialog;
import androidx.appcompat.app.AppCompatActivity;

import android.content.DialogInterface;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.Toast;

import java.util.ArrayList;


public class MainActivity extends AppCompatActivity implements View.OnClickListener, AdapterView.OnItemLongClickListener {
    private EditText ed;
    private ImageView adding_items;
    private ListView item_list;
    private ArrayList<String> values=new ArrayList<String>(); //ArrayList takes the user input
    private ArrayAdapter<String> adapter; // ArrayAdapter is used to insert a value in ListView
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        ed = findViewById(R.id.text_edit);
        adding_items = findViewById(R.id.add_text);
        item_list = findViewById(R.id.listview);

        adding_items.setOnClickListener(this);
        item_list.setOnItemLongClickListener(this); // Giving the reference of the onclick function 
    }

    @Override
    public void onClick(View view) {
        String add_item=ed.getText().toString();
        if(values.contains(add_item))// Checks the condition if item exists return the message displayed on Toast
        {
            Toast.makeText(getBaseContext(),"Item Already Exist", Toast.LENGTH_LONG).show();
    } // Enter the element if it does not exist  
      else
        {
            values.add(add_item);
            adapter=new ArrayAdapter<String>(MainActivity.this,android.R.layout.simple_list_item_1,values);
            item_list.setAdapter(adapter);
            ed.setText("");
        }
    }

   @Override
    public boolean onItemLongClick(AdapterView<?> adapterView, View view, int position, long l) {
        final int removing_item=position;
            AlertDialog.Builder builder = new AlertDialog.Builder(MainActivity.this); // Ask the user to get the confirmation before deleting an item from the listView
            builder.setMessage("Do you want to delete").setPositiveButton("Ok", new DialogInterface.OnClickListener() {
                @Override
                public void onClick(DialogInterface dialogInterface, int i) {
                    values.remove(removing_item);
                    adapter.notifyDataSetChanged();
                    Toast.makeText(getBaseContext(), "Item Deleted", Toast.LENGTH_LONG).show();
                }
            }).setNegativeButton("Cancel", null).show();



        return true;
    }
}

WORKING OF todo list IN MainActivity.java FILE:

  • Initially, create the object of elements such as EditText, ListView, and Button with the private access specifier. Allegedly private access specifier is used to taking the rights from the classes to use the element other than the class in which it is declared. 
  • ArrayList and ArrayAdapter also declare an object is created. ArrayList is taking the input which is entered by the user in EditText and ArrayAdapter will bring the input to the listView. With the ArrayApadter the text is visible to the user. These two play a key role in the making of a todo list application. 
  • Assigning the referring ids of elements to created objects. 
  • Call the onClick function to add the Values. 
  • In the onClick function, the condition is applied and the concept of Toast is used. If the entered element already exists in the list, the Toast will display a message "Item Already Exist". If the element does not exist it will enter the value. 
  • Call the OnItemLongClick function to remove the Values from the list. 
  • In OnItemLongClick, the concept of AlertDialog is used. It is used to get the confirmation from the user whether they want to delete the item from the list. If the user will enter on "OK", then the item will be removed from the list else on the click on "Cancel" it will return null. 

OUTPUT:

   

    

 

A to Z Full Forms and Acronyms