I have a locally stored HTML file in my assets folder that I am accessing through this:
#Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
Global.setUpButton(this);
WebView webView = new WebView(this);
setContentView(webView);
//Sets up the settings for the webView
webView.loadUrl("file:///android_asset/map.html");
webView.getSettings().setLoadsImagesAutomatically(true);
webView.getSettings().setBuiltInZoomControls(true);
webView.getSettings().setUseWideViewPort(true);
webView.getSettings().setLoadWithOverviewMode(true);
webView.setInitialScale(0);
}
I was wondering if there is any way I would be able to view and/or edit the source of the HTML file loaded, as I may need to manipulate certain elements.
I found a workaround instead.
Created a JavascriptInterface that will allow me to append any HTML tags I need to. This is easier because I won't have to reload the page, which reduces loading time.
Sure. You create a layout with an EditText box, you load the file and display it in the box, then after editing, you store the new string in the file.
added:
You can use AssetManager to read the data as a String from the URL. Then you put the string in the EditText. Then the user edits it. Then you get the edited html from the editText, and do what you wanted to do with it in the first place. If I understand your questions correctly.
Related
I have read several topics here on SO about this, at first all of them seem related but than when i read the "solution" code i cannot understand where the String that keeps the code is.
What i want to do is load a local HTML file, than modify it with some javascript. And after i have modified it i would like to either replace the unmodified HTML file with the modified HTML file, or create a new HTML file from the modified HTML. So after this process i would have the modified HTML file saved on the users SD card.
I would have loved it if there where a functions like this:
String htmlContent = myWebView.getSouce();
Than i could just create an HTML file from that String and save it to the sd card.
This is my code so far.
final WebView webview = (WebView)rootView.findViewById(R.id.webView);
webview.getSettings().setJavaScriptEnabled(true);
webview.setWebViewClient(new WebViewClient() {
#Override
public void onPageFinished(WebView view, String url)
{
/* This loads a javascript to the Html file and changes its design*/
view.loadUrl("javascript:(function() { "+
"Some Modifications to the test.html file"+
"})()");
//Now when i have modified the above test.html i would like
//to get the modified HTML (The HTML now displaying in my webview).
//So i was hoping i could write something like this:
// String htmlContent = view.getSouce();
}
});
webview.loadUrl("file:///android_asset/test.html");
If you don't understand javascript interfaces:
http://developer.android.com/reference/android/webkit/WebView.html#addJavascriptInterface(java.lang.Object, java.lang.String)
http://developer.android.com/guide/webapps/webview.html#BindingJavaScript
i am receiving html from server and need to show it to user with simple styles (like bold, italic etc) and user should be able to edit it and impart similar style in it. i used webview, but it is not editable. I have seen some prev ques about rich text editor, but none of them are clear about what to do, or i could not understand as i am a newbie in this field. Thanks in advance. Here data is a string which is recieved from server, it contains html tags within. I need an editor which can add new text, edit prev one, bold/unbold selected text and some similar functions
webView = (WebView) findViewById(R.id.webView1);
webView.getSettings().setJavaScriptEnabled(true);
webView.getSettings().setBuiltInZoomControls(true);
webView.setEnabled(true);
webView.loadDataWithBaseURL("file:///android_asset/", data, "text/html",
"utf-8", null);
I am experimenting with Android code: I would like to store one value using HTML 5 local storage. For this exercise I' using a page as simple as this one:
http://www.w3schools.com/html5/tryit.asp?filename=tryhtml5_webstorage_local_clickcount
My manifest does allow me to hit the internet, and it is min-sdk of 7.
Here is my java code:
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
webview = (WebView) findViewById(R.id.webview);
WebView webView = (WebView) findViewById(R.id.webview);
webView.addJavascriptInterface(new JavaScriptInterface(this), "Android");
WebSettings webSettings = webview.getSettings();
webSettings.setJavaScriptEnabled(true);
webSettings.setDatabasePath("");
webSettings.setDomStorageEnabled(true);
webview.setWebChromeClient(new WebChromeClient());
webview.loadUrl("http://www.xyz.com/test.html");
///xyz.com/test.html is a sample :)
webview.setWebViewClient(new HelloWebViewClient());
}
My problem is that when I close the app, the locally stored value is no longer there. I can browse to the same page using the default browser, and the value is persistent even after closing the emulator, which is exactly the behavior that I am looking for.
This is probably a something extremely simple....any ideas?
It appears the empty string DatabasePath is the problem. I tried similar code and with an empty string path, the value does not persist after the app exits. If I define a specific database path, the value persists as expected.
Try:
webSettings.setDatabasePath("/data/data/"+this.getPackageName()+"/databases/");
If your app use multiple webview you will still have troubles : localStorage is not correctly shared accross all webviews.
If you want to share the same data in multiple webviews the only way is to repair it with a java database and a javascript interface.
This page on github shows how to do this.
hope this help!
Couldn't get it working on all devices (especially with ICS) - even with database path, enabling DOMStorage etc. - using cookies instead helped me out.
I have a requirement where there is a URL = "http://www.example/Open.pdf"
Now from my android application I want to open this PDF file directly in the default PDF viewer.
The moment I click on this link on the webpage, user should be presented with a default PDF viewer opened with this document.
Note: This file should not be stored on the SD card.
How do I proceed for this implementation?
We can open PDF file in the webview without caching it. Write below code in "onCreate" method .
Working code :
String url = "http://www.example.com/abc.pdf";
final String googleDocsUrl = "http://docs.google.com/viewer?url=";
WebView mWebView=new WebView(this);
mWebView.getSettings().setJavaScriptEnabled(true);
mWebView.getSettings().setPluginsEnabled(true);
mWebView.setWebViewClient(new WebViewClient() {
public boolean shouldOverrideUrlLoading(WebView view, String url){
view.loadUrl(url);
return false; // then it is not handled by default action
}
});
mWebView.loadUrl((googleDocsUrl + url));
setContentView(mWebView);
What happens here is you open the PDF using Google Docs. Best Advantage of using above method is the lazy loading of PDF. Does not matter how heavy the PDF is. Google Docs takes care of it.
You can view the pdf in the WebView using googleDocs.
WebView webView = (WebView) findViewById(R.id.my_webview);
webView.setWebViewClient(new MyWebViewClient());
webView.addView(webView.getZoomControls());
webView.getSettings().setJavaScriptEnabled(true);
webView.loadUrl("http://docs.google.com/gview?embedded=true&url=http://myurl.com/demo.pdf");
There is no way you can open a default PDF view from your application.
If your file is on the server and you want to open it without downloading then this might also pose a greater security concern. If external applications like default adobe reader can access the content on your server, then this is breaking the security framework altogether.
So, best option would be to launch a new instance of browser or webview and show the PDF document in google docs to the user.
This way user can read the document and get back to the recent state of the application as well.
You can view the pdf in the WebView using googleDocs.
WebView webView = (WebView) findViewById(R.id.my_webview);
webView.setWebViewClient(new MyWebViewClient());
webView.addView(webView.getZoomControls());
webView.getSettings().setJavaScriptEnabled(true);
webView.loadUrl("http://docs.google.com/gview?embedded=true&url=http://myurl.com/demo.pdf");
do you have the others solution besides view pdf file using http://docs.google.com/gview?embedded=true&url=http://myurl.com/demo.pdf
I'm playing with http://www.siegmann.nl/epublib on Android. Can someone please explain the right way to
read epub HTML content,
how to show this on Android (using WebView?),
how to split content into pages and
how to search the content.
Thx 10x.
Answers #2:
Extract epub on file system /mnt/sdcard/epub/
Loading values in webview
protected void onCreate(Bundle savedInstanceState) {
setContentView(R.layout.epub_reader);
webView = (WebView) findViewById(R.id.webview);
Book book = (new EpubReader()).readEpub(new FileInputStream(filename);
String baseUrl="file://mnt/sdcard/epub/OEBPS/"
String data = new String(book.getContents().get(2).getData());
webView.loadDataWithBaseURL(baseUrl, data, "text/html", "UTF-8", null);
}
Regarding your questions:
How to read epub HTML content
I'm not sure what you mean. Do you want all the content? Or something specific?
All the content can be retrieved using Book.getContent().
how to show this on Android (using WebView?),
I would use WebView for this. Haven't tried that myself though.
how to split content into pages
This I don't know what would work best.
how to search the content.
The nl.siegmann.epublib.search package in epublib-tools has code for a simple search functionality.