1

I am working with Android Webview and my html file is in assets directory. I have a href in my html:

<a class='edit link1'></a>

Now, I want to change this code dynamically using my Java code when android button widget is pressed and add some style to it like:

<a class='edit link1' style="border-radius:20px;"></a>

Is it even possible? Thanks in advance.

Below is my webView:

 wv1 = (WebView) findViewById(R.id.webview);
    wv1.getSettings().setJavaScriptEnabled(true);
    wv1.getSettings().setLoadWithOverviewMode(true);
    wv1.setScrollBarStyle(WebView.SCROLLBARS_OUTSIDE_OVERLAY);
    wv1.setScrollbarFadingEnabled(false);
    wv1.getSettings().setBuiltInZoomControls(true);
    wv1.getSettings().setPluginState(WebSettings.PluginState.ON);
    wv1.getSettings().setAllowFileAccess(true);
    wv1.addJavascriptInterface(new WebViewJavaScriptInterface(this), "Android");
    wv1.getSettings().setSupportZoom(true);
    wv1.setWebViewClient(new MyBrowser());
    wv1.loadUrl("file:///android_asset/a.html");
Phantômaxx
  • 37,901
  • 21
  • 84
  • 115
kamranbhatti585
  • 232
  • 2
  • 16

1 Answers1

3

Is it even possible? No, Not Possible.

Android assets file are kind of resource files, you cannot modify them once you build your apk file.

solution for your problem -

  1. make use of storage file system. (very bad approach since read,write,load operations will be there).

  2. use java script interface and try add styles dynamically to html elements.(ideal one)

    example : Changing element style attribute dynamically using JavaScript

Community
  • 1
  • 1
Sush
  • 3,864
  • 2
  • 17
  • 35
  • Solution 2 looks fine to me. Can you provide any short example from your precious time? – kamranbhatti585 Oct 29 '17 at 09:58
  • please check the update, lots of example are available, easy too. – Sush Oct 29 '17 at 10:01
  • 1
    The link you provided doesn't gave me what I wanted. I have implemented a WebViewJavaScriptInterface. Now, the question is using java, how would I able to modify my html code before webView is loaded? – kamranbhatti585 Oct 29 '17 at 10:10
  • The solution what i have given is upto point and it does have contents what need to solve the your problem. "Now, the question is using java, how would I able to modify my html code before it is loaded? " for this you should have clear understanding of usage of javascript interface in android, well this requires how html engine content rendering knowledge too. This link can help you if want handle changes before loading - https://www.w3schools.com/jsref/event_onload.asp – Sush Oct 29 '17 at 10:17
  • +1 and accepted your answer. I got your point. Can you provide any example over "javascript interface in android?" Which will explain this situation to me. – kamranbhatti585 Oct 29 '17 at 10:20
  • 1
    you have ample of support please google , try some sample. This is learning step, you should jump in. check out this very basic sample https://developer.android.com/guide/webapps/webview.html – Sush Oct 29 '17 at 10:24