open a url on click of ok button in android

156,326

Solution 1

On Button click event write this:

Uri uri = Uri.parse("http://www.google.com"); // missing 'http://' will cause crashed
Intent intent = new Intent(Intent.ACTION_VIEW, uri);
startActivity(intent);

that open the your URL.

Solution 2

    Button imageLogo = (Button)findViewById(R.id.iv_logo);
    imageLogo.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {
            // TODO Auto-generated method stub
            String url = "http://www.gobloggerslive.com";

            Intent i = new Intent(Intent.ACTION_VIEW);
            i.setData(Uri.parse(url));
            startActivity(i);
        }
    });

Solution 3

You can use the below method, which will take your target URL as the only input (Don't forget http://)

void GoToURL(String url){
    Uri uri = Uri.parse(url);
    Intent intent= new Intent(Intent.ACTION_VIEW,uri);
    startActivity(intent);
}

Solution 4

String url = "https://www.murait.com/";
if (url.startsWith("https://") || url.startsWith("http://")) {
    Uri uri = Uri.parse(url);
    Intent intent = new Intent(Intent.ACTION_VIEW, uri);
    startActivity(intent);
}else{
    Toast.makeText(mContext, "Invalid Url", Toast.LENGTH_SHORT).show();
}

You have to check that the URL is valid or not. If URL is invalid application may crash so that you have to check URL is valid or not by this method.

Solution 5

create an intent and set an action for it while passing the url to the intent

yourbtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String theurl = "http://google.com";
                Uri urlstr = Uri.parse(theurl);
                Intent urlintent = new Intent();
                urlintent.setData(urlstr);
                urlintent.setAction(Intent.ACTION_VIEW);
                startActivity(urlintent);
Share:
156,326

Related videos on Youtube

Tushar
Author by

Tushar

Updated on July 08, 2022

Comments

  • Tushar
    Tushar almost 2 years

    I have to open a URL on Click of OK Button in a view. Can someone tell how to do this?

    • Harry Joy
      Harry Joy over 13 years
    • Tushar
      Tushar over 13 years
      public void openWebURL( String inURL ) { Intent browse = new Intent( Intent.ACTION_VIEW , Uri.parse( inURL ) ); startActivity( browse ); }
    • Ganapathy C
      Ganapathy C over 13 years
      This will work perfectly dude.. so 1 up...
    • Harry Joy
      Harry Joy over 13 years
      @tushar: have you tried it? i think it should work properly. do you get any error while running this code?
    • Givantha Kalansuriya
      Givantha Kalansuriya over 12 years
  • Chris - Jr
    Chris - Jr almost 7 years
    OR startActivity(new Intent(Intent.ACTION_VIEW,Uri.parse("http://www.google.com")‌​)
  • Muhammad Noman
    Muhammad Noman about 5 years
    @Chris-Jr you have missed the last parenthesis i.e. )
  • Ian
    Ian almost 3 years
    How is this different from opening a webview item?
  • Tugay
    Tugay over 2 years
    Can you please give some explanation?