Android webview append css to existing site - android

I am using the webview as following
WebView webView = (WebView) this.findViewById(R.id.webView);
webView.getSettings().setJavaScriptEnabled(true);
webView.loadUrl("https://m.facebook.com");
It loads the page fine. Now I am trying to add a line head section of the page before it renders in the web view. e.g.
<style>.test{color:red}</style>
is it possible to add. if yes then how??

this can be done using evaluateJavascript
webView.evaluateJavascript("javascript: document.getElementById('header').setAttribute('style','background-color: red;') ", new ValueCallback<String>() {
#Override
public void onReceiveValue(String value) {
//in case you have some return value from your script
}
});

Related

Denied starting an intent without a user gesture Webview Android

Trying to redirect local html page in android webview using Javascript redirect, gets denied starting an intent in Logcat:
Testing on android 5.1.1
document.location = "index.html";
Denied starting an intent without a user gesture, URI:
file:///android_asset/index.html
I read the documentation in 1,000 attempts Android.developer and this was my solution
I do not know if you understand, I speak Spanish
webView.setWebViewClient(new WebViewClient() {
#Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
return false;
}
});
This worked for me:
webView.setWebViewClient(new WebViewClient());
There are few issues here.
From newest androids, the WebView and Chrome Client is separated application which can be automatically updated without user intention.
From Chrome x >= 25 version, they changed how loading url is working in android application which is using webview component. https://developer.chrome.com/multidevice/android/intents Looks like they are blocking changing url without user gesture and launched from JavaScript timers
Solution here is to force user to activate URL change, for example on button click.
Also, you can override method mentioned above "shouldOverrideUrlLoading" in WebView client.
As alternate, i figured out was to add addJavascriptInterface each button click event fire action to JavascriptInterface
webView.addJavascriptInterface(new java2JSAgent(), "java2JSAgentVar"); //webView webview object
public class java2JSAgent
{
#JavascriptInterface
public String getContacts()
{
String jsonResponse = "{result:'redirected'}";
runOnUiThread(new Runnable() {
#Override
public void run() {
webView.loadUrl("file:///android_asset/index.html");
}
});
return jsonResponse;
}
}
might not be a good approach but atleast its working :-)
Thanks

Create intent on click WebView link

Ok, there will be two types of links in the content of my WebView. The behavior will be defined by the format of the link.
(1) Open the link in a browser. (The url begins with "openbrowser:")
(2) According to the link, open another Activity in the same project.
(The url will be "openactivity")
I am not sure if it is possible to create a map for the WebView which maps from a url pattern to an intent. For example, by default, if the url begins with "mailto:" the WebView will create an intent to open the mailbox. Could I define other mappings for my WebView?
I know there is a way to set a WebViewClient and override the shouldOverrideUrlLoading() method. But in API level 19, the function is not guaranteed to be called:
shouldOverrideUrlLoading() not called
So is it possible to set this url pattern to intent mapping as a general settings of the WebView?
Using a WebViewClient should be enough. We've had no problems with API level 19. For example:
WebView webView = new WebView(this);
String html = "<html><body>Test it</body></html>";
webView.loadData(html, "text/html", "utf-8");
webView.setWebViewClient(new WebViewClient()
{
#Override
public boolean shouldOverrideUrlLoading(WebView view, String url)
{
if (url.startsWith("showmessage"))
{
Toast.makeText(MainActivity.this, url, Toast.LENGTH_SHORT).show();
return true;
}
return false;
}
});

Android WebView loading part of a website

Some websites have comments portion and many other unnecessary portions. I want to just display the main article excluding the other portions in my custom Android WebView.
I have tried the example from Display a part of the webpage on the webview android
But it still shows the full website.
There is also the loaddatawithbaseurl class in Android but it is not working too as you can't specify till which portion of a website to show using this class.
Is there any other way to do this?
Here it is with some changes...
webView.getSettings().setJavaScriptEnabled(true);
webView.setWebViewClient(new WebViewClient() {
#Override
public void onPageFinished(WebView view, String url)
{
String javaScript = "javascript: var form = document.getElementsByClassName('university_corner');"
+ "var body = document.getElementsByTagName('div');"
+ "body[0].innerHTML = form[0].innerHTML";
webView.loadUrl(javaScript);
}
});
webView.loadUrl(url);

