Sample App - Calculator With JavaScript Library Rhino

Dependency : 

implementation 'com.faendir.rhino:rhino-android:1.5.2'


 Java Part : 

package first.learn.calculator;

import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;

import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;
import android.view.View;
import android.widget.TextView;

import com.google.android.material.button.MaterialButton;
import org.mozilla.javascript.Context;
import org.mozilla.javascript.Scriptable;


public class MainActivity extends AppCompatActivity implements View.OnClickListener {


TextView tvMath, tvResult;
MaterialButton btnDoubleZero, btnZero, btnDot, btnEqual, btnOne, btnTwo, btnThree, btnFour,
btnFive, btnSix, btnSeven, btnEight, btnNine, btnPlus, btnMinus, btnMultiply, btnDivide,
btnBracketOpen, btnBracketEnd, btnC;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

tvMath = (TextView) findViewById(R.id.tvMath);
tvResult = (TextView) findViewById(R.id.tvResult);

castingAndSettingClickListener(btnDoubleZero, R.id.btnDoubleZero);
castingAndSettingClickListener(btnZero, R.id.btnZero);
castingAndSettingClickListener(btnDot, R.id.btnDot);
castingAndSettingClickListener(btnEqual, R.id.btnEqual);
castingAndSettingClickListener(btnOne, R.id.btnOne);
castingAndSettingClickListener(btnTwo, R.id.btnTwo);
castingAndSettingClickListener(btnThree, R.id.btnThree);
castingAndSettingClickListener(btnFour, R.id.btnFour);
castingAndSettingClickListener(btnFive, R.id.btnFive);
castingAndSettingClickListener(btnSix, R.id.btnSix);
castingAndSettingClickListener(btnSeven, R.id.btnSeven);
castingAndSettingClickListener(btnEight, R.id.btnEight);
castingAndSettingClickListener(btnNine, R.id.btnNine);
castingAndSettingClickListener(btnPlus, R.id.btnPlus);
castingAndSettingClickListener(btnMinus, R.id.btnMinus);
castingAndSettingClickListener(btnMultiply, R.id.btnMultiply);
castingAndSettingClickListener(btnDivide, R.id.btnDivide);
castingAndSettingClickListener(btnBracketOpen, R.id.btnBracketOpen);
castingAndSettingClickListener(btnBracketEnd, R.id.btnBracketEnd);
castingAndSettingClickListener(btnC, R.id.btnC);




}


void castingAndSettingClickListener(MaterialButton bttn, int id){
bttn = findViewById(id);
bttn.setOnClickListener(this);
}


@Override
public void onClick(View view) {


MaterialButton clickedButton = (MaterialButton) view;

String buttonText = clickedButton.getText().toString();

String dataForCalculation = tvMath.getText().toString();

if (buttonText.equals("C")){
tvMath.setText("");
tvResult.setText("0");
return;
}else if (buttonText.equals("=")){
tvMath.setText(tvResult.getText().toString());
return;
}else{
dataForCalculation = dataForCalculation+buttonText;
}

tvMath.setText(dataForCalculation);

String finalResult = makeResult(dataForCalculation);

if (!finalResult.equals("Error")){
tvResult.setText(finalResult);
}


}



String makeResult(String data){
try {
Context newContext = Context.enter();
newContext.setOptimizationLevel(-1);
Scriptable newScriptable = newContext.initStandardObjects();
String finalResult = newContext.evaluateString(newScriptable, data, "Javascript", 1, null).toString();

if (finalResult.endsWith(".0")){
finalResult = finalResult.replace(".0", "");
}
return finalResult;
}catch (Exception e){
return "Error";
}
}


}


Xml Part : 


<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#F6B6B6"
android:gravity="bottom"
android:paddingBottom="15dp"
android:orientation="vertical"
tools:context=".MainActivity">


<TextView
android:layout_width="match_parent"
android:id="@+id/tvMath"
android:layout_marginBottom="20dp"
android:textSize="30dp"
android:textColor="@color/black"
android:paddingHorizontal="40dp"
android:gravity="right"
android:layout_height="wrap_content"/>
<TextView
android:layout_width="match_parent"
android:id="@+id/tvResult"
android:text="0"
android:layout_marginBottom="20dp"
android:textSize="60dp"
android:textColor="@color/black"
android:paddingHorizontal="40dp"
android:gravity="right"
android:layout_height="wrap_content"/>



<LinearLayout
android:layout_width="match_parent"
android:orientation="horizontal"
android:gravity="center_horizontal"
android:layout_height="wrap_content">

