Opening windows (i.e. the webbrowser) / reacting on Clicks
What is this: This tutorial shows how to open windows (in this case the webbrowser), by sending Intents (android.intent.action.VIEW) to the System.
What you learn: You will learn how easy it is, to react on clicks to a ListView and how to open new pages programmatically through so called Intents.
See also: The Anatomy of an Android Application
Difficulty: 1 of 5

What it will look like:



Description:
What we will do is:
- Create a ListView containing some Search-Keywords.
- React on the listItem-Clicks
- Open a webpage (anddev.org) and search for the keyword selected in the ListView
As we are extending the ListActivity (see ListActivitiy-FAQ here)
On the creation of our Application we want some search-keywords appear in a list.
Using java Syntax Highlighting
- public void onCreate(Bundle icicle) {
- super.onCreate(icicle);
- // Create an array of Strings, that will be put to our ListActivity
- String[] mStrings = new String[]{"Android", "Google", "Eclipse"};
- // Create an ArrayAdapter, that will actually make the Strings above appear in the ListView
- this.setListAdapter(new ArrayAdapter<String>(this,
- android.R.layout.simple_list_item_1, mStrings));
- }
Parsed in 0.032 seconds, using GeSHi 1.0.8.4
As we have to react on clicks to the listview-items, we have to @override onListItemClick(...):
What is done in there, is that we read out the String, that the ArrayAdapter put to the Listview for us.
After that we create a so called Intent. So what is an Intent (see javadoc)...
Android Javadoc wrote:An Intent provides a facility for performing late runtime binding between the code in different applications. Its most significant use is in the launching of activities, where it can be thought of as the glue between activities. It is basically a passive data structure holding an abstract description of an action to be performed.
So we say to the system: I want to "android.intent.action.VIEW" a ContentURI "http://anddev.org/search.php?mode=results&search_keywords='" + keyword + "'")); do whatever is needed to acomplish that. And do it now: startActivity(myIntent);
Using java Syntax Highlighting
- @Override
- protected void onListItemClick(ListView l, View v, int position, long id){
- super.onListItemClick(l, v, position, id);
- // Get the item that was clicked
- Object o = this.getListAdapter().getItem(position);
- String keyword = o.toString();
- // Create an VIEW intent
- Intent myIntent = null;
- try {
- // The intent will open our anddev.org-board and search for the keyword clicked.
- myIntent = new Intent("android.intent.action.VIEW",
- new ContentURI("http://anddev.org/search.php?mode=results&search_keywords='"
- + keyword + "'"));
- } catch (URISyntaxException e) {
- e.printStackTrace();
- }
- // Start the activity
- startActivity(myIntent);
- }
- }
Parsed in 0.033 seconds, using GeSHi 1.0.8.4
The Full SourceCode
Using java Syntax Highlighting
- package org.anddev.android.listviews;
- import java.net.URISyntaxException;
- import android.app.ListActivity;
- import android.content.Intent;
- import android.net.ContentURI;
- import android.os.Bundle;
- import android.view.View;
- import android.widget.ArrayAdapter;
- import android.widget.ListView;
- public class ListViewDemo extends ListActivity {
- /** Called when the activity is first created. */
- @Override
- public void onCreate(Bundle icicle) {
- super.onCreate(icicle);
- // Create an array of Strings, that will be put to our ListActivity
- String[] mStrings = new String[]{"Android", "Google", "Eclipse"};
- // Create an ArrayAdapter, that will actually make the Strings above appear in the ListView
- this.setListAdapter(new ArrayAdapter<String>(this,
- android.R.layout.simple_list_item_1, mStrings));
- }
- @Override
- protected void onListItemClick(ListView l, View v, int position, long id){
- super.onListItemClick(l, v, position, id);
- // Get the item that was clicked
- Object o = this.getListAdapter().getItem(position);
- String keyword = o.toString();
- // Create an VIEW intent
- Intent myIntent = null;
- try {
- // The intent will open our anddev.org-board and search for the keyword clicked.
- myIntent = new Intent("android.intent.action.VIEW",
- new ContentURI("http://anddev.org/search.php?mode=results&search_keywords='"
- + keyword + "'"));
- } catch (URISyntaxException e) {
- e.printStackTrace();
- }
- // Start the activity
- startActivity(myIntent);
- }
- }
Parsed in 0.040 seconds, using GeSHi 1.0.8.4
Regards,
plusminus






