Android Open Source - NotAnotherTodoApp Main Activity






From Project

Back to project page NotAnotherTodoApp.

License

The source code is released under:

GNU General Public License

If you think the Android project NotAnotherTodoApp listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.

Java Source Code

/*
  NotAnotherTodoApp: Simple Todo List Android App
    Copyright (C) 2014  Tian Zhi Wang tianzhi@gmail.com
/*  w ww  .  j  a v  a 2 s.  com*/
    This program is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with this program.  If not, see <http://www.gnu.org/licenses/>.
*/

package ca.ualberta.cs.notanothertodoapp;

import java.util.ArrayList;
import java.util.Collection;
import java.util.Iterator;

import android.R.color;
import android.app.Activity;
import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.Toast;
import android.widget.AdapterView.OnItemLongClickListener;
import android.widget.AdapterView.OnItemClickListener;


public class MainActivity extends Activity {
  /* The Main Activity where user adds todos, 
   * views todos, emails and archives todos */
  
  //List of Selected Todos
  private TodoList selectedList;
  //List of Archived Todos that will be sent to archiveActivity
  private ArrayList<Todo> archiveListPackage;
  
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        
        final ListView listView = (ListView) findViewById(R.id.todoList);
        Collection<Todo> todos = TodoListController.getTodoList().getTodos();
        selectedList = new TodoList();
        
        //Buttons
        Button selectAllButton = (Button) findViewById(R.id.buttonSelectAll);
        Button archiveButton = (Button) findViewById(R.id.buttonArchive);
        Button emailButton = (Button) findViewById(R.id.buttonEmail);
        
        
        final ArrayList<Todo> list = new ArrayList<Todo>(todos);
        final ArrayList<Todo> archiveList = new ArrayList<Todo>(todos);
        final TodoListAdapter customAdapter = new TodoListAdapter(this, R.layout.todo_layout, list);
        listView.setAdapter(customAdapter);
        
        //Updates Todos with a listener
        TodoListController.getTodoList().addListener(new Listener() {
          @Override
          public void update(){
            list.clear();
            Collection<Todo> todos = TodoListController.getTodoList().getTodos();
            list.addAll(todos);
            customAdapter.notifyDataSetChanged();
          }
        });
        
        //Selects Todos on Click
        listView.setOnItemClickListener(new OnItemClickListener() {
      @Override
      public void onItemClick(AdapterView<?> adapterView, View view,
          int position, long id) {
        Todo todo = list.get(position);
        if (!selectedList.getTodos().contains(todo)) {
          selectedList.addTodo(todo);
          view.setBackgroundColor(Color.CYAN);
          customAdapter.notifyDataSetChanged();
          Toast.makeText(MainActivity.this, "Selected", Toast.LENGTH_SHORT).show();
        } else {
          selectedList.removeTodo(todo);
          view.setBackgroundColor(color.background_light);
          customAdapter.notifyDataSetChanged();
          Toast.makeText(MainActivity.this, "Unselected", Toast.LENGTH_SHORT).show();
        }
      }      
    });
        
        //Deletes Todos on Long Click
        listView.setOnItemLongClickListener(new OnItemLongClickListener() {
      @Override
      public boolean onItemLongClick(AdapterView<?> adapterView, View view,
          int position, long id) {
        Todo todo = list.get(position);        
        TodoListController.getTodoList().removeTodo(todo);
        Toast.makeText(MainActivity.this, "Deleted", Toast.LENGTH_SHORT).show();
        return false;
      }      
    });
        
        //Archives The Selected Todos
        archiveButton.setOnClickListener(new View.OnClickListener() {
      public void onClick(View view) {
          Toast.makeText(MainActivity.this,"Archived",Toast.LENGTH_SHORT).show();
          for (Todo todo: selectedList.getTodos()) {
            archiveList.add(todo);
            TodoListController.getTodoList().removeTodo(todo);
          }
          customAdapter.notifyDataSetChanged();
          listView.setBackgroundColor(color.background_light);
          archiveListPackage = archiveList;
          selectedList.clear();
      }      
    });
        
        //Selects all Todos
        selectAllButton.setOnClickListener(new View.OnClickListener() {
      public void onClick(View view) {
          Toast.makeText(MainActivity.this,"Selected All",Toast.LENGTH_SHORT).show();
          for (Todo todo: list) {
            selectedList.addTodo(todo);
          }
          listView.setBackgroundColor(Color.CYAN);
      }      
    });
        
        //Emails all Todos
        // Code from User fiXedd http://stackoverflow.com/users/76835/fixedd 2014
        // http://stackoverflow.com/questions/2197741/how-can-i-send-emails-from-my-android-application
    emailButton.setOnClickListener(new View.OnClickListener() {
      public void onClick(View view) {
          Toast.makeText(MainActivity.this,"Emailing Selected",Toast.LENGTH_SHORT).show();
          Intent i = new Intent(Intent.ACTION_SEND);
          i.setType("message/rfc822");
          i.putExtra(Intent.EXTRA_EMAIL  , new String[]{"recipient@example.com"});
          i.putExtra(Intent.EXTRA_SUBJECT, "Your Todos");
          i.putExtra(Intent.EXTRA_TEXT   , selectedList.toString());
          try {
              startActivity(Intent.createChooser(i, "Send mail..."));
          } catch (android.content.ActivityNotFoundException ex) {
              Toast.makeText(MainActivity.this, "There are no email clients installed.", Toast.LENGTH_SHORT).show();
          }
      }      
    });
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();
        if (id == R.id.action_settings) {
            return true;
        }
        return super.onOptionsItemSelected(item);
    }
    
    //Menu item to show archived todos
    public void showArchive(MenuItem menu) {
      Toast.makeText(this,"Showing Archived Todos",Toast.LENGTH_SHORT).show();
      Intent intent = new Intent(MainActivity.this, ArchiveActivity.class);
      intent.putExtra("ArchiveList", archiveListPackage);
      startActivity(intent);  
    }
    
  //Menu item to show all todos
    public void showAll(MenuItem menu) {
      Toast.makeText(this,"Showing All Todos",Toast.LENGTH_SHORT).show();
      Intent intent = new Intent(MainActivity.this, AllTodosActivity.class);
      startActivity(intent);
    }
    
    //Button to add todo
    public void addTodo(View v) {
      TodoListController tl = new TodoListController();
      EditText textview = (EditText) findViewById(R.id.todoTextView);
      String todoString = textview.getText().toString();
      if (!todoString.equals("")) {
        tl.addTodo(new Todo(todoString));
        textview.setText("");
        Toast.makeText(this,"Added",Toast.LENGTH_SHORT).show();
      }
    }
}




Java Source Code List

ca.ualberta.cs.notanothertodoapp.AllTodosActivity.java
ca.ualberta.cs.notanothertodoapp.ArchiveActivity.java
ca.ualberta.cs.notanothertodoapp.Listener.java
ca.ualberta.cs.notanothertodoapp.MainActivity.java
ca.ualberta.cs.notanothertodoapp.TodoListAdapter.java
ca.ualberta.cs.notanothertodoapp.TodoListController.java
ca.ualberta.cs.notanothertodoapp.TodoList.java
ca.ualberta.cs.notanothertodoapp.Todo.java