Easiest way to have button open browser to specific URL

12,749

Solution 1

This line should open your built-in browser, with the specified url:

    startActivity(new Intent(Intent.ACTION_VIEW).setData(Uri.parse("http://www.google.com")));

Your Activity should have parts like this:

//define class variables here
Button btn;

protected void onCreate(Bundle savedInstanceState)
{
    //some code of yours
    btn=(Button)findViewById(R.id.button1);
    btn.setOnClickListener(this);
    //more code of yours
}

//whatever else you have in your source code

public void onClick(View v)
{
    //handle the click events here, in this case open www.google.com with the default browser
    startActivity(new Intent(Intent.ACTION_VIEW).setData(Uri.parse("http://www.google.com")));
}

It might not be 100% accurate syntax, since I did just write this on my own, but you get the idea.

Solution 2

You can do that with Rebol 3, this easily:

REBOL [] 
load-gui 
view [button "Go" on-action [browse http://msn.com]]

That's a fully functioning GUI program, which runs on Android AND on desktop, using the exact same code across all platforms. Take a look at:

http://rebolforum.com/index.cgi?f=printtopic&permalink=Nick25-Aug-2013/10:08:38-7:00&archiveflag=new

Share:
12,749
user1301764
Author by

user1301764

Updated on June 04, 2022

Comments

  • user1301764
    user1301764 almost 2 years

    It's a simple app I've got and I'd like the button I've made to launch a specific URL via the browser. Could you guys give me a little info to get this going, like I've said I've got the button already to go in my app. Here's the code -- lemme' know if you need anything else

    .java File

    package reseeveBeta.mpi.dcasey;
    
    import android.app.Activity;
    import android.os.Bundle;
    
    public class ReseeveBetaActivity extends Activity {
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);                
        }  
    
    }
    

    .XML

    <?xml version="1.0" encoding="utf-8"?>
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical" >
    
    <TextView
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="Welcome to Reseeve, tap register to begin account creation" />
    
    <Button
        android:id="@+id/button1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="Register" />
    
    <EditText
        android:id="@+id/editText1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:ems="10"
        android:inputType="textMultiLine"
        android:text="If you already have and account, please login below" >
    
        <requestFocus />
    </EditText>
    
    </LinearLayout>