Shortest Code for a Simple Calculator on Android
String RESULT;
String input = "(1+3)/4 * 2 - 7";
...
webSettings.setJavaScriptEnabled(true);
...
webView.addJavascriptInterface(new JavaScriptInterface() {
public void returnResult(String o) {
RESULT = o;
}}, "JavaCallback"));
webView.loadUrl("javascript:window.JavaCallback"
+ ".returnResult("+input+")");
// now RESULT is -5
Is there a shorter one? BTW: this is only a sketch not sure if I've missed a bracket somewhere …
From http://karussell.wordpress.com/2011/12/09/shortest-code-for-a-simple-calculator-on-android/
(Note: Opinions expressed in this article and its replies are the opinions of their respective authors and not those of DZone, Inc.)





Comments
John David replied on Thu, 2012/01/26 - 3:08am
So what about if I need a complete calculator on Android?
It looks like your android calculator code can just perform the basic calculations and it has not provisions for other operations.
Please share if you found a complete code for an android calculator.
Java Eclipse