Android Simple ListView

In this session will learn use of simple ListView.

Simple ListView means we will use built in list row or layout.

What is ListView

ListView is an Android control that repeat a specific layout in the form of row.

Create new project in eclipse (Android-First-Programm).

Click on res folder and then layout, now open required xml where you want to use ListView.

eclipse-res-layout

We can create any control like ListView using both layouts

1) Graphical Layout

2) Xml Layout

1) Using “Graphical Layout” Drag ListView from “Composite” tab.

listView-graphical-layout

Double click on ListView and set its properties in xml layout.

2) In Xml Layout write this code to create ListView.


Now by reference we can use ListView in code and can use its events like click event or long press event.

Open Activity (.java file) to use that ListView.

Activity is located in src folder -> package and choose activity where you want to use ListView.

To access ListView from xml layout we use its reference using this code.

 

Using “findViewById();” we can access reference of any control like ListView from xml layout.

“listView1” is id of that ListView that we want to use and it must be exist in xml layout that we have assigned in “setContentView(R.layout.main);”. So complete code will be

 

listView1 is exists in main layout. We can set reference of layout using “setContentView(R.layout.main);”

Button click event:

To set buttons click event use this code.

To check is button is clicked show message using Toast.

 

Other way to set click event in button is, we can use function name in xml.

And define “button1Click” function in activity, without using button reference.

For your understanding I make two buttons and use button click events by two types.

See complete xml or java files.

 

Main.xml

AndroidButtonActivity.jave