<com.google.android.material.button.MaterialButton
android:layout_width="70dp"
android:layout_height="70dp"
android:backgroundTint="#0022FF"
app:cornerRadius="35dp"
style="@style/Widget.MaterialComponents.ExtendedFloatingActionButton"
android:text="C"
android:id="@+id/btnC"
android:layout_margin="10dp"
android:textSize="30sp"
android:textColor="@color/white" />
<com.google.android.material.button.MaterialButton
android:layout_width="70dp"
android:layout_height="70dp"
android:backgroundTint="#0022FF"
app:cornerRadius="35dp"
style="@style/Widget.MaterialComponents.ExtendedFloatingActionButton"
android:text="("
android:id="@+id/btnBracketOpen"
android:layout_margin="10dp"
android:textSize="30sp"
android:textColor="@color/white" />
<com.google.android.material.button.MaterialButton
android:layout_width="70dp"
android:layout_height="70dp"
android:backgroundTint="#0022FF"
app:cornerRadius="35dp"
style="@style/Widget.MaterialComponents.ExtendedFloatingActionButton"
android:text=")"
android:id="@+id/btnBracketEnd"
android:layout_margin="10dp"
android:textSize="30sp"
android:textColor="@color/white" />
<com.google.android.material.button.MaterialButton
android:layout_width="70dp"
android:layout_height="70dp"
android:backgroundTint="#0022FF"
app:cornerRadius="35dp"
style="@style/Widget.MaterialComponents.ExtendedFloatingActionButton"
android:text="/"
android:id="@+id/btnDivide"
android:layout_margin="10dp"
android:textSize="30sp"
android:textColor="@color/white" />

</LinearLayout>


<LinearLayout
android:layout_width="match_parent"
android:orientation="horizontal"
android:gravity="center_horizontal"
android:layout_height="wrap_content">

<com.google.android.material.button.MaterialButton
android:layout_width="70dp"
android:layout_height="70dp"
android:backgroundTint="@color/white"
app:cornerRadius="35dp"
style="@style/Widget.MaterialComponents.ExtendedFloatingActionButton"
android:text="7"
android:id="@+id/btnSeven"
android:layout_margin="10dp"
android:textSize="30sp"
android:textColor="@color/black" />
<com.google.android.material.button.MaterialButton
android:layout_width="70dp"
android:layout_height="70dp"
android:backgroundTint="@color/white"
app:cornerRadius="35dp"
style="@style/Widget.MaterialComponents.ExtendedFloatingActionButton"
android:text="8"
android:id="@+id/btnEight"
android:layout_margin="10dp"
android:textSize="30sp"
android:textColor="@color/black" />
<com.google.android.material.button.MaterialButton
android:layout_width="70dp"
android:layout_height="70dp"
android:backgroundTint="@color/white"
app:cornerRadius="35dp"
style="@style/Widget.MaterialComponents.ExtendedFloatingActionButton"
android:text="9"
android:id="@+id/btnNine"
android:layout_margin="10dp"
android:textSize="30sp"
android:textColor="@color/black" />
<com.google.android.material.button.MaterialButton
android:layout_width="70dp"
android:layout_height="70dp"
android:backgroundTint="#0022FF"
app:cornerRadius="35dp"
style="@style/Widget.MaterialComponents.ExtendedFloatingActionButton"
android:text="*"
android:id="@+id/btnMultiply"
android:layout_margin="10dp"
android:textSize="30sp"
android:textColor="@color/white" />

</LinearLayout>


<LinearLayout
android:layout_width="match_parent"
android:orientation="horizontal"
android:gravity="center_horizontal"
android:layout_height="wrap_content">

<com.google.android.material.button.MaterialButton
android:layout_width="70dp"
android:layout_height="70dp"
android:backgroundTint="@color/white"
app:cornerRadius="35dp"
style="@style/Widget.MaterialComponents.ExtendedFloatingActionButton"
android:text="4"
android:id="@+id/btnFour"
android:layout_margin="10dp"
android:textSize="30sp"
android:textColor="@color/black" />
<com.google.android.material.button.MaterialButton
android:layout_width="70dp"
android:layout_height="70dp"
android:backgroundTint="@color/white"
app:cornerRadius="35dp"
style="@style/Widget.MaterialComponents.ExtendedFloatingActionButton"
android:text="5"
android:id="@+id/btnFive"
android:layout_margin="10dp"
android:textSize="30sp"
android:textColor="@color/black" />
<com.google.android.material.button.MaterialButton
android:layout_width="70dp"
android:layout_height="70dp"
android:backgroundTint="@color/white"
app:cornerRadius="35dp"
style="@style/Widget.MaterialComponents.ExtendedFloatingActionButton"
android:text="6"
android:id="@+id/btnSix"
android:layout_margin="10dp"
android:textSize="30sp"
android:textColor="@color/black" />
<com.google.android.material.button.MaterialButton
android:layout_width="70dp"
android:layout_height="70dp"
android:backgroundTint="#0022FF"
app:cornerRadius="35dp"
style="@style/Widget.MaterialComponents.ExtendedFloatingActionButton"
android:text="-"
android:id="@+id/btnMinus"
android:layout_margin="10dp"
android:textSize="30sp"
android:textColor="@color/white" />

