Opening windows (i.e. webbrowser) / reacting on Clicks

Basic Tutorials concerning: GUI, Views, Activites, XML, Layouts, Intents, ...

Opening windows (i.e. webbrowser) / reacting on Clicks

Postby plusminus » Thu Nov 15, 2007 11:11 pm

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

:?: Problems/Questions: Write it right below...

Difficulty: 1 of 5 :)

What it will look like:
Image
Image
Image


Description:
What we will do is:
  1. Create a ListView containing some Search-Keywords.
  2. React on the listItem-Clicks
  3. Open a webpage (anddev.org) and search for the keyword selected in the ListView
Most interesting:
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.
Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1.     public void onCreate(Bundle icicle) {
  2.         super.onCreate(icicle);
  3.         // Create an array of Strings, that will be put to our ListActivity
  4.         String[] mStrings = new String[]{"Android", "Google", "Eclipse"};
  5.         // Create an ArrayAdapter, that will actually make the Strings above appear in the ListView
  6.         this.setListAdapter(new ArrayAdapter<String>(this,
  7.                                         android.R.layout.simple_list_item_1, mStrings));
  8.     }
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);
Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1.     @Override
  2.     protected void onListItemClick(ListView l, View v, int position, long id){
  3.         super.onListItemClick(l, v, position, id);
  4.        
  5.         // Get the item that was clicked
  6.         Object o = this.getListAdapter().getItem(position);
  7.         String keyword = o.toString();
  8.  
  9.         // Create an VIEW intent
  10.         Intent myIntent = null;
  11.                 try {
  12.                         // The intent will open our anddev.org-board and search for the keyword clicked.
  13.                         myIntent = new Intent("android.intent.action.VIEW",
  14.                                 new ContentURI("http://anddev.org/search.php?mode=results&search_keywords='"
  15.                                                 + keyword + "'"));
  16.                 } catch (URISyntaxException e) {
  17.                         e.printStackTrace();
  18.                 }
  19.                 // Start the activity
  20.         startActivity(myIntent);
  21.     }
  22. }
Parsed in 0.033 seconds, using GeSHi 1.0.8.4


The Full SourceCode
Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1. package org.anddev.android.listviews;
  2.  
  3. import java.net.URISyntaxException;
  4.  
  5. import android.app.ListActivity;
  6. import android.content.Intent;
  7. import android.net.ContentURI;
  8. import android.os.Bundle;
  9. import android.view.View;
  10. import android.widget.ArrayAdapter;
  11. import android.widget.ListView;
  12.  
  13. public class ListViewDemo extends ListActivity {
  14.     /** Called when the activity is first created. */
  15.     @Override
  16.     public void onCreate(Bundle icicle) {
  17.         super.onCreate(icicle);
  18.         // Create an array of Strings, that will be put to our ListActivity
  19.         String[] mStrings = new String[]{"Android", "Google", "Eclipse"};
  20.         // Create an ArrayAdapter, that will actually make the Strings above appear in the ListView
  21.         this.setListAdapter(new ArrayAdapter<String>(this,
  22.                                         android.R.layout.simple_list_item_1, mStrings));
  23.     }
  24.    
  25.     @Override
  26.     protected void onListItemClick(ListView l, View v, int position, long id){
  27.         super.onListItemClick(l, v, position, id);
  28.        
  29.         // Get the item that was clicked
  30.         Object o = this.getListAdapter().getItem(position);
  31.         String keyword = o.toString();
  32.  
  33.         // Create an VIEW intent
  34.         Intent myIntent = null;
  35.                 try {
  36.                         // The intent will open our anddev.org-board and search for the keyword clicked.
  37.                         myIntent = new Intent("android.intent.action.VIEW",
  38.                                 new ContentURI("http://anddev.org/search.php?mode=results&search_keywords='"
  39.                                                 + keyword + "'"));
  40.                 } catch (URISyntaxException e) {
  41.                         e.printStackTrace();
  42.                 }
  43.                 // Start the activity
  44.         startActivity(myIntent);
  45.     }
  46. }
