Paldan
Paldan

Reputation: 435

Android WebView not working

I am quite new to Android and I am trying to utilize WebView. When I load a website,

it uses the phones browser rather than displaying the website from within the app. Any suggestions?

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

        String url = "http://www.kylesutherland.com";

        WebView view = (WebView) findViewById(R.id.webview);
        view.getSettings().setJavaScriptEnabled(true);
        view.loadUrl(url);
    }

Upvotes: 0

Views: 195

Answers (4)

Born To Win
Born To Win

Reputation: 3329

Try this..

public class MainActivity extends Activity {

WebView web;

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

        web = (WebView) findViewById(R.id.webView1);
        web.setWebViewClient(new myWebClient());
        web.loadUrl("http://uniqueandroidtutorials.blogspot.in");
    }

    public class myWebClient extends WebViewClient {
        @Override
        public void onPageStarted(WebView view, String url, Bitmap favicon) {
            // TODO Auto-generated method stub
            super.onPageStarted(view, url, favicon);
        }

        @Override
        public boolean shouldOverrideUrlLoading(WebView view, String url) {
            // TODO Auto-generated method stub

            view.loadUrl(url);
            return true;

        }
    }
}

activity_main.xml

<RelativeLayout 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"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context=".MainActivity" >

    <WebView
        android:id="@+id/webView1"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_alignParentTop="true"
        android:layout_centerHorizontal="true" />

</RelativeLayout>

Hope this helps you..

Upvotes: 0

Siddharth_Vyas
Siddharth_Vyas

Reputation: 10100

Try this :

mWebView.setWebViewClient(new CustomWebViewClient());

mWebView.setWebChromeClient(new WebChromeClient());

    private class CustomWebViewClient extends WebViewClient {

    @Override
    public void onPageStarted(WebView view, String url, Bitmap favicon) {
        super.onPageStarted(view, url, favicon);

    }

    @Override
    public void onLoadResource(WebView view, String url) {
        super.onLoadResource(view, url);

    }

    @Override
    public boolean shouldOverrideUrlLoading(WebView view, String url) {
         view.loadUrl(url);

        return true;

    }

    @Override
    public void onPageFinished(WebView view, String url) {

    }
}

Hope this helps.

Upvotes: 1

Steve M
Steve M

Reputation: 9794

Try this before loadUrl():

webView.setWebViewClient(new WebViewClient()
{
        @Override
        public boolean shouldOverrideUrlLoading(WebView view, String url)
        {
            return false;
        }
});

Upvotes: 1

Coderji
Coderji

Reputation: 7765

you need to add WebViewClient in order to get the links within the page"

browser.setWebViewClient(new WebViewClient() {
        public boolean shouldOverrideUrlLoading(WebView view, String url) {
            // TODO Auto-generated method stub
            view.loadUrl(url);
            return true;
        }
    });

in your case it should look like this:

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

    String url = "http://www.kylesutherland.com";

    WebView view = (WebView) findViewById(R.id.webview);
    view.getSettings().setJavaScriptEnabled(true);
    view.loadUrl(url);
    //starting from here
    view.setWebViewClient(new WebViewClient() {
        public boolean shouldOverrideUrlLoading(WebView view, String url) {
            // TODO Auto-generated method stub
            view.loadUrl(url);
            return true;
}

hope this is what you are looking for. Please give me a feedback

Upvotes: 1

Related Questions