3

In my xml file I'm having text and below that text,I have placed a webview. I am trying to open google's home page in Webview. Instead of opening in webview,webpage is opening on browser.What I want is web page should load in webview which is below some text.Below is my code:

           <TextView
                  android:id="@+id/txt"

                  android:text="Hello Android"
                  android:textSize="30sp"
                  android:textStyle="bold"
                  android:textColor="#003399"
                  android:layout_height="wrap_content"
                  android:layout_width="wrap_content"
                  android:layout_centerHorizontal="true"
                  />

      <WebView 

                 android:id="@+id/webview"
                 android:layout_marginTop="50dp"
                 android:layout_below="@id/txt"
                 android:layout_height="fill_parent"
                 android:layout_width="fill_parent"/>

Kindly help to solve this.Thank you

Jigar Pandya
  • 2,129
  • 2
  • 17
  • 27
  • 1
    Make sure you override `shouldOverrideUrlLoading` as detailed here: http://stackoverflow.com/a/2379054/833647 – Ken Wolf Nov 25 '13 at 13:21

2 Answers2

2

This is because a combination of two things:

  1. the WebViewClient is not set (set to null), this makes the WebView try and offer every navigation as an intent to the system. Since you have a browser installed the system will try to handle that navigation there.
  2. going to google.com ususally results in a redirect, which is why the stuff about navigations in the previous point matters.

Try this:

webview = (WebView) findViewById(R.id.webview);
webview.setWebViewClient(new WebViewClient());
marcin.kosiba
  • 3,221
  • 14
  • 19
0

Create your activity and than use this code...

mWebview = (WebView) findViewById(R.id.webview); mWebview.getSettings().setJavaScriptEnabled(true); // enable javascript

      mWebview.setWebViewClient(new WebViewClient() {
          public void onReceivedError(WebView view, int errorCode,String description, String failingUrl) {
              Toast.makeText(youractivity_name.this, description, Toast.LENGTH_SHORT).show();
          }
      });

      mWebview.loadUrl("http://www.yahoo.com");
Priyanka
  • 677
  • 5
  • 20