Welcome again to Android ListView. What we are gonna do is build up on the progress of Part I, and add some new concepts such as Intents and SearchManager to it. So let's begin:
1. Create a new Android Application Project
- Follow up on Part I for the basic app setup process or create new by going to File > New > Android Application Project.
|New Android Appication|
2. Add List View to activity_main.xml
- Add the ListView to the graphical layout of the app from the Palette by dragging and dropping from the Composite Section.
- Remember to set the android:id to "@andorid:id/list".
3. Code away the MainActivity.java
- Add the code below to your MainActivity.java.
- For code explanation scroll to the end of the post.
4. Run the app
- The java code in this tutorial builds up on the ListView Part I. The new thing here is that this app now can search the name in the list item clicked onto the Internet.
- This is accomplished by adding an Intent to the app. Intents are used to describe run time actions to be done by the app. Through Intents we can call any other activity or perform any default actions provided by the system (e.g, Intent.ACTION_WEB_SEARCH).
- We add this Intent in the onListItemClick() method of the ListActivity which is called when any list item is selected by clicking touching.
- Another new thing here are the Toasts. A Toast is like a real toast which pops up to notify you about your choices or any thing else. The function Toast.makeText( Context, String, Length).show() is used to display Toasts.
- Now we make a search string, put it in the Extras field of our intent under the SearchManager.Query name. Its similar to a name:value pair.
- Finally we start the intent by calling startActivity(my_intent).