</LinearLayout>

<LinearLayout
android:layout_width="match_parent"
android:orientation="horizontal"
android:gravity="center_horizontal"
android:layout_height="wrap_content">

<com.google.android.material.button.MaterialButton
android:layout_width="70dp"
android:layout_height="70dp"
android:backgroundTint="@color/white"
app:cornerRadius="35dp"
style="@style/Widget.MaterialComponents.ExtendedFloatingActionButton"
android:text="1"
android:id="@+id/btnOne"
android:layout_margin="10dp"
android:textSize="30sp"
android:textColor="@color/black" />
<com.google.android.material.button.MaterialButton
android:layout_width="70dp"
android:layout_height="70dp"
android:backgroundTint="@color/white"
app:cornerRadius="35dp"
style="@style/Widget.MaterialComponents.ExtendedFloatingActionButton"
android:text="2"
android:id="@+id/btnTwo"
android:layout_margin="10dp"
android:textSize="30sp"
android:textColor="@color/black" />
<com.google.android.material.button.MaterialButton
android:layout_width="70dp"
android:layout_height="70dp"
android:backgroundTint="@color/white"
app:cornerRadius="35dp"
style="@style/Widget.MaterialComponents.ExtendedFloatingActionButton"
android:text="3"
android:id="@+id/btnThree"
android:layout_margin="10dp"
android:textSize="30sp"
android:textColor="@color/black" />
<com.google.android.material.button.MaterialButton
android:layout_width="70dp"
android:layout_height="70dp"
android:backgroundTint="#0022FF"
app:cornerRadius="35dp"
style="@style/Widget.MaterialComponents.ExtendedFloatingActionButton"
android:text="+"
android:id="@+id/btnPlus"
android:layout_margin="10dp"
android:textSize="30sp"
android:textColor="@color/white" />

</LinearLayout>

<LinearLayout
android:layout_width="match_parent"
android:orientation="horizontal"
android:gravity="center_horizontal"
android:layout_height="wrap_content">

<com.google.android.material.button.MaterialButton
android:layout_width="70dp"
android:layout_height="70dp"
android:backgroundTint="@color/white"
app:cornerRadius="35dp"
style="@style/Widget.MaterialComponents.ExtendedFloatingActionButton"
android:text="00"
android:id="@+id/btnDoubleZero"
android:layout_margin="10dp"
android:textSize="22sp"
android:textColor="@color/black" />
<com.google.android.material.button.MaterialButton
android:layout_width="70dp"
android:layout_height="70dp"
android:backgroundTint="@color/white"
app:cornerRadius="35dp"
style="@style/Widget.MaterialComponents.ExtendedFloatingActionButton"
android:text="0"
android:id="@+id/btnZero"
android:layout_margin="10dp"
android:textSize="30sp"
android:textColor="@color/black" />
<com.google.android.material.button.MaterialButton
android:layout_width="70dp"
android:layout_height="70dp"
android:backgroundTint="@color/white"
app:cornerRadius="35dp"
style="@style/Widget.MaterialComponents.ExtendedFloatingActionButton"
android:text="."
android:id="@+id/btnDot"
android:layout_margin="10dp"
android:textSize="30sp"
android:textColor="@color/black" />
<com.google.android.material.button.MaterialButton
android:layout_width="70dp"
android:layout_height="70dp"
android:backgroundTint="#0022FF"
app:cornerRadius="35dp"
style="@style/Widget.MaterialComponents.ExtendedFloatingActionButton"
android:text="="
android:id="@+id/btnEqual"
android:layout_margin="10dp"
android:textSize="30sp"
android:textColor="@color/white" />

</LinearLayout>


</LinearLayout>



No comments:

Post a Comment

Featured Post

Lesson 1 : Android Studio : DownLoad And Install and First App

বিসমিল্লাহির রাহমানির রাহীম Lesson 1 : Android Studio : DownLoad And Install  and First App এই লেসনটির ভিডিও ইউটিউবে দেখতে এখানে ক্ল...

Popular Posts