Hacking on GraphHopper - a Java road routing engine. Peter has posted 62 posts at DZone. You can read more from them at their website. View Full User Profile

Shortest Code for a Simple Calculator on Android

12.13.2011
| 6355 views |
  • submit to reddit

 

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/

Published at DZone with permission of its author, Peter Karussell.

(Note: Opinions expressed in this article and its replies are the opinions of their respective authors and not those of DZone, Inc.)

Tags:

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

Comment viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.