Shopping list app Android Studio

This application displays a list of items with the ability to add items to the list using an EditText widget. The list is displayed using a ListView, which obtains the data for the list using an Adapter class. Often a custom Adapter will be created and used for managing items in a list, but we will use the provided ArrayAdapter class. Items can be added to or removed from this ArrayAdapter similar to a normal array, but each time the content in the ArrayAdapter changes the ListView will create or remove Views accordingly.

The EditText widget is a View that you can type into to change the text. In this application a Button widget next to the EditText can be pressed to add the current contents of the EditText to the ListView. To add this functionality an OnClickListener must be registered to the Button. An OnClickListener is simply a class that contains a public void onClick(View v) method to be used as a callback whenever the Button is clicked. For simplicity the Activity class implements the OnClickListener interface.

The layout of this application contains several Views arranged within a RelativeLayout. By using RelativeLayout additional alignment options are available for arranging Views, for example in this application the Button is placed on the right side of the screen using the layout_alignParentRight attribute and the EditText is placed to the left of the Button using the layout_toLeftOf attribute.

Download the source of the project by clicking here.

  • First take a look at the strings.xml file located in the res/values directory. This file contains string resources that can be used by the application. The resources can be accessed both in the Java code for the project and in other xml files such as layout

  • Open ShoppingList.java located in the src/ directory in the edu.calpoly.android package

  • Initialization of the layout and widgets happens in the public void onCreate(Bundle savedInstanceState) method

    • The ArrayAdapter used to store items in the list is initialized using an array from the strings.xml resource file

    • The ListView has its Adapter set to the ArrayAdapter member variable

  • The public void onClick(View v) method is the callback used when the Button is clicked, this method retrieves the text from the EditText and adds it to the list

  • The public void onKey(View v) method allows us to override the default behavior of key presses, specifically when the enter key or trackball is pressed with the EditText in focus it should respond as if the Button were pressed

  • Once you understand the operation of the application attempt to run it on an emulator or device

To modify the application you will be adding the ability to remove items from the list. After completing this task items can be removed by long clicking on an item on the list and selecting Remove from the menu that appears. This will require the addition of three new methods.

  • The first step in removing the item is determining which item was pressed, to do this an OnItemLongClickListener must be set for the ListView, to keep track of which item was clicked, create an int member variable to store the selected position

    • You will need to make this Activity implement OnItemLongClickListener by adding the method:
      public boolean onItemLongClick(AdapterView parent, View v, int position, long id)

      • Set your new member variable equal to the position parameter and return false so that this long click event is not consumed

    • In the onCreate method add the line m_vwList.setOnItemLongClickListener(this) after m_vwList is initialized so that the new onItemLongClick method will be used

  • Before creating the menu we will need a method to handle menu item clicks, make the Activity implement OnMenuItemClickListener and add the following method:
    public boolean onMenuItemClick(MenuItem item)

    • In this method you will remove the selected item from the list by calling m_adapter.remove(String object), take a look at the method ArrayAdapter.getItem method for determining the String object from the position. Return true once this is done

  • Now add the method:
    public void onCreateContextMenu(ContextMenu menu, View v, ContextMenuInfo menuInfo)
    This method will called when you press and hold on an item in the list and display a menu

    • Add the line super.onCreateContextMenu(menu, v, menuInfo)

    • Create a new MenuItem by calling menu.add(Remove), this will create a new item in the menu with the text Remove

    • Set your newly created onMenuItemClick method to handle when this menu item is clicked by calling MenuItem.setOnMenuItemClickListener(this)

  • Finally add the following line to the onCreate method:
    registerForContextMenu(m_vwList)
    This will let the application know that when the ListView is long clicked a context menu should be created by calling onCreateContextMenu

  • Test the application to ensure that it works correctly, pressing and holding on an item should bring up a screen similar to the image below.

Shopping list app Android Studio