Is it possible to affect mobile browser contents with an Android app? - android

I'm starting to build an Android app with my limited knowledge and I'm not sure if this functionality is possible, but hopefully the great minds at SO can help though!
The basic functionality of the app would be similar to the user entering a text string such as "Chicago Bulls" into the app and then when they're using their mobile browser, all instances of that text string on a page are formatted into bold font and red letters for example.
Is this possible with an Android app at all? Or something similar?
I Googled for building Android apps integrating with mobile browsers, but I wasn't able to find anything relevant. I thought that potentially I could build a mobile Chrome extension, but it looks like Chrome Extensions aren't supported by Android yet?

I am not clear your question. I assume that you want to change some font within webview? If so then you can try
Example:
a) Init you webview
webview = (WebView) findViewById(R.id.webView);
b) Create you won stylesheet and attach to html body
String pish = "<html><head><style type=\"text/css\">#font-face {font-family:
MyFont;src: url(\"file:///android_asset/font/BMitra.ttf\")}body {font-family:
MyFont;font-size: medium;text-align: justify;}</style></head><body>";
String pas = "</body></html>";
c) Load on webview
String webContent = pish + YourTxext + pas;
String internalFilePath = "file://" + getFilesDir().getAbsolutePath() + "/";
webview.loadDataWithBaseURL(internalFilePath, webContent, "text/html", "UTF-8", null);

Related

My jqMath is not formatting \text and \table correctly

I have finally gotten jqMath to work in Android Studio, but then I realize that it does not format the \text and \table correctly!
https://mathscribe.com/author/jqmath.html
At the jqMath homepage above, the examples show how to use \text and \table.
In my project I use this string.
$$\text"Molarity" = \text"moles of solute" / \text"liters of solution"$$
And this code for jqMath.
WebView webView = new WebView(context);
WebSettings webSettings = webView.getSettings();
webSettings.setJavaScriptEnabled(true);
String path="file:///android_asset/";
String js = "<html><head>"
+ "<link rel='stylesheet' href='"+path+"jqmath-0.4.3.css'>"
+ "<script src='"+path+"jquery-1.4.3.min.js'></script>"
+ "<script src='"+path+"jqmath-etc-0.4.6.min.js'></script>"
+ "</head><body>"
+ "<script>var s = '"+formulaText+"';jqMath.parseMath(s);document.write(s);</script></body>";
webView.loadDataWithBaseURL( "file:///android_asset/", js, "text/html", "utf-8", null );
And this is the result.
here
When I entered the same thing in on the homepage it displayed this.
Please help - I have no idea why the formula formatting is working, but \text and \table and all the \ command isn't formatting right!?!?
Ok so, I ended up finding the answer myself. The problem wasn't with the jqMath! It was with Android Studio! Android Studio has this weird way of parsing backslashes \ so I had to change the text from
$$\text"Molarity" = \text"moles of solute" / \text"liters of solution"$$
to
$\\\\text\\\"Molarity\\\" = \\\\text\\\"moles of solute\\\" / \\\\text\\\"liters of solution\\\"$
This is because strings in Android Studio seem to undergo automatic backslash parsing.
So my previous text was getting changed before undergoing jqMath format. By adding more backlashes (four backslashes for every backslash) I can turn the string into the right string to go through jqMath!

Webview doesnt load image with String Html

Im making an app in which i show one wikipedia page, for testing im create a new project.
mvw.getSettings().setJavaScriptEnabled(true);
mvw.getSettings().setLoadsImagesAutomatically(true);
mvw.getSettings().setAllowContentAccess(true);
mvw.getSettings().setDomStorageEnabled(true);
mvw.setWebViewClient(new mostrarEnAplicaion());
String htmlString = "<!DOCTYPE html><html><body style = \"text-align:center\"><img src=\"https://en.wikipedia.org/?curid=52633874#/media/File:Wat_Phra_Mahathat_Woramahawihan,_Nakhon_Si_Thammarat.jpg\" alt=\"pageNo\" height=\"100%\" width=\"100%\"></body></html>";
mvw.loadDataWithBaseURL(null,htmlString,"text/html","UTF-8","about:blank");
If I load the URL directly works fine
mvw.loadUrl("https://en.wikipedia.org/wiki/Santa_Mar%C3%ADa_del_Naranco");
I have the permission.INTERNET on the Manifest but doesnt work, anyone know the solution?
After look for a while, I just see that in this line
mvw.loadDataWithBaseURL(null,htmlString,"text/html","UTF-8","about:blank");
I only have to change the null (BaseUrl) for the url of the website in which the images are storage, in this case "https://en.wikipedia.org/"

why my webview displays differently this link than my browser

