A Simple Android Application Which Shows How to Show a List View And Toast Notification


If you are quite new to Android app development, I would recommend a simple android app for adding two numbers.
In this android application tutorial, we'll see how to use list view in android programming and how to display pop up notification using Toast function in android programming.

A Simple Android Application Which Shows How to Show a List View And Toast Notification


So first of all lets create a a new project in eclipse and let name it Listing App.Now after the project structure has been created navigate to the res/layout directory of the project and in that open the main.xml and paste the following code.

 <?xml version="1.0" encoding="utf-8" ?>
 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/content2" android:orientation="vertical" android:layout_width="fill_parent" android:layout_height="fill_parent">
 </LinearLayout>
After that add another file in the same res/layout directory called the list_item.xml and paste the following code in there

 <?xml version=”1.0″ encoding=”utf-8″?>
 <TextView xmlns:android=”http://schemas.android.com/apk/res/android”
    android:layout_width=”fill_parent”
    android:layout_height=”fill_parent”
    android:padding=”10dp”
    android:textSize=”16sp” >
 </TextView>
When we display a list view we populate the list_item.xml into our main.xml each time a new item is added into the list view.
After that open up your .java file from the src/yourcomfile/.java and paste the following code into that file.

import android.app.ListActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;
import android.widget.AdapterView.OnItemClickListener;
 
public class listingactivity extends ListActivity {
  /** Called when the activity is first created. */
  LinearLayout.LayoutParams layoutParams;
  LinearLayout ll;
  static int i;
  @Override
  public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setListAdapter(new ArrayAdapter(this, R.layout.list_item, NAMES));
 
    ListView lv = getListView();
    lv.setTextFilterEnabled(true);
 
    lv.setOnItemClickListener(new OnItemClickListener() {
 
      @Override
      public void onItemClick(AdapterView parent, View view, int position,
        long id) {
        // TODO Auto-generated method stub
        Toast.makeText(getApplicationContext(), ((TextView) view).getText(),
          Toast.LENGTH_SHORT).show();
      }
    });
  }
  static final String[] NAMES = new String[] {"
    Harry", "Ron", "Hermioni", "Snapes", "Dumbuldoree", "Weasley", "Potter", "Smaerty", "Lord Voldemort", "Smelly"
  };
}
Thats it now you are ready to run your small application.