3

I need to open a webpage directly if i run the app. Without using a single a component in it.

4
  • You don't want to use WebView ? Commented Dec 5, 2012 at 14:50
  • It shows web page not available in emulater.
    – AndRaGhu
    Commented Dec 5, 2012 at 15:11
  • Did you add <uses-permission android:name="android.permission.INTERNET" /> to your Manifest file Commented Dec 5, 2012 at 15:19
  • I added another answer that uses a WebView Commented Dec 5, 2012 at 15:27

5 Answers 5

4

Here is onCreate method of an Activity that opens google.com directly:

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_browser);

    WebView wb = (WebView) findViewById(R.id.webView1);

    wb.loadUrl("http://www.google.com.tr");
}

and here is the activity_browser.xml layout file:

<WebView xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".BrowserActivity"
    android:id="@+id/webView1" > 
</WebView>

Don't forget to add the internet permission to AndroidManifest.xml file:

<uses-permission android:name="android.permission.INTERNET" />
0

Create and start the intent(with uri.parse ofcourse) at your onCreate mehod.

0

I do not quite understand, but I think that's what you need ...

Four different ways of opening a web page in Android

1
  • Put a link in the response is not the best way to answer. As you can see the link was expired.
    – Yazon2006
    Commented Jul 8, 2021 at 14:55
0

As Ercan said, this is how it is done:

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

Do it on onCreate method of your Activity.

0

Add Code in onCreate()

    webView.setWebViewClient(new WebViewClient());    //the lines of code added
    webView.setWebChromeClient(new WebChromeClient()); //same as above
    webView.getSettings().setJavaScriptEnabled(true);
  webView.canGoBack();

    webView.loadUrl("your url");


    final ProgressDialog progressBar = new ProgressDialog(getActivity());
    progressBar.setMessage("Please wait...");


    webView.setWebViewClient(new WebViewClient() {
        public boolean shouldOverrideUrlLoading(WebView view, String url) {
            view.loadUrl(url);
            return true;
        }

        @Override
        public void onPageStarted(WebView view, String url, Bitmap favicon) {
            super.onPageStarted(view, url, favicon);
            if (!progressBar.isShowing()) {
                progressBar.show();
            }
        }

        public void onPageFinished(WebView view, String url) {
            if (progressBar.isShowing()) {
                progressBar.dismiss();
            }
        }

        public void onReceivedError(WebView view, int errorCode, String description, String failingUrl) {
            if (progressBar.isShowing()) {
                progressBar.dismiss();
            }
        }
    });


    //To handle Webpage back in fragment
    webView.setOnKeyListener(new View.OnKeyListener() {

        public boolean onKey(View v, int keyCode, KeyEvent event) {
            if (keyCode == KeyEvent.KEYCODE_BACK
                    && event.getAction() == MotionEvent.ACTION_UP
                    && webView.canGoBack()) {
                webView.goBack();
                return true;
            }
            return false;
        }
    });

in xml Add webview:

<WebView
    android:id="@+id/webView"
    android:layout_width="match_parent"
    android:layout_height="wrap_content" />

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.