Android ListView Part I


List Views are the most versatile of the widgets in Android as they can be used in many ways and customized according to need.List Views are used to list various options to the user or to show what's already there with the system and update it with time to time.Let's have a closer look at List Views.

Be patient and follow the road:

 1. Create a new Android Application Project

  • Go to File > New > Android Application Project.
  • Provide a name for the app and proceed as shown in previous blogs.
  • Select Empty Activity under the activity type.

Naming the app

 2.  Add a list view to activity_main.xml

  • Remove the Text View from the graphical layout saying Hello World.
  • In the Palette go to Composite > ListView.
  • Drag and drop the list view to the graphical window.
  • Re-size it accordingly.

Empty ListView

3.  Make some changes to activity_main.xml

  • Firstly change the background color of your relative layout to black(#000000), as shown in code below.
  • Secondly change the id of the ListView to "@andorid:id/list".

4. Open the MainActivity.java file

  • Open the MainActivity,java file from src folder in the package explorer.
  • Add the code shown below. Remember to change the package name.

5. Run the app

  • Run the app on a AVD or a physical device as demonstrated in previous blogs.


    Run As Android Application


ListView app

Code Explained:

  1. The MainActivity class of the app extends a ListActivity. ListActivity has a default layout that consists of a single, full-screen list in the center of the screen.
  2. We create a private ArrayAdapter to bind the data to the List View using a ListAdapter interface.
  3. The function show_in_list() declares a List object containing names of movies.
  4. .add(String ) is used to add Strings to the arrayadapter.
  5. Finally in onCreate we set the data in the list using the setListAdapter(arrayadapter) function.

Download Code:



SHARE

Shobhit Chittora

Hi there, I am Shobhit Chittora. I am a college student and like to develop apps for Android platform.I started this blog to share my experiences with Android development and may be help you guys on the “roadtodroid”.

  • Image
  • Image
    Blogger Comment
    Facebook Comment

0 comments:

Post a Comment