This simple example creates
a new to-do list application using
native Android View controls.Some of the features used
to create this application, includes
ArrayAdapters, ListViews, and KeyListeners.
following files are required ,
1.java file(Activity)
2.main.xml
3.Another xml holding the textview.(in
this ex: simple.xml)
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/edittext"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="enter to add to the list"/>
<ListView android:id="@+id/listView"
android:layout_width="fill_parent"
android:layout_height="wrap_content"/>
</LinearLayout>
Sample.xml
<?xml version="1.0" encoding="utf-8"?>
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/text"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
/>
import java.util.ArrayList;
import android.app.Activity;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.View;
import android.view.View.OnKeyListener;
import android.widget.ArrayAdapter;
import android.widget.EditText;
import android.widget.ListView;
public class to_do_list_activity extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
//get reference to UI widgets
final EditText text=(EditText) findViewById(R.id.edittext);
ListView list=(ListView) findViewById(R.id.listView);
//arraylist to store the items
final ArrayList<string> al=new ArrayList<string>();
// Create the array adapter to bind the array to the listview
final ArrayAdapter<string> aa=new ArrayAdapter<string>(this, R.layout.simple, al);
// Bind the array adapter to the listview.
list.setAdapter(aa);
text.setOnKeyListener(new OnKeyListener() {
public boolean onKey(View v, int keyCode, KeyEvent event) {
// TODO Auto-generated method stub
if(event.getAction()==KeyEvent.ACTION_DOWN)
if(keyCode==KeyEvent.KEYCODE_DPAD_CENTER)
{
al.add(0,text.getText().toString());
aa.notifyDataSetChanged();
text.setText("");
return true;
}
return false;
}
});
}
}

