About
Home > Tutorials Home > Android > ToDoList in Android

ToDoList in Android

Published on November 30, -0001 12:00:00 AM

We are an online tutoring company that has laid its foundation in utilizing Multiple Intelligence methodology to understand specific needs and learning styles of a student.So that we patch a particular student to a tutor having a compatible teaching style. We have devised a smart online tutoring system to give you measurable results.Schools, cramped with students, fail to give your child the edge, the motivation and the right teacher guidance to help him find the true potential. We at Eonlinetutors.com make sure that your child gets the perfect learning environment to outperform.




Step 1 : Open the Eclipse and create a new Android project using Blank Activity application template. Give the application name "TODO LIST Example in Android".

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
	android:orientation="vertical"
	android:layout_width="fill_parent"
	android:layout_height="fill_parent"
	android:gravity="bottom">
	
    <ListView
		android:layout_width="fill_parent"
		android:layout_height="wrap_content"
		android:id="@+id/lv">
	</ListView>

	<LinearLayout
		android:layout_width="fill_parent"
		android:layout_height="wrap_content"
		android:orientation="horizontal">
		
		<Button
			android:layout_width="90dp"
			android:layout_height="wrap_content"
			android:text="Add"
			android:id="@+id/add">
		</Button>

		<EditText
			android:layout_width="140dp"
			android:layout_height="wrap_content"
			android:id="@+id/et">
		</EditText>

		<Button
			android:layout_width="90dp"
			android:layout_height="wrap_content"
			android:text="Edit"
			android:id="@+id/edit"
			android:enabled="false">
		</Button>
		
	</LinearLayout>
</LinearLayout>

Step 2 : Copy the Following Code into your .Java File.

package com.todolistexamle1;

import java.util.ArrayList;

import android.app.Activity;
import android.os.Bundle;
import android.view.ContextMenu;
import android.view.ContextMenu.ContextMenuInfo;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.AdapterView.AdapterContextMenuInfo;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.Toast;

public class todolistexamle1 extends Activity {
	private Button add;
	private Button edit;
	private EditText et;
	private ListView lv;
	ArrayList item=new ArrayList();
	
	/** Called when the activity is first created. */
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.main);

		add=(Button)findViewById(R.id.add);
		edit=(Button)findViewById(R.id.edit);
		et=(EditText)findViewById(R.id.et);
		lv=(ListView)findViewById(R.id.lv);

		registerForContextMenu(lv);

		add.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				String text=et.getText().toString();
				item.add(text);
				setuplist();
				et.setText("");
			}
		});

		edit.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v)  {
				item.remove(idd);
				String text=et.getText().toString();
				item.add(idd,text);
				setuplist();
			}
		});
	}

	protected void setuplist()  {
		lv.setAdapter(new ArrayAdapter(this,android.R.layout.simple_list_item_1,item));
	}

	@Override
	public void onCreateContextMenu(ContextMenu menu, View v,ContextMenuInfo menuInfo)  {

		super.onCreateContextMenu(menu, v, menuInfo);
		MenuItem m1=menu.add(0,1,1,"Remove Item");
		MenuItem m2=menu.add(0,2,2,"Edit item");
		MenuItem m3=menu.add(0,3,3,"Exit");
	}
	int idd;
	
	@Override
	public boolean onContextItemSelected(MenuItem item) {
		int id=item.getItemId();		

		if(id==1) {
			AdapterView.AdapterContextMenuInfo menuinfo;
			menuinfo=(AdapterContextMenuInfo)item.getMenuInfo();
			idd=menuinfo.position;
			removeitem(idd);
		}
		if(id==2) {
			AdapterView.AdapterContextMenuInfo menuinfo;
			menuinfo=(AdapterContextMenuInfo) item.getMenuInfo();
			idd=menuinfo.position;
			updatedata(idd);
		}
		if(id==3) {
			finish();
		}
		return super.onContextItemSelected(item);
	}

	private void removeitem(int idd) {
		item.remove(idd);
		setuplist();
	}

	private void updatedata(int idd)  {
		String text=item.get(idd);
		et.setText(text);
		add.setEnabled(false);
		edit.setEnabled(true);
	}
}
ALL COMMENTS (2)
Abhishek Bedre 2017-03-28 17:25:15
I have implemented my own "jarvis"....thanks to this example.        
2019-01-03 13:22:19
It is working fine in desktop but not working in android mobile