I have a webview in my android app, the html file which gets loaded is on a server, however I need to address some files from app's assets folder. I wanted to ask how can I address those files.
Thanks
public class ViewWeb extends Activity {
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.webview);
WebView wv;
wv = (WebView) findViewById(R.id.webView1);
wv.loadUrl("file:///android_asset/aboutcertified.html"); // now it will not fail here
}
}
And in general you can use
getAssets().open("filename.txt"))
To get filea from assets
On a side note:please put some code next time that you have triedget
Related
I want to display a html file from a URL into a webview
If i run the app on a device running pre-lollipop (4.2)the html file is displayed as desired like below
But if i run the app on a device running lollipop (5.1)its showing unformatted text in webview like below
Why does this happen ?
mainactivity
public class MainActivity extends Activity
{
#Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
String webViewurl = "http://files.parsetfss.com/f1c96efa-83e3-4dc3-8e0e-7c5b6dbbd7a2/tfss-7862469c-fb9d-4e9e-b914-779fd8415e13-main.html";
WebView wv = (WebView) findViewById(R.id.mainWebView);
wv.loadUrl(webViewurl);
}
}
I have a problem. I created some app, which should open my website, but when I run the app on my phone, its ask me in which browser I want to open it(Google Chrome...). But I want that website is open in app, not in browser... I'm working with Android Studio 1.1.0... Here is my code in MainActivity.java:
public class MainActivity extends ActionBarActivity {
private WebView MyWeb;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
MyWeb = (WebView) findViewById(R.id.WebView);
MyWeb.getSettings().setJavaScriptEnabled(true);
MyWeb.getSettings().setSaveFormData(true);
MyWeb.loadUrl("http://www.mywebsite.com");
} }
P.S. I changed the url to the fake one...
Add this line of code before your MyWeb.loadUrl
MyWeb.setWebViewClient(new WebViewClient());
This worked for me.!
I am integrating html in android.I have created a web view, But i am not able load local html page. Surprisingly web view is loading 'http:google.com' properly, not my local html file. I have tried almost all possible link of SO. The error message is'Web Page could not be loaded'
WebView view = (WebView) findViewById(R.id.webView1);
view.loadUrl("file:///assets/www/trialhtml.html");![enter image description here][1]
Create your HTML file and place it in the assets folder, then you need to add these lines to the onCreate method like this:
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
WebView webView = new WebView(this);
webView.loadUrl("file:///android_asset/test.html");
setContentView(webView);
}
Here is the final result:
try below code :-
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
String myURL = "file:///android_asset/index.html";
WebView view = (WebView) findViewById(R.id.webView1);
/*By default Javascript is turned off,
* it can be enabled by this line.
*/
view.getSettings().setJavaScriptEnabled(true);
view.setWebViewClient(new WebViewClient());
view.loadUrl(myURL);
}
Check ur file exist or not and also you can clean the project, and rebuild it.
paste your .html file in assets folder of your project folder. and create an xml file in layout folder with the fol code
WebView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/webview"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
/>
add fol code in activity
setContentView(R.layout.my);
WebView mWebView = null;
mWebView = (WebView) findViewById(R.id.webview);
mWebView.getSettings().setJavaScriptEnabled(true);
mWebView.loadUrl("file:///android_asset/www/trialhtml.html"); //new.html is html file na
Trouble in loading local html file from asset into a webview. HTML page has javascript code, so I have used webview.setJavaScriptEnabled(true) to enable it. It loads the html page, but not all the widgets properly. Any suggestions...
public class ViewWeb extends Activity {
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.webview);
WebView wv;
wv = (WebView) findViewById(R.id.webView1);
wv.loadUrl("file:///android_asset/hello.html");
}
}
I'm trying to load a html file in my webview using this codes. but is show Webpage not available
public class MainActivity extends Activity {
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(com.frux.web.R.layout.activity_main);
WebView web = (WebView) findViewById(R.id.web1);
web.loadUrl("file:///android_assets/www/webpage");
}
}
I already edit my codes into this but still webpage not available
public class MainActivity extends Activity {
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
WebView web = (WebView) findViewById(R.id.web1);
web.getSettings().setJavaScriptEnabled(true);
web.loadUrl("file://android_assets/www/index.html");
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.activity_main, menu);
return true;
}
}
I just mistype the assets. It should be "asset" without "S".
The file should be extension with html
The easiest way would probably be to put your web resources into the assets folder then call webView.loadUrl("file:///android_asset/www/filename.html");
try this will help you...
web.loadUrl("file:///android_assets/www/webpage");
webpage is a folder or a file? I guess it should be
web.loadUrl("file:///android_assets/www/webpage/file.html");
or
web.loadUrl("file:///android_assets/www/webpage.html");
Write below line
setContentView(R.layout.activity_main);
web.loadUrl("file:///android_assets/www/webpage/index.html");
instead of
setContentView(com.frux.web.R.layout.activity_main);
web.loadUrl("file:///android_assets/www/webpage");