0

I am trying to get an html table to work properly in android with an onclick event for certain rows and cells.

It's supposed to get user input from 2 rounds, then add them up and then keep adding up after each round till the 7th round. This is my code so far and everything runs fine on the PC but when I go to do it through my tablet ver 4.2.2 I get no results from the inputs.

Sample Code:

Javascript / HTML Code:

<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en">
<head>
<script language="JavaScript" type="text/javascript">
    /*<![CDATA[*/
    function calcP1()
    {
        var p1ro1 = document.getElementById("p1r1").value;
        var p1ro2 = document.getElementById("p1r2").value;

        var pl1 = document.getElementById("p1").value;

        var sum1 = (parseInt(p1ro1) + parseInt(p1ro2));

        document.getElementById("sp1r2").value = sum1;
    }
    /*]]>*/
</script>
</head>
<body>
<table>
    <tr>
        <td>Player Names:</td>
        <td><input type="text" id="p1"/></td>
    </tr>
    <tr>
        <td>Round 1: 2 Books</td>
        <td><input type="text" id="p1r1"/></td>
    </tr>
    <tr>
        <td>Round 2: 1 Book 1 Run</td>
        <td><input type="text" id="p1r2"onclick="calcP1();"/></td>
    </tr>
    <tr>
        <td>Total Round 1 & 2 </td>
        <td bgcolor="#00FF00"><input type="text" id="sp1r2" readonly /></td>
    </tr>
</table>
</body>
</html>

TableActivity:

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.table);

    wv = (WebView)findViewById(R.id.tables);
    wv.loadUrl("file:///android_asset/ScoreCard.html");
}

Would I have to use addJavascriptInterface into my TableActivity so how? Thank you.

  • Try not using onclick and use ontouch events: http://stackoverflow.com/questions/9122679/difference-between-ontouch-and-onclick-android For example – silver May 26 '14 at 04:44
  • Can you put the code where you call the onclick event? – luisZavaleta May 26 '14 at 04:52
  • I call the onclick in the at the Round 2: 1 Book 1 Run table row. – user3675043 May 26 '14 at 04:55
  • What role is android supposed to be playing in this? Is something in the Android app supposed to respond to this onclick? This just looks like a standard javascript/html relationship. Is your javascript embedded in the HTML or is it a separate js file referenced by your HTML? – a2345sooted May 26 '14 at 05:13
  • The Android app just loads the html file. The javascript is embedded into the head of the HTML. I will change that in the original post to make that clear. – user3675043 May 26 '14 at 05:16
  • I mean, what are you trying to change in the app based on the click in the HTML? – a2345sooted May 26 '14 at 05:18
  • What supposed to happen is that once the table is loaded. It gets user input from Round 1 and Round 2 then adds them after leaving the Round 2 cell for that player and display the addition into the cell called "Total Round 1 & 2, but it does nothing. – user3675043 May 26 '14 at 05:25
  • check my answer below and tell me if that works,.. if I get near my other computer soon, I'll check it myself – a2345sooted May 26 '14 at 05:27

1 Answers1

0

try adding these lines to your activity after you get a reference to the webview but before you load the url

wv.setWebChromeClient(new WebChromeClient());
wv.getSettings().setJavaScriptEnabled(true);

adding a javascriptinterface will allow you to call java methods within the application from your javascript and vice versa

a2345sooted
  • 569
  • 5
  • 20
  • Thank you so much, this works! Selected as answer. Thanks again and have a great day/night! – user3675043 May 26 '14 at 05:30
  • no problem, if you get to a point that you need help using a javascriptinterface to communicate back and forth between the HTML/Javascript and Android code, just let me know :) – a2345sooted May 26 '14 at 05:33