Reputation: 137
i am trying to load a web page in webview
but it is not loading web page in webview
instead it is asking another browser app to load web page
i want to load webpage in my app webview
this is my code: it ask another app to load page
package com.example.webview;
import android.os.Bundle;
import android.support.v7.app.ActionBarActivity;
import android.webkit.WebView;
public class MainActivity extends ActionBarActivity {
private WebView browser;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
browser = (WebView) findViewById(R.id.webView1);
browser.loadUrl("http://www.google.com");
}
}
Upvotes: 0
Views: 4956
Reputation: 66
You are missing the following line. Add it after calling the id of the webview, then it will work :
browser.setWebViewClient(new WebViewClient() {
public void onReceivedError(WebView view, int errorCode, String description, String failingUrl) {
.....
}
Upvotes: 1
Reputation: 164
You can try this, Hope it will help you.
import android.app.Activity;
import android.os.Bundle;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.Toast;
public class Main extends Activity {
private WebView mWebview ;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
mWebview = new WebView(this);
mWebview.getSettings().setJavaScriptEnabled(true); // enable javascript
final Activity activity = this;
mWebview.setWebViewClient(new WebViewClient() {
public void onReceivedError(WebView view, int errorCode, String description, String failingUrl) {
Toast.makeText(activity, description, Toast.LENGTH_SHORT).show();
}
});
mWebview .loadUrl("http://www.google.com");
setContentView(mWebview );
}
}
Upvotes: 0
Reputation: 482
You have not set webviewclient so please set it using below code.
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
browser = (WebView) findViewById(R.id.webView1);
browser .setWebViewClient(new WebViewClient() {
public void onReceivedError(WebView view, int errorCode, String description, String failingUrl) {
}
});
browser.loadUrl("http://www.google.com");
}
Upvotes: 1