You can do this using exp4j in android studio.
Download exp4j binary jar from the official site. Download
Import exp4j into android studio by copying the jar files in app/libs
folder.
Add the following line in your module build.gradle
file dependencies.
dependencies {
implementation fileTree(dir: 'libs', include: ['*.jar'])
...
}
Now you can try the following demo,
package com.example.expressionevaluator;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import de.congrace.exp4j.Calculable;
import de.congrace.exp4j.ExpressionBuilder;
import de.congrace.exp4j.UnknownFunctionException;
import de.congrace.exp4j.UnparsableExpressionException;
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Calculable calc = null;
try {
calc = new ExpressionBuilder("(200 + 100) / 2 + 300").build();
double result = calc.calculate();
Log.d("result", result);
} catch (UnknownFunctionException e) {
e.printStackTrace();
} catch (UnparsableExpressionException e) {
e.printStackTrace();
}
}
}
For more:
https://lallafa.objecthunter.net/exp4j/
https://github.com/codemaker2015/Expression-Evaluator