Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Easiest way to have button open browser to specific URL

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>
like image 583
user1301764 Avatar asked Dec 06 '22 15:12

user1301764


1 Answers

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.

like image 135
hundeva Avatar answered Dec 09 '22 03:12

hundeva