3

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

AndRaGhu
  • 161
  • 7
  • 17

5 Answers5

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.

Ercan
  • 3,705
  • 1
  • 22
  • 37
0

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

Four different ways of opening a web page in Android

Ascension
  • 2,599
  • 2
  • 15
  • 13
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" />
Pratibha Sarode
  • 1,819
  • 17
  • 17