Android - how to intercept a form POST in android WebViewClient on API level 4

I have a WebViewClient attached to my WebView like so:
webView.setWebViewClient(new MyWebViewClient());
Here is my implementation of MyWebViewClient:
private class MyWebViewClient extends WebViewClient {
#Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
webView.loadUrl(url);
return true;
}
}
I give the WebView a URL to load via loadUrl(). If I have a link (a href...) in the page, my shouldOverrideUrlLoading method is called and I can intercept the link click.
However, if I have a form whose method is POST, the shouldOverrideUrlLoading method is not called.
I noticed a similar issue here: http://code.google.com/p/android/issues/detail?id=9122 which seems to suggest overriding postUrl in my WebView. However, this API is only available starting from API level 5.
What can I do if I'm on API level 4? Is there any other way to intercept form posts?
This is known issue, that shouldOverrideUrlLoading don't catch POST. See http://code.google.com/p/android/issues/detail?id=9122 for details.
Use GET! I personally tried using POST, because I expected some limitation of GET parameters (i.e. length of URL), but I just successfully passed 32000 bytes through GET locally without any problems.
Do you really need to use a POST? If you want to handle formdata locally, why not have a piece of javascript handle your form and interface with "native" java code using addJavascriptInterface. E.g.
WebView engine = (WebView) findViewById(R.id.web_engine);
engine.getSettings().setJavaScriptEnabled(true);
engine.addJavascriptInterface(new MyBridge(this), "bridge");
engine.loadUrl(...)
Your bridge can be any class basically and you should be able to access its methods directly from javascript. E.g.
public class MyBridge {
public MyBridge(Context context) {
// ...
}
public String doIt(String a, String b) {
JSONArray result = new JSONArray();
result.put("Hello " + a);
result.put("Hello " + b);
return result.toString();
}
Your html / javascript could look like:
<script type="text/javascript">
$("#button").click(function() {
var a = $("#a").val();
var b = $("#b").val();
var result=JSON.parse(bridge.doIt(a, b));
// ...
}
</script>
<input id="a"><input id="b"><button id="button">click</button>
I think you can override onLoadResource(WebView view, String url) from WebViewClient. This function is Added in API LEVEL 1.
This function is called when WebView will load the resource specified by the given url. Resource include js, css, iframe embeded url. Code example like this:
#Override
public void onLoadResource(WebView view, String url) {
if (url.indexOf("http://www.example.com") != -1 && view != null) {
// open url in default browser
view.stopLoading();
view.getContext().startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse(url)));
}
}

Android Webview Anchor Link (Jump link) not working