Parsed in 0.040 seconds, using GeSHi 1.0.8.4


Regards,
plusminus
Last edited by plusminus on Tue Nov 20, 2007 5:04 pm, edited 11 times in total.
Image
Image | Android Development Community / Tutorials
User avatar
plusminus
Site Admin
Site Admin
 
Posts: 2688
Joined: Wed Nov 14, 2007 8:37 pm
Location: Schriesheim, Germany

Top

Postby Kenney » Fri Nov 16, 2007 2:27 am

Maybe you would like to analyze the code so these tutorials are actually tutorials instead of codes.
Kenney
Once Poster
Once Poster
 
Posts: 1
Joined: Fri Nov 16, 2007 2:24 am

Postby plusminus » Fri Nov 16, 2007 9:57 am

Thanks for the info.
I'm sorry, really needed to sleep yesterday night... :roll:

I've just improved it, let it is more a tutorial to you now.

Regards,
plusminus
Image
Image | Android Development Community / Tutorials
User avatar
plusminus
Site Admin
Site Admin
 
Posts: 2688
Joined: Wed Nov 14, 2007 8:37 pm
Location: Schriesheim, Germany

Postby tcsavage » Fri Nov 16, 2007 1:59 pm

This is realy usefull. Just checking but is there anything happening in the main.xml file layout wise?
tcsavage
Freshman
Freshman
 
Posts: 3
Joined: Fri Nov 16, 2007 1:58 pm

Postby plusminus » Fri Nov 16, 2007 2:18 pm

Nothing has to be changed in the Layouts, as we are not using them :)

We pass an new Adapter, that does not relate to our xml-Layout in any way, to the setListAdapter(...)-function.

Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1.     public void onCreate(Bundle icicle) {
  2.  
  3.         super.onCreate(icicle);
  4.  
  5.         // Create an array of Strings, that will be put to our ListActivity
  6.  
  7.         String[] mStrings = new String[]{"Android", "Google", "Eclipse"};
  8.  
  9.         // Create an ArrayAdapter, that will actually make the Strings above appear in the ListView
  10.  
  11.         this.setListAdapter(new ArrayAdapter<String>(this,
  12.  
  13.                                         android.R.layout.simple_list_item_1, mStrings));
  14.  
  15.     }
Parsed in 0.036 seconds, using GeSHi 1.0.8.4


Regards,
plusminus
Image
Image | Android Development Community / Tutorials
User avatar
plusminus
Site Admin
Site Admin
 
Posts: 2688
Joined: Wed Nov 14, 2007 8:37 pm
Location: Schriesheim, Germany

Postby ramvi » Sat Nov 17, 2007 1:00 pm

Thanks!

Adding: "android:theme="@android:style/Theme.Dialog"" to AndroidManifest.xml makes the list appear in a nifty little dialog.

It should be added like this:
<activity class=".CLASSNAME" android:label="@string/app_name" android:theme="@android:style/Theme.Dialog">
ramvi
Freshman
Freshman
 
Posts: 2
Joined: Sat Nov 17, 2007 12:56 pm

Top

Postby atr » Thu Nov 22, 2007 7:25 am

Hi All,

Why I keep getting this error message, once I clicked on like "Google"?
Network Error
Error 32
anddev.org - anddev.org


Can somebody help me? Thanks.
atr
Junior Developer
Junior Developer
 
Posts: 14
Joined: Mon Nov 19, 2007 11:36 am
Location: Singapore

Postby plusminus » Thu Nov 22, 2007 1:07 pm

Hello atr,

as the tutorials (or the following lines) are sending out an Intent that will Open the Browser to view the search-results on a keyword of <THIS BOARD>, the Emulator needs a connection to the Internet :!:
Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1.      myIntent = new Intent("android.intent.action.VIEW",
  2.  
  3.           new ContentURI("http://anddev.org/search.php?mode=results&search_keywords='"
  4.  
  5.                               + keyword + "'"));
  6.  
  7.      startActivity(myIntent);
Parsed in 0.036 seconds, using GeSHi 1.0.8.4



