To do list app : ListActivity « UI « Android






To do list app

   

package app.test;

import java.util.ArrayList;

import android.app.Activity;
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.os.Bundle;
import android.util.AttributeSet;
import android.view.ContextMenu;
import android.view.KeyEvent;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnKeyListener;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.TextView;

class TodoListItemView extends TextView {

  private Paint marginPaint;
  private Paint linePaint;
  private int paperColor;
  private float margin;

  public TodoListItemView(Context context, AttributeSet attrs, int defStyle) {
    super(context, attrs, defStyle);
    init();
  }

  public TodoListItemView(Context context) {
    super(context);
    init();
  }

  public TodoListItemView(Context context, AttributeSet attrs) {
    super(context, attrs);
    init();
  }

  private void init() {
    marginPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
    marginPaint.setColor(Color.RED);
    linePaint = new Paint(Paint.ANTI_ALIAS_FLAG);
    linePaint.setColor(Color.BLUE);
    paperColor = Color.YELLOW;
    margin = Color.CYAN;
  }

  @Override
  public void onDraw(Canvas canvas) {
    canvas.drawColor(paperColor);
    canvas.drawLine(0, 0, getMeasuredHeight(), 0, linePaint);
    canvas.drawLine(0, getMeasuredHeight(), getMeasuredWidth(),
        getMeasuredHeight(), linePaint);
    canvas.drawLine(margin, 0, margin, getMeasuredHeight(), marginPaint);
    canvas.save();
    canvas.translate(margin, 0);
    super.onDraw(canvas);
    canvas.restore();
  }
}

public class Test extends Activity {
  private ArrayList<String> todoItems;
  private ListView myListView;
  private EditText myEditText;
  private ArrayAdapter<String> aa;
  private boolean addingNew = false;
  final int ADD_NEW_TODO = Menu.FIRST;
  final int REMOVE_TODO = Menu.FIRST + 1;

  @Override
  public void onCreate(Bundle icicle) {
    super.onCreate(icicle);
    setContentView(R.layout.main);
    myListView = (ListView) findViewById(R.id.myListView);
    myEditText = (EditText) findViewById(R.id.myEditText);
    todoItems = new ArrayList<String>();
    aa = new ArrayAdapter<String>(getApplicationContext(), R.layout.row,
        todoItems);
    myListView.setAdapter(aa);
    myEditText.setOnKeyListener(new OnKeyListener() {
      public boolean onKey(View v, int keyCode, KeyEvent event) {
        if (event.getAction() == KeyEvent.ACTION_DOWN)
          if (keyCode == KeyEvent.KEYCODE_DPAD_CENTER) {
            todoItems.add(0, myEditText.getText().toString());
            myEditText.setText("");
            aa.notifyDataSetChanged();
            cancelAdd();
            return true;
          }
        return false;
      }
    });

    registerForContextMenu(myListView);
  }

  @Override
  public boolean onCreateOptionsMenu(Menu menu) {
    super.onCreateOptionsMenu(menu);
    MenuItem itemAdd = menu.add(0, ADD_NEW_TODO, Menu.NONE, "add_new");
    MenuItem itemRem = menu.add(0, REMOVE_TODO, Menu.NONE, "remove");
    itemAdd.setIcon(R.drawable.icon);
    itemRem.setIcon(R.drawable.icon);
    itemAdd.setShortcut('0', 'a');
    itemRem.setShortcut('1', 'r');
    return true;
  }

  @Override
  public void onCreateContextMenu(ContextMenu menu, View v,
      ContextMenu.ContextMenuInfo menuInfo) {
    super.onCreateContextMenu(menu, v, menuInfo);
    menu.setHeaderTitle("Selected To Do Item");
    menu.add(0, REMOVE_TODO, Menu.NONE, "remove");
  }

  @Override
  public boolean onPrepareOptionsMenu(Menu menu) {
    super.onPrepareOptionsMenu(menu);

    int idx = myListView.getSelectedItemPosition();
    MenuItem removeItem = menu.findItem(REMOVE_TODO);
    removeItem.setTitle("remove");
    removeItem.setVisible(addingNew || idx > -1);
    return true;
  }

  @Override
  public boolean onOptionsItemSelected(MenuItem item) {
    super.onOptionsItemSelected(item);

    int index = myListView.getSelectedItemPosition();

    switch (item.getItemId()) {
    case (REMOVE_TODO): {
      if (addingNew) {
        cancelAdd();
      } else {
        removeItem(index);
      }
      return true;
    }
    case (ADD_NEW_TODO): {
      addNewItem();
      return true;
    }
    }

    return false;
  }

  @Override
  public boolean onContextItemSelected(MenuItem item) {
    super.onContextItemSelected(item);
    switch (item.getItemId()) {
    case (REMOVE_TODO): {
      AdapterView.AdapterContextMenuInfo menuInfo = (AdapterView.AdapterContextMenuInfo) item
          .getMenuInfo();
      int index = menuInfo.position;
      removeItem(index);
      return true;
    }
    }
    return false;
  }

  private void cancelAdd() {
    addingNew = false;
    myEditText.setVisibility(View.GONE);
  }

  private void addNewItem() {
    addingNew = true;
    myEditText.setVisibility(View.VISIBLE);
    myEditText.requestFocus();
  }

  private void removeItem(int _index) {
    todoItems.remove(_index);
    aa.notifyDataSetChanged();
  }
}

//main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
  >
  <EditText
    android:id="@+id/myEditText"
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    android:text=""
    android:visibility="gone"
  />
  <ListView  
    android:id="@+id/myListView"
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
  />
</LinearLayout>

//row.xml
<?xml version="1.0" encoding="utf-8"?>
<app.test.TodoListItemView 
  xmlns:android="http://schemas.android.com/apk/res/android"
  android:layout_width="fill_parent"
  android:layout_height="fill_parent"
  android:padding="10dp"
  android:scrollbars="vertical"
  android:fadingEdge="vertical"
/> 

   
    
    
  








Related examples in the same category

1.A list view example where the data for the list comes from an array of strings.
2.A list view example where the data comes from a cursor.
3.Using ListActivity
4.SimpleCursorAdapter and ListActivity
5.Get ListActivity selected index
6.Multi-column ListActivity
7.A list view example with separators.
8.List item click event
9.Dynamic List item
10.Self Wrapper for List
11.Static text for List view
12.List selection event
13.Simple List single choice
14.Scale listener
15.Weather List Widget
16.Demonstrates expandable lists backed by Cursors
17.Demonstrates expandable lists backed by a Simple Map-based adapter
18.Demonstrates the using a list view in transcript mode
19.Demonstrates how a list can avoid expensive operations during scrolls or flings.
20.Demonstrates how to write an efficient list adapter.
21.A list view where the last item the user clicked is placed in the "activated" state, causing its background to highlight.
22.Calculate the minimum and maximum values out of a list of doubles.
23.Bounded Linked List
24.Adapter that simply returns row views from a list.
25.Diary app