I have a WebView in my Android App that is loading an HTML string using the loadDataWithBaseURL() method. The problem is that local anchor links (<a href="#link">...) are not working correctly. When the link is clicked, it becomes highlighted, but does not scroll to the corresponding anchor.
This also does not work if I use the WebView's loadUrl() method to load a page that contains anchor links. However, if I load the same URL in the browser, the anchor links do work.
Is there any special handling required to get these to work for a WebView?
I am using API v4 (1.6).
There isn't much to the code, here are the relevant parts of some test code I've been working with:
WebView detailBody = (WebView) findViewById(R.id.article_detail_body);
String s = "LINK!<br/><br/><br/><br/><br/><br/><br/><br/><br/><br/><br/><a name=\"link\"></a>Testing!";
detailBody.loadDataWithBaseURL(API.HomeURL(this), s, "text/html", "utf-8", "");
It looks like the problem is that I had a WebView within a ScrollView. The WebView isn't able to scroll to an anchor link when configured like this. After refactoring my layout to eliminate the ScrollView, the anchor links work correctly.
Android Webview Anchor Link (Jump link) Not Working
True, WebView Anchor Links, or Jump Links initiated through the #LINK extension to the URL will not work when the WebView is inside of a ScrollView(*).
Still, the problem for me and apparently others is that the #LINK does work when launched from a touch in an href, but is ignored when launched via the URL. Other symptoms include navigating to the link only on the first time in a session or navigating to the bottom of the html file.
The Solution is to load the url after a short delay.
Here is an example:
My html is saved in assets: res/assets/help.html
With anchors like this:
<a name="helplinkcontacts"/>
And loaded like this:
final String baseUrl = "file:///android_asset/help.html#helplinkcontacts";
final WebView helpTextView = (WebView)findViewById(R.id.help_dialog_text);
helpTextView.loadUrl(baseUrl); // Ignores Anchor!!
I added the timer like this:
final String baseUrl = "file:///android_asset/help.html#helplinkcontacts";
final WebView helpTextView = (WebView)findViewById(R.id.help_dialog_text);
Timer timer = new Timer();
timer.schedule(new TimerTask() {
#Override
public void run() {
helpTextView.loadUrl(baseUrl);
}
}, 400);
Note: Shorter delays, such as 100ms failed to navigate to the link.
(*) It turns out that so many of us have our WebViews inside of ScrollViews because we started out with a TextView rendering Spannable text which both supports some HTML and requires a ScrollView. Anyways, remove the ScrollView as soon as you convert your TextView into a WebView.
My Solution is , Check this Answer
public class MainActivity extends Activity {
WebView myWebView;
public static boolean flag = false;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
myWebView = new WebView(this);
myWebView.getSettings().setJavaScriptEnabled(true);
myWebView.loadUrl("file:///android_asset/chapters.html");
setContentView(myWebView);
myWebView.setWebViewClient(new WebViewClient() {
public void onPageFinished(WebView view, String url) {
if (url.contains("#") && flag == false) {
myWebView.loadUrl(url);
flag = true;
} else {
flag = false;
}
}
});
}
}
I had a similar problem. Nothing would jump to anchor tags in the html. I didn't have my WebView within a ScrollView. Instead the problem was the base url I passed into loadDataWithBaseURL did not have a colon (':') in it. I believe the baseUrl needs to have some text, then a colon, then some more text, for example "app:htmlPage24".
So here's the initial call to my WebView, just to load the data in the string HTML_24:
wv.loadDataWithBaseURL("app:htmlPage24", HTML_24, "text/html", "utf-8", null);
Then I have a list that jumps to sections on the screen, depending on the list item you tap:
sectionsLV.setOnItemClickListener(new OnItemClickListener()
{
public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3)
{
wv.loadUrl("app:htmlPage24#section" + arg2);
}
});
HTML_24 is something like:
<html>
...
<a name="section1"/>
...
<a name="section2"/>
...
<a name="section3"/>
...
</html>
I was also facing the same issue. Anchor link was jumping to arbitrary position. Make sure not to hide webview when loading.
Use Invisible instead of gone.
This change fixed my issue.
try this
String myTemplate = "LINK!<br/><br/><br/><br/><br/><br/><br/><br/><br/><br/><br/><a name=\"link\"></a>Testing!";
myWebView.loadDataWithBaseURL(null, myTemplate, "text/html", "utf-8", null);
the word "Testing!" must be outside of the screen to see it works.
WebView in Android 4.0 fails to open URLs with links in them.
e.g.
"file:///android_asset/help.html#helplinkcontacts"
Here is how I got around it
WebView wv = (WebView) nagDialog.findViewById(R.id.wv);
wv.getSettings().setJavaScriptEnabled(true);
wv.setWebViewClient(new MyWebViewClient(link));
wv.loadUrl("file:///android_asset/help.html");
And define the custom WebViewClient class
class MyWebViewClient extends WebViewClient {
private String link;
public MyWebViewClient(String link) {
this.link = link;
}
#Override
public void onPageFinished(WebView view, String url) {
if (!"".equals(link) && link != null)
view.loadUrl("javascript:location.hash = '#" + link + "';");
}
}

Categories

Resources