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!
Enjoy!






import Android.R;com.your.package.name.R;


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!
}
}<?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>
import com.your.package.name.R;




Return to Code Snippets for Android
Users browsing this forum: Google [Bot] and 7 guests