Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Open browser to web page Android App

Tags:

java

android

Trying to grasp Java and Android would like help with a simple task of opening a users browser after they click a button.

I have been doing tutorials for the last two days though it might help if I just took a stab at it and got feedback. thanks in advance for any help.

main.xml:

<AbsoluteLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:background="@drawable/bgimage2"> 
    >

<Button
android:id="@+id/goButton"
android:layout_width="150px"
android:layout_height="wrap_content"
android:text="@string/start"
android:layout_x="80px"
android:layout_y="21px"
>
</AbsoluteLayout>

GetURL.java:

package com.patriotsar;

import android.app.Activity;
import android.content.Intent;
import android.view.View.OnClickListener;

String url = "http://www.yahoo.com";
Intent i = new Intent(Intent.ACTION_VIEW);
Uri u = Uri.parse(url);
i.setData(u);


public class patriosar extends Activity {

     private Button goButton;

    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);


        goButton.setOnClickListener(new OnClickListener() {         
            @Override
            public void onClick(View v){
                try {
                      // Start the activity
                      startActivity(i);
                    } catch (ActivityNotFoundException e) {
                      // Raise on activity not found
                      Toast toast = Toast.makeText(context, "Browser not found.", Toast.LENGTH_SHORT);
                    }
                  } 
        });

    }
}
like image 462
Denoteone Avatar asked May 29 '11 07:05

Denoteone


People also ask

How do I open a link in an app instead of Chrome?

Every android app will have list of urls that it can open. So you have to go to that app settings and tell that it should open in browser for the urls and not in the app. To do that go to Settings -> Apps -> scroll down to the app that you don't want URLs to open in -> Tap on 'Open by Default' and select always Ask.


2 Answers

It's close, but a few things are in the wrong place or missing. The below code works -- I tried to make the minimum necessary alterations. You could load both versions into something like WinMerge to see exactly what changed.

main.xml:

<?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"
    android:background="@drawable/bgimage2"
    >

    <Button
        android:id="@+id/goButton"
        android:layout_width="150px"
        android:layout_height="wrap_content"
        android:text="@string/start"
        android:layout_x="80px"
        android:layout_y="21px"
    ></Button>
</LinearLayout>

GetURL.java:

import android.app.Activity;
import android.content.ActivityNotFoundException;
import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.Toast;

public class GetURL extends Activity {
    private Button goButton;
    String url = "http://www.yahoo.com";
    Intent i = new Intent(Intent.ACTION_VIEW);
    Uri u = Uri.parse(url);
    Context context = this;

    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        goButton = (Button)findViewById(R.id.goButton);
        goButton.setOnClickListener(new OnClickListener() {         
            @Override
            public void onClick(View v){
                try {
                      // Start the activity
                        i.setData(u);
                      startActivity(i);
                    } catch (ActivityNotFoundException e) {
                      // Raise on activity not found
                      Toast.makeText(context, "Browser not found.", Toast.LENGTH_SHORT);
                    }
                  } 
        });
    }
}

(You also need a bgimage2.png file in /res/drawable/ and a start string in /res/values/strings.xml, of course).

like image 51
Sven Viking Avatar answered Oct 26 '22 23:10

Sven Viking


To simplify you could do

Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse("http://www.yahoo.com")); startActivity(intent);

like image 37
Dave S Avatar answered Oct 27 '22 00:10

Dave S