2

There are few SO questions and other articles about calling Java class method from Javascript but all of them deals with java method with return type void.

Here is what I am trying to achieve: There are 2 strings to be displayed in WebView - say Yes and No. But they needs to be localized and hence I want to get the string value from Java method rather than using multiple JS for each locale.

Here's the code sample: Java class

onCreate(){
//Some code
contentWebView.addJavascriptInterface(new CalculatorJavaScriptCallInterface(), "calculatorjavascriptcallinterface");
//Some code
}

String localizedString = "";
private class CalculatorJavaScriptCallInterface {
    CalculatorJavaScriptCallInterface() {
    }

    @JavascriptInterface
    public String getLocalizedString(final int stringId) {
        localizedString = getResources().getString(stringId);
        Toast.makeText(getActivity(), "localizedString :: " + localizedString, Toast.LENGTH_SHORT).show();

        return localizedString;
    }
}

Javascript file

  function Checkboxpicker(element, options) {
    //Some code
    this.options = $.extend({}, $.fn.checkboxpicker.defaults, options, this.$element.data());
}
  $.fn.checkboxpicker.defaults = {
    //EXISTING STRINGS
    //offLabel: 'No',
    //onLabel: 'Yes',
    offLabel: window.calculatorjavascriptcallinterface.getLocalizedString("Consult.JSSupport.checkbox.selected"),
    onLabel: window.calculatorjavascriptcallinterface.getLocalizedString("Consult.JSSupport.checkbox.notSelected"),
  };

I am getting blank string as output when I run above code.

Here are some notes:

  1. This Javascript is being used appropriately as it works if I use hard-coded strings
  2. Respective stings have been defined in string.xml
  3. I tried using calculatorjavascriptcallinterface in Camel case and lower case both
  4. I tried with and without window. to call Java method
  5. Tried returning hard-coded value from Java method - IT IS WORKING THIS WAY

Any suggestions will be appreciated. Thanks in advance!


EDIT I'm getting following error even though the string is present in strings.xml:

No package identifier when getting value for resource number 0x00000000
android.content.res.Resources$NotFoundException: String resource ID #0x0
9
  • you may need to make asynchronous calls Commented May 25, 2018 at 6:35
  • have you setJavaScriptEnabled ? Is your getLocalizedString getting called? Commented May 25, 2018 at 6:41
  • @sagar - Yes. How to verify that? Please check important update in Question. Commented May 25, 2018 at 6:55
  • @brk - Can you please elaborate? Commented May 25, 2018 at 6:55
  • @GAMA is your Toast message showing correct String? Commented May 25, 2018 at 6:57

2 Answers 2

1

It looks like issue with getting the String with proper Id. change your getLocalizedString as follows:

@JavascriptInterface
public String getLocalizedString(final String stringId) {
    localizedString = getResources().getString(getResources().getIdentifier(stringId,"string",getContext().getPackageNam‌​e()));
    Toast.makeText(getActivity(), "localizedString :: " + localizedString, Toast.LENGTH_SHORT).show();

    return localizedString;
}
Sign up to request clarification or add additional context in comments.

2 Comments

Hi @Sagar - it worked like a charm. Minor change - need to use getContext().getPackageName() instead of getPackageName()
@GAMA great! Thanks for updating. I have updated the answer
1

1.)

 mWebView.getSettings().setJavaScriptEnabled(true);
    mWebView.getSettings().setDomStorageEnabled(true);
    mWebView.addJavascriptInterface(new WebViewInterface(this), "Android");
    mWebView.loadUrl("url or html file path");

2.)

public class WebViewInterface {

    Context mContext;

    WebViewInterface(Context mContext) {
        this.mContext = mContext;
    }

    @JavascriptInterface
    public void performAction(String pro_cat_id) {
          //write your code here to perform any action.
    }

3.)

 <html>
    <head>
        <script type="text/javascript" src="/js/MyJS.js"></script>
    </head>
    <body>
        <button onClick="mClick('1');">Yes</button>
        <button onClick="mClick('0');">No</button>
    </body>
</html>

4.) javascript: MyJS.js

function mClick(mValue)
{
    Android.performAction(mId);
}

1 Comment

Hi @Akash - I am almost doing the same thing as mentioned in question but no success.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.