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.
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
- 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.
- We create a private ArrayAdapter to bind the data to the List View using a ListAdapter interface.
- The function show_in_list() declares a List object containing names of movies.
- .add(String ) is used to add Strings to the arrayadapter.
- Finally in onCreate we set the data in the list using the setListAdapter(arrayadapter) function.