Android tutorial: launch website (Http) from Android application

In this small tutorial I show you how you can launch a website from an Android application. We use a button to initiate the launch of a website and an Intent with Uri parse.

package com.example.launchwebsite;

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

public class MainActivity extends Activity {

String url1 = "http://m.nu.nl";

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        
        Button btnlaunchwebsite = (Button)findViewById(R.id.button1);
        
        btnlaunchwebsite.setOnClickListener(new View.OnClickListener() {
			
			@Override
			public void onClick(View v) {
				Intent i = new Intent(Intent.ACTION_VIEW);
                i.setData(Uri.parse(url1));
                startActivity(i);
			}
		});
        
    }

    
}

- Create a variable for the website you want to launch (in my case String url1 = “http://m.nu.nl” ).
- Create your button reference via findViewById.
- Set a OnClickListener to your Button.
- Create an Intent with ACTION_VIEW.
- Parse the Url1 variable with Uri.Parse.
- Finally, don’t forget to call startActivity.

Leave a Reply

Your email address will not be published.

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>

*