[TUT] Button to open web browser

Quickly share your Android Code Snippets here...

[TUT] Button to open web browser

Postby blundell » Sun Oct 31, 2010 6:14 pm

Someone requested this so here goes!

If you want to send someone to a website when they press a button in your activity, see my blog post:

http://blog.blundell-apps.com/button-to-open-web-browser/

:-) Enjoy!
User avatar
blundell
Master Developer
Master Developer
 
Posts: 1610
Joined: Tue Nov 18, 2008 12:58 pm
Location: UK

Top

Re: [TUT] Button to open web browser

Postby leroytrolley » Fri Nov 05, 2010 11:11 pm

Many thanks for this....I will hopefully be able to get to play with it over the next few days. So many ideas, just not enough hours in the day!
leroytrolley
Junior Developer
Junior Developer
 
Posts: 12
Joined: Wed Oct 06, 2010 8:10 pm
Location: Kent, UK

Re: [TUT] Button to open web browser

Postby deafmetal » Thu Nov 11, 2010 10:29 pm

Blundell,

I've tried this, but when I click/press the button, nothing happens. Also, had to rem (//) out the @overrides, otherwise I kept getting Java errors. Only changes I made was renaming the button name and void call. Everything lines up, debugs fine, just won't open browser and run the URL.

Using Win 7 Ultimate (64bit), Eclipse Helios, Java SE JDK 6 Update 22 (64bit), Android SDK for 2.1.

Debugging using AVD 2.1 update 1 and also an HTC Eris.

Thoughts? Thanks!

Rob
deafmetal
Freshman
Freshman
 
Posts: 2
Joined: Thu Nov 11, 2010 10:17 pm

Re: [TUT] Button to open web browser

Postby blundell » Thu Nov 11, 2010 10:45 pm

Hmm I'm not to good at debugging environment problems over the internet sorry :-(
but my thought's are, you need the @override annotation so this is an error with your java setup you need to look into this,
I've heard of error's with 64bit eclipse and the android install is it possible for you to get 32bit?
i've also heard of people complaining about Helios, you could install Galileo (the version I use)

These are both error's I've heard of / read about in the past and may be fixed by now but it's my two cent!

The first thing you could try is:
Go to your project/ide preferences and set the java compiler level to 1.6 and also make sure you select JRE 1.6 to execute your program from eclipse. Then replace the @Override annotation.
User avatar
blundell
Master Developer
Master Developer
 
Posts: 1610
Joined: Tue Nov 18, 2008 12:58 pm
Location: UK

Re: [TUT] Button to open web browser

Postby deafmetal » Sat Nov 13, 2010 8:36 am

Thx, I'll try your last recommendation for the compiler level and reply how it goes.
deafmetal
Freshman
Freshman
 
Posts: 2
Joined: Thu Nov 11, 2010 10:17 pm

Re: [TUT] Button to open web browser

Postby Sorid » Fri Nov 19, 2010 5:46 pm

I copy the codes into my project and fix it so it looks good but I getting this error: http://img831.imageshack.us/img831/6341/96782657246.png
How do I fix it?
Sorid
Freshman
Freshman
 
Posts: 6
Joined: Fri Nov 19, 2010 4:13 pm

Top

Re: [TUT] Button to open web browser

Postby blundell » Fri Nov 19, 2010 6:19 pm

Check your import statements, remove an import of:
Code: Select all
import Android.R;

and replace with
Code: Select all
com.your.package.name.R;
User avatar
blundell
Master Developer
Master Developer
 
Posts: 1610
Joined: Tue Nov 18, 2008 12:58 pm
Location: UK

Re: [TUT] Button to open web browser

Postby Sorid » Fri Nov 19, 2010 6:56 pm

I don't have a import Android.R; in my import statments.
Sorid
Freshman
Freshman
 
Posts: 6
Joined: Fri Nov 19, 2010 4:13 pm

Re: [TUT] Button to open web browser

Postby blundell » Fri Nov 19, 2010 8:19 pm

Do you have an error in your xml file that is stopping your R file being built? Paste your code in [ code ] tags [ /code ] it's better than a screen shot
User avatar
blundell
Master Developer
Master Developer
 
Posts: 1610
Joined: Tue Nov 18, 2008 12:58 pm
Location: UK

Re: [TUT] Button to open web browser

Postby Sorid » Fri Nov 19, 2010 8:33 pm

Here is the code of the java file:
Code: Select all
package com.*MYNAME*.android;

import android.app.Activity;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;

public class urlsender extends Activity {
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main); //Set the screen's view to your xml file
        Button twitterButton = (Button) findViewById(R.id.twitterButton); // Retrieve the button from the XML file
        twitterButton.setOnClickListener(new View.OnClickListener() {  //Add a listener for when the button is pressed
                        @Override
                        public void onClick(View v) {
                                sendToTwitter();                       
                        }
                });
    }

        protected void sendToTwitter() {
                String url = "http://twitter.com/blundell_apps"; // You could have this at the top of the class as a constant, or pass it in as a method variable, if you wish to send to multiple websites
                Intent i = new Intent(Intent.ACTION_VIEW); // Create a new intent - stating you want to 'view something'
                i.setData(Uri.parse(url));  // Add the url data (allowing android to realise you want to open the browser)
                startActivity(i); // Go go go!
        }
}


And here is the code of the main.xml in res/layout:
Code: Select all
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
   android:orientation="vertical"
   android:layout_width="fill_parent"
   android:layout_height="fill_parent" />
    <Button
        android:id="@+id/twitterButton"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:text="Send me to Twitter" />
</LinearLayout>
Sorid
Freshman
Freshman
 
Posts: 6
Joined: Fri Nov 19, 2010 4:13 pm

Re: [TUT] Button to open web browser

Postby blundell » Fri Nov 19, 2010 8:35 pm

Try this import,
Code: Select all
import com.your.package.name.R;


Apart from that in eclipse goto on the taskbar: Project > Clean and press Ok.
User avatar
blundell
Master Developer
Master Developer
 
Posts: 1610
Joined: Tue Nov 18, 2008 12:58 pm
Location: UK

Re: [TUT] Button to open web browser

Postby Sorid » Fri Nov 19, 2010 9:07 pm

Even if I put that in the java file the error still exists.
import com.*MYNAME*.android.R; is what I typed in.

*MYNAME* = my real name
Sorid
Freshman
Freshman
 
Posts: 6
Joined: Fri Nov 19, 2010 4:13 pm

Re: [TUT] Button to open web browser

Postby blundell » Fri Nov 19, 2010 9:09 pm

I'd recommend creating a new project and putting the code in again. You must have something wrong with your setup.
User avatar
blundell
Master Developer
Master Developer
 
Posts: 1610
Joined: Tue Nov 18, 2008 12:58 pm
Location: UK

Re: [TUT] Button to open web browser

Postby Sorid » Fri Nov 19, 2010 10:46 pm

It works now! Thanks for the help! :)
Sorid
Freshman
Freshman
 
Posts: 6
Joined: Fri Nov 19, 2010 4:13 pm

Top

Return to Code Snippets for Android

Who is online

Users browsing this forum: No registered users and 6 guests