I'm tring to load this url in a WebView :
https://crocodoc.com/view/PFk2xRsJYPjfDhhm-2nfzi1CnZtUvvXqpy7Uh7uCcFGTfiFDbVpzbgJ2kSUyGGRkf1M_eq_COHKAf14Okz0ShL0WzoNiprqf2UWXUSP7hJ0CHiCGaP4Vs7CHKJAW2A
I tried the basic way :
wv.loadUrl("..");
within an Iframe :
String str2 = "<body style=\"margin:0px;padding:0px;overflow:hidden\">\n" +
"<iframe src=\""+"https://crocodoc.com/view/"+sessionId+"\" frameborder=\"0\" style=\"overflow:hidden;height:100%;width:100%\" height=\"100%\" width=\"100%\"></iframe>\n" +
"</body>";
wv.loadData(str2, "text/html","utf-8");
With a classic client, a chrome chient, enabling more of less every WebView Setting that I could think of. But I loose the controls, I just see an horribele zommed view of the document, I can't zoom etc...
I only left :
wv.getSettings().setJavaScriptEnabled(true);
In my phone browsers It displays as I want it to.
So my question is : why ? how should I do to make it look the same way than in firefox or chrome.
Thanks

Android WebView not loading local html string correctly

I'm working on a project where I'm receiving some html string which I should show in WebView. The problem is that the html string contains only body part, without any tags like <html>, <head>, <body> and when I'm trying to show the received string in webView the result is showing the tags included in html string received from server.
Here is some example what I receive and what I'm doing :
Html String received from server :
05-30 14:02:56.785: D/(16970): body : <p align='justify'>The so-called Fiscal Compact is unlikely to be approved by the Latvian parliament in the second and final reading as four opposition MPs said that they would not back the initiative, local media reported on Tuesday evening. The second reading is scheduled for May 31. Under local legislation, the bill should be backed by 67 MPs in the 100-seat parliament, though the ruling coalition controls only 56 seats and depends on the opposition Union of Greens and Farmers (ZZS) to approve it. During the first reading 68 MPs backed the bill. The four MPs from ZZS will meet PM Dombrovskis later today to discuss the issue. They want more clarity about the country’s participation in neighboring Lithuania’s nuclear power plant project. Previously, ZZS said it extends its support on condition that the government fights for higher subsidies for farmers in the 2014-2020 EU budget framework.</p>
Here is my code :
String bodyTxt = cursor.getString(cursor.getColumnIndex("body"));
Log.d("","body : "+bodyTxt);
String newBody = "<html><body>" + bodyTxt + "</body></html>";
body.loadDataWithBaseURL(null, newBody, "text/html", "utf-8", null);
and the result is this :
I've tried using body.loadData(newBody, "text/html", "UTF-8");
, but this way didn't even show me the result.
So any ideas which I'm doing wrong and how can I fix this?
Thanks in advance!
As I can see from your logs you are receiving the html tags not with < symbol,but like >. I guess if you replace these characters in your html string it will work as it should be. Try this :
String bodyTxt = cursor.getString(cursor.getColumnIndex("body"));
String replaced = bodyTxt.replace("<","<");
Log.d("","replaced : "+replaced);
String replaced2 = replaced.replace(">", ">");
Log.d("","replaced2 : "+replaced2);
Log.d("","body : "+bodyTxt);
String newBody = "<html><body>" + replaced2 + "</body></html>";
body.loadDataWithBaseURL(null, newBody, "text/html", "utf-8", null);
you can use this
webview.loadData(Html.fromHtml(htmlData),"text/html","utf-8");
This is how i do it;
String htmlContent = "<html><body>hello</body></html>";
WebView wv = (WebView) findViewById(R.id.my_view);
wv.loadData(htmlContent, "text/html", "UTF-8");
Investigate how the data looks in the db, all the "<>" and such are quoted with "<", this might be the problem you looking for.

Is there any way to have WebView auto-link URLs and phone numbers in Android?

If a page has a URL or a phone number on it that isn't a link is there any way to have WebView recognize it and automatically turn it into a link like you can with TextViews?
With a TextView you would simply set the android:autoLink to the desired settings:
<TextView
android:autoLink="web|phone"
... />
but I can't find any equivalent for WebView.
If you are loading your own (web) content from a String, then you can do something like this:
final String content = "My email is: firstname#email.com ...";
Spannable sp = new SpannableString(content);
Linkify.addLinks(sp, Linkify.ALL);
final String html = "<body>" + Html.toHtml(sp) + "</body>";
myWebView.loadData(html, "text/html", "utf-8");
I don't know about any way which would make this work just by changing a setting, but a workaround would be to wait until the web page finishes loading and then do:
yourWebView.loadUrl("javascript:(function(){ /* code that creates links */ })()");
This will inject javaScript into the already loaded web page.
There's a slightly longer example available here: http://lexandera.com/2009/01/injecting-javascript-into-a-webview/.
You can find the JavaScript source for creating links if you take a look at the source of Linkify script for Greasemonkey (it's a plugin for Firefox in case you're not familiar with it). I believe it comes with the default install.

Categories

Resources