WebView.loadDataWithBaseURL. Go exactly to #label - android

How to go EXACTLY to http://example.com/index.html#label if content to webview loading via WebView.loadDataWithBaseURL ?
By default webview go to root only. How go to label ?

WebView doesn't have such functionality from the box.
You can achieve this with the help of javascript, but this is a bit of ugly.
You should load your html page and then add javascript code to it.
After this you can load your webview and call javascript methods from current page.
Check this link for example how that can be done.
WebView jump to anchor using loadDataWithBaseUrl

Example goes here
webContents.setWebViewClient(new WebViewClient(){
#Override
public void onPageFinished(WebView view, String url) {
String id = "895884";
webContents.loadUrl("javascript:scrollAnchor(" + id + ");");
}
});

Related

Show only certain parts of a web page in a web view, AndroidStudio?

How can I get the certain parts from Website and show in WebView, Android Studio???
Below Image is that I want get part (square in red part)
and site is this : https://kimpga.com
I'd like to display only the red part as an Android web view. What should I do?
and then I used this method: webView.loadUrl("javascript:document.getElementsByClassName('').style.display='none'");
but that website doesn't have div id and doesn't work method!
+) add homepage source
Please understand to attach the overall picture. I'd like to show you the part of div selected in blue through a web view.
I solved it!
using
mWebView.setWebViewClient(new WebViewClient(){
#Override
public void onPageFinished(WebView webView, String url)
{
webView.loadUrl("javascript:(function() { " +
"document.getElementsByClassName(//classname//)[index of document].style.display='none'; " +
"})()");
}
});

Change font WebView for an already loaded site with Android

I have been searching for days but could not find solution for my problem. Please kindly take a look:
Input: a WebView with an already loaded web.
Output: change the font of that loaded web inside the WebView.
What I have done so far is this:
#SuppressLint("SetJavaScriptEnabled")
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mWebView = (WebView) findViewById(R.id.webview);
mWebView.getSettings().setJavaScriptEnabled(true);
mWebView.setWebViewClient(new WebViewClient());
mWebView.loadUrl("http://www.bbc.co.uk/");
and then, this is how I change the font inside webview:
1/ way 1:
mWebView.loadUrl("javascript:(function() { var s=document.createElement('style');s.innerHTML ="
+ " '#font-face{font-family:ZawGyi-One;src:url(\"http://db.tt/OQ1RZoWc\");}"
+ "body,div,h1,h2,h3,input,textarea{font-family:ZawGyi-One! important;}';"
+ "document.getElementsByTagName('head')[0].appendChild(s);document.getElementsByTagName('body')[0].style.fontFamily = \"ZawGyi-One\"})()");
2/ way 2:
mWebView.loadDataWithBaseURL(
null,
"<script>javascript:(function() { var s=document.createElement('style');s.innerHTML ="
+ " '#font-face{font-family:ZawGyi-One;src:url(\"http://db.tt/OQ1RZoWc\");}"
+ "body,div,h1,h2,h3,input,textarea{font-family:ZawGyi-One! important;}';"
+ "document.getElementsByTagName('head')[0].appendChild(s);document.getElementsByTagName('body')[0].style.fontFamily = \"ZawGyi-One\"})() </script>",
"text/html", "UTF-8", null);
3/ way 3:
mWebView.setWebViewClient(new WebViewClient() {
#Override
public void onPageFinished(WebView view, String url) {
// TODO Auto-generated method stub
super.onPageFinished(view, url);
mWebView.loadUrl("javascript:(function() { var s=document.createElement('style');s.innerHTML ="
+ " '#font-face{font-family:ZawGyi-One;src:url('file:///android_asset/fonts/BLKCHCRY.TTF');}"
+ "body,div,h1,h2,h3,input,textarea{font-family:ZawGyi-One! important;}';"
+ "document.getElementsByTagName('head')[0].appendChild(s);document.getElementsByTagName('body')[0].style.fontFamily = \"ZawGyi-One\"})()");
}
});
But unfortunately, none of my solutions works. Even though I have tried to change html file insdie /assets/ folder and also the fonts are stored at /assets/fonts/ folder, I cannot walk through this.
**One more question: is it really possible to change font style inside an already loaded site of webview ? Someone, please kindly guide me.
I am appreciated your great help.
look into this answer..How to change font face of Webview in Android?..
Or try this demo project..https://github.com/mayooresan/Japs-Attack-Ceylon--Android-App
Hope it will helps you.
At last, I have figured it out:
1/ Without making change in the content of webpage you cant change the font.
WebView is basically a view to display web pages That web pages may be static(eg html) or dynamic. But it doesn't change the look and feel of web page. so It wil display the text exactly same as that of webpage. If you need to change the font then you have to change it in webpage (may be in html).
(from Webview change font ? _android)
2/ one more solution for this is about changing font from the rendering engine of webview, for Chrome app, it should be now is Blink, or Internet app, it would be Webkit.
So, if anyone is facing the same problem as I was, my little piece of advice is find the way around, you normally do not have enough time or authorize to make the change at rendering engine of webview. :)
Have a nice day.

