Dynamically changing css values in a webview without reloading (Android) - android

I have a WebView based app to which I want to add a 'night-mode'.
My first instinct was to just have the program render the right CSS values each time depending on whether night-mode was on or not, and it works great, but it means that the page has to be reloaded each time the mode is toggled.
Is there any way I can change the css values retro-actively without having to reload the page?

You could inject code into your page's DOM. So you could for example have a reset stylesheet, and then inject a day or night mode class with javascript into the dom as required like this:
public class WebClient extends WebViewClient {
#Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
view.loadUrl(url);
return true;
}
#Override
public void onPageFinished(WebView view, String url)
{
view.loadUrl("javascript:document.getElementById(id).style.property=new style");
}
}
HTH

Related

How to get the WebView object inside a custom WebViewClient?

I created a new function in a CustomWebViewClient class that needs to use the webViewObject. However, I dont seem to find a way how to access it without passing the parameter to the constructor.
This is what the class looks like:
public class CustomWebViewClient extends WebViewClient {
public CustomWebViewClient(){
myFunction();
}
private myFunction(){
//needs to use the webView object
}
#Override
public boolean shouldOverrideUrlLoading(WebView view, String check_url){
//some code here
return false
}
}
This is how to set the custom class to a webViewObject:
CustomWebViewClient myWebViewClient = new CustomWebViewClient();
webViewObject.setWebViewClient(myWebViewClient);
Notice that shouldOverrideUrlLoading function gets the parameter. So its possible its there somewhere.

Android WebViewClient / WebViewAssetLoader implementation error

I am trying to load a webpage in asset folder and app's local storage using the webasset loader using the code provided by google.
final WebViewAssetLoader assetLoader = new WebViewAssetLoader.Builder()
.addPathHandler("/assets/", new WebViewAssetLoader.AssetsPathHandler(this))
.build();
myWebView = (WebView) findViewById(R.id.webview);
myWebView.setWebViewClient(new WebViewClient() {
#Override
#RequiresApi(21)
public WebResourceResponse shouldInterceptRequest(WebView view,
WebResourceRequest request) {
return assetLoader.shouldInterceptRequest(request.getUrl());
}
//This gives the error: shouldInterceptRequest(WebView, WebResourceRequest)' is already
defined in 'Anonymous class derived from android.webkit.WebViewClient
#Override
#SuppressWarnings("deprecation") // for API < 21
public WebResourceResponse shouldInterceptRequest(WebView view,
WebResourceRequest request) {
return assetLoader.shouldInterceptRequest(Uri.parse(request));
//This gives an error for the request variable. It says : Required type: String and
Provided: WebResourceRequest. When I cast it as a string type, it
gives an error similar to first error.
}
});
Can someone please guide me here? What am i doing wrong here, i am using the exact code given by google. Any tutorial on using this class?
The snippet you found in the documentation is wrong, and I have already opened an issue for it.
The problem is that the second method's signature, which refers to a deprecated implementation, should instead be:
public WebResourceResponse shouldInterceptRequest(WebView view, String url) {
Use this and it'll compile (and work) as expected.

.htaccess in iframe in a Webview

I have a webview app that I've created that pulls in a password protected camera feed with a .htaccess file protecting it. Of course, it says that it's unauthorised.
I found this bit of code:
private class MyWebViewClient extends WebViewClient {
#Override
public void onReceivedHttpAuthRequest(WebView view,
HttpAuthHandler handler, String host, String realm) {
handler.proceed("user", "password");
}
}
I'm assuming this would work if the whole page was protected but it doesn't seem to work where it's the iframe that's protected.
Is there another way I can do this?

How to retrieve width and heigh of the webview in Cordova Android?

I write a Cordova plugin for Android.
Here is a simple initialization function which resume mu probleme
public class MyPlugin extends CordovaPlugin {
CordovaWebView _webView;
ViewGroup _root;
public void initialize(CordovaInterface cordova, final CordovaWebView webView) {
super.initialize(cordova, webView);
_webView = webView;
_root = (ViewGroup) _webView.getView().getParent();
Log.d(TAG, Integer.toString(root.getLayoutParams().width)); // -1
Log.d(TAG, Integer.toString(webView.getView().getLayoutParams().width)); //-1 too
}
public boolean execute(final String action, final CordovaArgs args, final CallbackContext _callbackContext) throws JSONException {
Log.d(TAG, Integer.toString(_root.getLayoutParams().width)); // -1
Log.d(TAG, Integer.toString(_webView.getView().getLayoutParams().width)); // -1
...
}
}
Note that when logging FrameLayout.LayoutParams.MATCH_PARENT it displays -1.
Does the default WebView layout is set with MATCH_PARENT in Cordova?
How I could retrieve the WebView or the parent of the WebView with/heigh ?
After page rendering get finished you can get that event in onPageFinished method of WebView client.
In that method you can use
webview.getContentWidth()
and
webview.getContentHeight()
for getting content's height and width of webpage.
Note:- This approach is for the android stock webview. Hoping it will work for cordova webview.
I was using the wrong method. getLayoutParams() does not give the actual dimensions.
My code work in both initialize and execute function with:
Log.d(TAG, Integer.toString(_webView.getView().getWidth()));
Log.d(TAG, Integer.toString(_webView.getView().getHeight()));

Get type of object when click on webview - Android

I am developing a webview in which i have to define the type of object when i click to this object. For example, when i click to a link, webview understand it is a link and i can get link (returned object); when i click to a image, webview understand it is a image and return image object. I want to get the object type to do some more activities. For example, when i long click to the image, there are some action for me: download, set as background,... when i long click to a link, there are some option like: open in new tab, add to bookmark,... Does anyone know the solution. Thank u very much :)
I think that you cannot have direct visibility to the "downside" html/javasctipt from webview listeners.
(WebView reference here: http://developer.android.com/reference/android/webkit/WebView.html)
However, if you are developing the web application running in the WebView, you can obtain the same result by using JavascriptInterface methods, implementing the javascript methods to "upside" return objects toward android app.
Example interface class:
public class JSUpInterface {
protected Context mContext;
public JSUpInterface(Context c) {
mContext = c;
}
#JavascriptInterface
public void showToast(String message) {
Toast.makeText(mContext, message, Toast.LENGTH_SHORT).show();
}
#JavascriptInterface
public void getLink(String link) {
// shows in a toast the clicked link
Toast.makeText(mContext, link, Toast.LENGTH_SHORT).show();
}
}
WebView attaching:
jsUi = new JSUpInterface(context);
webview.addJavascriptInterface(jsUi, "jsui");
your html:
www.google.com

Categories

Resources