I am trying to set an image to a WebView for my app.
I am using the below code to set the image to my WebView,
String imageUrl = " file:///android_res/drawable/dinner_menu.png";
WebView wv = (WebView) findViewById(R.id.yourwebview);
wv.getSettings().setBuiltInZoomControls(true);
wv.loadUrl(imageUrl);
This works fine for Android 2.2 and higher.
But if I try to run the code in 2.1 or lower, it shows an error as,
The requested file was not found /android_res/drawable/dinner_menu.png.
Can anyone help me out.
Try putting your image in your assets folder, and load it using:
WebView webView = new WebView(this);
webView.loadUrl("file:///android_asset/dinner_menu.png");
setContentView(webView);
It should work on earlier versions of Android, but you'll need to manually manage using different versions of your image for different screen sizes / densities.
Related
I realized that not working for under android 4.3 versions.
I have an android app which has webview. When i try to load url "http://instagram.com" it's not working It shows blank page but facebook webpage is working.
It's really important for me please help.
WebView view = (WebView) findViewById(R.id.webView1);
view.getSettings().setJavaScriptEnabled(true);
view.loadUrl("http://www.instagram.com");
Have you tried using the method:
setDomStorageEnabled();
Sets whether the DOM storage API is enabled. The default value is
false.
for example:
WebView view = (WebView) findViewById(R.id.webView1);
view.getSettings().setJavaScriptEnabled(true);
view.getSettings().setDomStorageEnabled(true);
view.loadUrl("http://www.instagram.com");
I had previously problems loading Facebook and Twitter pages in some devices, solved using setDomStorageEnabled() method.
I ahve a webview in my layout screen. I need to display a PDF file in that webview from my assets folder. I tried below code. While running app displays zoom in and out controls with a blank page(our PDF file is kind of large.is it due to that?).
webView = (WebView) findViewById(R.id.webView1);
WebSettings settings = webView.getSettings();
settings.setJavaScriptEnabled(true);
if(Build.VERSION.SDK_INT>=Build.VERSION_CODES.JELLY_BEAN) //required for running javascript on android 4.1 or later
{
settings.setAllowFileAccessFromFileURLs(true);
settings.setAllowUniversalAccessFromFileURLs(true);
}
settings.setBuiltInZoomControls(true);
webView.setWebChromeClient(new WebChromeClient());
webView.loadUrl("file:///android_asset/test.pdf");
I dont want to use external apps to read my PDF. is it possible?then how?
You can use pdf.js from Mozilla to display a PDF in a webview inside an Android application.
Here is the solution...
https://stackoverflow.com/a/21383356/2260073
This question is answered already . For the details refer the following links.
How to read pdf in my android application?.
One more is
stackoverflow.com/questions/10299839/how-to-read-pdf-in-my-android-application/10352422#10352422
After searching a lot about this i came up with my own kind of solution
I used this website to convert my PDF to DOCX and then DOCX to HTML using online converters and then used the following code to load the HTML in my Webview.
String htmlString = "<p>YOUR HTML CODE</p>";
webView.loadData(htmlString, "text/html", "UTF-8");
Hope this helps!
I am trying to embed youtube video in my android app using WebView. Here is the code:
{
WebView mView = new WebView(getContext());
String embeded = loadPlayer(); // this is javascript string containing YtPlayer api
WebSettings webSettings = mPreview.getSettings();
webSettings.setJavaScriptEnabled(true);
mView.setBackgroundColor(535353);
WebChromeClient mChromeClient = new WebChromeClient();
mView.setWebChromeClient(mChromeClient);
mView.setWebViewClient(new WebViewClient());
mView.loadDataWithBaseURL("http://www.youtube.com", embeded, "text/html", "utf-8", null);
}
The problem I am having is my video gets clipped somewhere in the middle and the touch events on video don't work. Here is the link to screenshot:
Any ideas on what I am doing wrong here.
P.S. - This happens only on ICS 4.0.3 and it works fine on JellyBean 4.1
Any help is greatly appreciated.
I just got same type of error.
Here the problem is of Relative-Layout. Don't take Relative Layout when using Webview.
Always go for Linear-Layout. The time i changed Relative to Linear the View was Perfect.
You can turn off hardware acceleration and it fixes this issue for me. However, it does not play any longer on Amazon devices. I'm still looking for a fix to satisfy both issues.
webview.setLayerType(View.LAYER_TYPE_SOFTWARE, null);
I want to use two html in a single webpage so i choose to implement frameset in html. it works fine in all android version except android4.0. If i html file in default browser its working fine but in webview only one frame is loaded not the other ie first frame is override by second frame so it looks only second frame is loading.
here is my code for loading html in webview..
WebView web=new WebView(this);
String url="<html><frameset cols=\"25%,25%\" FRAMEBORDER=NO FRAMESPACING=0 BORDER=0><frame src=\"file:///android_asset/preview.html"
+"\" /><frame src=\"file:///android_asset/preview.html\" /> </frameset></html>";
web.loadDataWithBaseURL("file:/"+baseurl+"/",url, "text/html", "utf-8","");
i dont know why its not supporting in android4.0 whether android webview is not accepting frame in 4.0 or not? If anyone knows please help me..
We have a webview that we are loading from android_assets. This works fine in 2.1, 2.2, and 2.3. However, when we load it in 3.0, we get a "Webpage not available" message.
This only happens on pages where we are passing parameters to the webview:
WebView webView = (WebView) findViewById(R.id.my_webview);
webView.getSettings().setJavaScriptEnabled(true);
webView.addJavascriptInterface(new JavaScriptInterface(this), "androidInterface");
webView.setBackgroundColor(0);
//This works
webView.loadUrl("file:///android_asset/my.html");
//This does not work.
webView.loadUrl("file:///android_asset/my.html" + "?param=value");
Any idea how to get this to work on Honeycomb?
Not answer to the original problem, but a workaround. Since I was already using the JavascriptInterface, I set the value that I wanted to read in Javascript on the JavascriptInterface.