Update URL according to the link clicked in webview in android

When I type the url in the text box and click on the Go button the webview loads the required page but when I click on any link inside the webview the address bar which is the text box is not updated with the exact url? How do I go about doing this?
I am trying all varies ways on google not getting the solution - probably key words are wrong. :( - Help on this would be great.
Thanks!
You need to Override onPageFinished method
#Override
public void onPageFinished(WebView view, String url) {
urlEditText.setText(url, TextView.BufferType.NORMAL);
super.onPageFinished(view, url);
}

Non-clickable links in WebView

In my app, I am pulling down some HTML from a web service and displaying it in a WebView. Most of the time the app will display links in the HTML just fine, as in they are clickable and open up the Android Browser. Other times, however, the links are not clickable. It turns out sometimes the service will provide HTML with links that are not inside an href, and are just plain text.
Is there anyway for a WebView to parse the HTML and make these links "clickable"? I know the default Android Browser can do it, but I'm not sure about WebViews.
The Webview may not have built-in detectors to auto-link plain URLs within a page, but you could run a JavaScript function within the WebView to parse the URLs when the page finishes loading.
Basically, something like the following (I haven't checked this code for syntax yet, but it should give you the idea):
final WebView webview = (WebView)findViewById(R.id.browser);
/* JavaScript must be enabled if you want it to work, obviously */
webview.getSettings().setJavaScriptEnabled(true);
/* WebViewClient must be set BEFORE calling loadUrl! */
webview.setWebViewClient(new WebViewClient() {
#Override
public void onPageFinished(WebView view, String url)
{
webview.loadUrl("javascript:(function() { " +
"var exp = /(\b(https?|ftp|file):\/\/[-A-Z0-9+&##\/%?=~_|!:,.;]*[-A-Z0-9+&##\/%=~_|])/ig; " +
"document.getElementsByTagName('body')[0].innerHTML = document.getElementsByTagName('body')[0].innerHTML.replace(exp,'<a href='$1'>$1</a>');" +
"})()");
}
});
webview.loadUrl("http://code.google.com/android");
Note that the above JavaScript borrows the URL-parsing JavaScript regex from " How to replace plain URLs with links?," and more info can be found on JavaScript injection within the Android WebViewClient reference and at http://lexandera.com/2009/01/injecting-javascript-into-a-webview/ (not me).

How to display Web site in Android WebView

I am new to Android WebView, in my application i need to display a web site conatins 3 web pages. In the first web page there will be a link to navigate to the second page and second to third page. I given the URL in the WebView, the first page is displayed perfectly, when i click the link it directly opens the browser application to display the second page. But i want to display the second page in the WebView itself. Please find my code below:
WebView forumView=(WebView)findViewById(R.id.forumView);
forumView.getSettings().setJavaScriptEnabled(true);
forumView.loadUrl("url");
As i said i am very new to WebView my code might be wrong, please help me to solve this problem.
Thanks in Advance,
Rajapandian
This piece of code will help you.
wbb = (WebView) findViewById(R.id.webView_tobe_loaded);
WebSettings wbset=wbb.getSettings();
wbset.setJavaScriptEnabled(true);
wbb.setWebViewClient(new MyWebViewClient());
String url="http://www.google.com";
System.out.println(getdeviceid());
wbb.getSettings().setJavaScriptEnabled(true);
wbb.loadUrl(url);
You'll have to intercept the clicks yourself if you don't want the default Android behavior.
You can monitor events in a WebView using a WebViewClient. The method you want is shouldOverrideUrlLoading(). This allows you to perform your own action when a particular URL is selected.
You set the WebViewClient of your WebView using the setWebViewClient() method.
If you look at the WebView sample in the SDK there's an example which does just what you want. It's as simple as:
private class HelloWebViewClient extends WebViewClient {
#Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
view.loadUrl(url);
return true;
}
}

Categories

Resources