I did try samples, demos from Google codes and other resources with WebView, but when i try to do it in my own code, it doesn't work for me.
I want to load index.html which i put in assets folder, and using:
public class MainActivity extends Activity {
private WebView webView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//define webview
webView = (WebView)findViewById(R.id.webView);
webView.setHorizontalScrollBarEnabled(false);
webView.loadUrl("file:///android_asset/www/index.html");
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
}
On emulator i cannot see my file its still white background and not open my index.html.
Note:-
my index.html is include JavaScript than get data from web site online by ajax.
Is there any way of loading an existing .html file in the application package which works on all API versions ?
Thank you in advance for your help!
Edit :-
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
webView = (WebView) findViewById(R.id.webView);
webView.getSettings().setJavaScriptEnabled(true);
webView.setWebChromeClient(new WebChromeClient() {
public void onProgressChanged(WebView view, int progress) {
MainActivity.this.setProgress(progress * 1000);
}
});
webView.setWebViewClient(new WebViewClient() {
public void onReceivedError(WebView view, int errorCode, String description, String failingUrl) {
Toast.makeText(MainActivity.this, "Oh no! " + description, Toast.LENGTH_SHORT).show();
}
});
webView.loadUrl("file:///android_asset/www/index.html");
}
i will try this code but i have error :-
Multiple markers at this line
- WebViewClient cannot be resolved to a type
- The method setWebViewClient(WebViewClient) in the type WebView is not applicable for the arguments (new
WebViewClient(){})