What happens if you open the Browser on your Home-Screen :?:
:!: If the same (or similar) happens their is probably a FireWall (or sth else) blocking In/Out-Internet-Connections.

Regards,
plusminus
Image
Image | Android Development Community / Tutorials
User avatar
plusminus
Site Admin
Site Admin
 
Posts: 2688
Joined: Wed Nov 14, 2007 8:37 pm
Location: Schriesheim, Germany

Postby atr » Fri Nov 23, 2007 12:12 pm

plusminus wrote:Hello atr,

as the tutorials (or the following lines) are sending out an Intent that will Open the Browser to view the search-results on a keyword of <THIS BOARD>, the Emulator needs a connection to the Internet :!:
Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1.      myIntent = new Intent("android.intent.action.VIEW",
  2.           new ContentURI("http://anddev.org/search.php?mode=results&search_keywords='"
  3.                               + keyword + "'"));
  4.      startActivity(myIntent);
Parsed in 0.037 seconds, using GeSHi 1.0.8.4



What happens if you open the Browser on your Home-Screen :?:
:!: If the same (or similar) happens their is probably a FireWall (or sth else) blocking In/Out-Internet-Connections.

Regards,
plusminus


Hi,

the weirdest thing is, I can go google.com with the default browser of emulator, but not with this. I don't think Firewall is the reason.
atr
Junior Developer
Junior Developer
 
Posts: 14
Joined: Mon Nov 19, 2007 11:36 am
Location: Singapore

Postby plusminus » Fri Nov 23, 2007 2:45 pm

Hey atr,

you are behind a router or sth.?
If yes, try plugging your inet-connection directly to your pc.

Apparently the Error 32 you are likely getting could be due to the fact that you have two network connections active at the same time (Wifi & Ethernet Cable).
If that is you case, then try to disable one and check if it works better.

Seen some posts in google-groups where that helped.

Regards,
plusminus
Image
Image | Android Development Community / Tutorials
User avatar
plusminus
Site Admin
Site Admin
 
Posts: 2688
Joined: Wed Nov 14, 2007 8:37 pm
Location: Schriesheim, Germany

Postby nithin.warier » Wed Mar 05, 2008 1:34 pm

in the program

import android.net.ContentURI;

is not working for me, can anyone tell the error..

Thanks
Nithin
nithin.warier
Experienced Developer
Experienced Developer
 
Posts: 87
Joined: Thu Feb 28, 2008 12:05 pm
Location: Malappuram Kerala India

Postby plusminus » Wed Mar 05, 2008 1:41 pm

Hello nithin.warier,

since m5 it is [font=Lucida Console]android.net.Uri[/font].

Regards,
plusminus
Image
Image | Android Development Community / Tutorials
User avatar
plusminus
Site Admin
Site Admin
 
Posts: 2688
Joined: Wed Nov 14, 2007 8:37 pm
Location: Schriesheim, Germany

Postby nithin.warier » Wed Mar 05, 2008 2:12 pm

thanks plusminus....
nithin.warier
Experienced Developer
Experienced Developer
 
Posts: 87
Joined: Thu Feb 28, 2008 12:05 pm
Location: Malappuram Kerala India

Cannot instantiate the type Uri

Postby ekambresh » Thu Mar 13, 2008 12:09 pm

hi plusminus ,
tnx for the tutorial,bt i am getting error"Cannot instantiate the type Uri",can u please tell me wt is the reason for that....


regards
ekambresh
ekambresh
ekambresh
Junior Developer
Junior Developer
 
Posts: 22
Joined: Fri Feb 08, 2008 8:54 am
Location: Bangalore

solved

Postby ekambresh » Thu Mar 13, 2008 2:58 pm

hmm i used uri.parse instead of uri,now its working
ekambresh
ekambresh
Junior Developer
Junior Developer
 
Posts: 22
Joined: Fri Feb 08, 2008 8:54 am
Location: Bangalore

Top
Next

Return to Novice Tutorials

Who is online

Users browsing this forum: No registered users and 6 guests