Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

I wants to know how to use java/android object variable values from javascript. following is my scenario.

Below is My JavaScript interface class.

public class JavaScriptTestClass {

public String strText;
public String enabletest = "false";

@JavascriptInterface
public void setText(String str) {
 Log.v("mylog","JavaScriptTestClass setText() strText : " + str);
    strText = str;
}

@JavascriptInterface
public void dispInfo() {
     Log.v("mylog","JavaScriptTestClass dispInfo() strText : " + strText);
     Log.v("mylog","JavaScriptTestClass dispInfo() enabletest : " + enabletest);
}
}

below is my Activity

public class AndroidJSTestActivity extends Activity{
Button testBtn;
WebView myWebView;
String scriptSource = "";
boolean isScriptparsed = false;


/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

    testBtn = (Button)this.findViewById(R.id.login);      
    testBtn.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {
            StringBuilder buf = new StringBuilder("javascript:validateFormInputs()");
            myWebView.loadUrl(buf.toString());
        }
    });

    new ScriptReader().start();

    while(!isScriptparsed);

    myWebView = (WebView)this.findViewById(R.id.myWebView);
    myWebView.getSettings().setJavaScriptEnabled(true);
    myWebView.loadData(scriptSource, "text/html", "utf-8");
    myWebView.addJavascriptInterface(new JavaScriptTestClass(), "abc");        
}

class ScriptReader extends Thread {
    @Override
    public void run() {
        try {
            InputStreamReader isReader = new InputStreamReader(
                    AndroidJSTestActivity.this.getAssets().open("script.js"));
            int blah;

            blah = isReader.read();

            while (blah != -1) {
                scriptSource += (char) blah;
                blah = isReader.read();
            }
            isScriptparsed = true;
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
}

and below is content of script.js file

<script>function validateFormInputs(){console.log('Testing JavaScriptTestClass before setting data from JS  ');abc.dispInfo();abc.setText("data");abc.enabletest = "true";console.log('Testing JavaScriptTestClass after setting data from JS enabletest : ' + abc.enabletest);abc.dispInfo();}</script>

i don't wants to use setter and getter to with @JavascriptInterface to access variables. i wants to use like abc.enabletest. but if i use abc.enabletest to access the value before update statement like abc.enabletest = "true"; its showing as undefined, and after abc.enabletest = "true"; statement the value is updated in JS. But that change is limited to only that script content. That change is not reflecting in java. I.e when i call abc.dispInfo(); after updating the value in JavaScript its not printing the updated value in Java script.

I have seen same kind for statements in Titanium project coding with javascript file. but unable to find how they are managing. If any one knows about Titanium please help me.

Any help to update java variable from java script will be appreciated.

Thanks in Advance.

share|improve this question

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Browse other questions tagged or ask your own question.