Go Back   Android Forums > Android Development > Application Development
Application Development Dev lounge for our application developers.

Get excited for the Samsung Galaxy S5! Find everything you need and discuss it in our Galaxy S5 Forum!

test: Reply
 
LinkBack Thread Tools
Old October 24th, 2012, 10:44 AM   #1 (permalink)
New Member
Thread Author (OP)
 
Join Date: Oct 2012
Posts: 3
 
Device(s):
Carrier: Not Provided

Thanks: 0
Thanked 0 Times in 0 Posts
Default Simply Clickable Button to URL

I'm new to android. I have an app. I just want to add to it a button which when clicked opens a browser to "google.com" website. This is the button code:

<Button
android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:text="Website" />

I don't know what to put on the activity code. Any help would be appreciated.

skylab8 is offline  
Reply With Quote
sponsored links
Old October 24th, 2012, 11:42 AM   #2 (permalink)
Senior Member
 
jonbonazza's Avatar
 
Join Date: Jul 2010
Gender: Male
Posts: 1,936
 
Device(s): Nexus 4, Samsung Galaxy S II, HTC Evo 4G, Amazon Kindle Fire
Carrier: ATT

Thanks: 235
Thanked 478 Times in 292 Posts
Default

Moved to App Development.

You need to register an OnClickListener and override the onClick() method to load the web page. To do this, you could either use a WebView or send an Intent to open the user's preferred browser.
__________________
My blog, Inverted Keystrokes, is looking for aspiring developers (not necessarily with Android) to post articles. If you have any development experience and are interested in participating, please PM me. =)
jonbonazza is offline  
Reply With Quote
Old October 24th, 2012, 05:25 PM   #3 (permalink)
ROM Developer
 
cr5315's Avatar
 
Join Date: Jul 2010
Location: Narnia
Posts: 435
 
Device(s): Current: HTC Evo 4G, Asus Nexus 7 Retired: LG Ally
Carrier: Not Provided

Thanks: 103
Thanked 161 Times in 105 Posts
Default

Take the code for your button and add
Code:
android:onClick="openWebsite"
to make it look like
Code:
<Button
android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:text="Website"
android:onClick="openWebsite" />
Then in your activity...

Code:
public void openWebsite(View view) {
    String url = "http://www.example.com";
    Intent i = new Intent(Intent.ACTION_VIEW);
    i.setData(Uri.parse(url));
    startActivity(i);
}
The androidnClick calls openWebsite() which will then pass String url to Android which will open it in the default browser.
__________________

(That's a link. Just saying.)
cr5315 is offline  
Last edited by cr5315; October 27th, 2012 at 07:28 PM. Reason: Fixed the Java portion of the button
Reply With Quote
Old October 24th, 2012, 08:15 PM   #4 (permalink)
New Member
Thread Author (OP)
 
Join Date: Oct 2012
Posts: 3
 
Device(s):
Carrier: Not Provided

Thanks: 0
Thanked 0 Times in 0 Posts
Default

Thanks a lot for the code. When I pasted your code to the activity I got these errors with "Intent" highlighted in red.
Intent cannot be resolved to a variable.
Intent cannot be resolved to a type
Intent cannot be resolved to a type

Google suggested to "import "intent" (andoird.conent) as a quick fix.
When I choose the quick fix Android suggested and run the app. The button was visible, and clickable, but when I click on it the app freezes.
skylab8 is offline  
Reply With Quote
Old October 25th, 2012, 08:38 AM   #5 (permalink)
Senior Member
 
jonbonazza's Avatar
 
Join Date: Jul 2010
Gender: Male
Posts: 1,936
 
Device(s): Nexus 4, Samsung Galaxy S II, HTC Evo 4G, Amazon Kindle Fire
Carrier: ATT

Thanks: 235
Thanked 478 Times in 292 Posts
Default

When you say freezes, does it show an exception eventually?
jonbonazza is offline  
Reply With Quote
Old October 25th, 2012, 11:44 AM   #6 (permalink)
New Member
Thread Author (OP)
 
Join Date: Oct 2012
Posts: 3
 
Device(s):
Carrier: Not Provided

Thanks: 0
Thanked 0 Times in 0 Posts
Default

Yes right after I click on the link I get an exception. Thanks!
skylab8 is offline  
Reply With Quote
Reply


Go Back   Android Forums > Android Development > Application Development
Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On



All times are GMT -5. The time now is 04:22 PM.
Powered by vBulletin® Version 3.8.7
Copyright ©2000 - 2014, vBulletin Solutions, Inc.