Hi guys, I am Prabakaran, Welcome to innovative codes academy, In this section we learn about
How to develop Mobile Calculator in Android Studio Tool?
Using Rhino Library Package:
Rhino Library Package:https://github.com/F43nd1r/rhino-android
Google Fonts:https://fonts.google.com/
You want source code contact me:
innovativecodesacacademy1@gmail.com
watch full video, Any doubt or quires leave comment with your mail, I will chat with you.
JAVA Source Code:
MainActivity.java
package com.example.calculator_01;
import androidx.appcompat.app.AppCompatActivity; import android.os.Bundle; import android.view.View; import android.widget.Button; import android.widget.TextView; import org.mozilla.javascript.Context; import org.mozilla.javascript.Scriptable; public class MainActivity extends AppCompatActivity { Button btn0,btn1,btn2,btn3,btn4,btn5,btn6,btn7,btn8,btn9,
btnBracket,btnClear,btnPercent,btnDivide,btnMultiplication,
btnMinus,btnAddition,btnEqual,btnDot;
TextView tvinput,tvoutput; String process; Boolean checkbracket=false; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); btn0 =findViewById(R.id.btn0); btn1 =findViewById(R.id.btn1); btn2 =findViewById(R.id.btn2); btn3 =findViewById(R.id.btn3); btn4 =findViewById(R.id.btn4); btn5 =findViewById(R.id.btn5); btn6 =findViewById(R.id.btn6); btn7 =findViewById(R.id.btn7); btn8 =findViewById(R.id.btn8); btn9 =findViewById(R.id.btn9); btnDivide =findViewById(R.id.btnDivide); btnAddition =findViewById(R.id.btnAddition); btnMinus =findViewById(R.id.btnSubtraction); btnMultiplication =findViewById(R.id.btnMultiply); btnPercent =findViewById(R.id.btnPercent); btnBracket =findViewById(R.id.btnBracket); btnClear =findViewById(R.id.btnclear); btnEqual =findViewById(R.id.btnEqual); btnDot =findViewById(R.id.btnDot); tvinput =findViewById(R.id.tvinput); tvoutput =findViewById(R.id.tvoutput); btnClear.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { tvinput.setText(""); tvoutput.setText(""); } }); btn0.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { process =tvinput.getText().toString(); tvinput.setText(process+"0"); } }); btn1.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { process =tvinput.getText().toString(); tvinput.setText(process+"1"); } }); btn2.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { process =tvinput.getText().toString(); tvinput.setText(process+"2"); } }); btn3.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { process =tvinput.getText().toString(); tvinput.setText(process+"3"); } }); btn4.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { process =tvinput.getText().toString(); tvinput.setText(process+"4"); } }); btn5.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { process =tvinput.getText().toString(); tvinput.setText(process+"5"); } }); btn6.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { process =tvinput.getText().toString(); tvinput.setText(process+"6"); } }); btn7.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { process =tvinput.getText().toString(); tvinput.setText(process+"7"); } }); btn8.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { process =tvinput.getText().toString(); tvinput.setText(process+"8"); } }); btn9.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { process =tvinput.getText().toString(); tvinput.setText(process+"9"); } }); btnMultiplication.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { process =tvinput.getText().toString(); tvinput.setText(process+"x"); } }); btnAddition.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { process =tvinput.getText().toString(); tvinput.setText(process+"+"); } }); btnMinus.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { process =tvinput.getText().toString(); tvinput.setText(process+"-"); } }); btnDivide.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { process =tvinput.getText().toString(); tvinput.setText(process+"÷"); } }); btnPercent.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { process =tvinput.getText().toString(); tvinput.setText(process+"%"); } }); btnDot.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { process =tvinput.getText().toString(); tvinput.setText(process+"."); } }); btnBracket.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { if(checkbracket) { process =tvinput.getText().toString(); tvinput.setText(process+")"); checkbracket=false; } else { process =tvinput.getText().toString(); tvinput.setText(process+"("); checkbracket=true; } } }); btnEqual.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { process =tvinput.getText().toString(); process =process.replaceAll("x","*"); process =process.replaceAll("%","/100"); process =process.replaceAll("÷","/"); Context rhino = Context.enter(); rhino.setOptimizationLevel(-1); String finalresult=""; try { Scriptable scriptable = rhino.initStandardObjects(); finalresult=rhino.evaluateString(scriptable,process,"javascript",1,null).toString(); } catch (Exception e) { finalresult="0"; } tvoutput.setText(finalresult); } }); } }
XML Source Code:Below XML code for Connect the two file input.xml and buttons.xml in activity_main.xmlactivity_main.xml <?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_height="match_parent" android:layout_width="match_parent" android:orientation="vertical" android:background="@color/white"> <include layout="@layout/input" android:layout_width="match_parent" android:layout_height="wrap_content" /> <RelativeLayout android:layout_width="match_parent" android:layout_height="wrap_content"> <include layout="@layout/buttons" android:layout_height="wrap_content" android:layout_width="match_parent" /> </RelativeLayout> </LinearLayout> Buttons Creations:buttons.xml <?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_height="match_parent" android:layout_width="match_parent"> <GridLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:columnCount="6" android:rowCount="5"> <Button android:id="@+id/btnclear" android:layout_height="100dp" android:layout_width="wrap_content" android:layout_row="0" android:layout_column="0" android:text="C" android:textSize="25sp" android:background="@color/white" android:textColor="@color/black" android:fontFamily="@font/got" /> <Button android:id="@+id/btnBracket" android:layout_height="100dp" android:layout_width="wrap_content" android:layout_row="0" android:layout_column="1" android:text="()" android:textSize="25sp" android:background="@color/white" android:textColor="@color/black" android:fontFamily="@font/got" /> <Button android:id="@+id/btnPercent" android:layout_height="100dp" android:layout_width="wrap_content" android:layout_row="0" android:layout_column="2" android:text="%" android:textSize="25sp" android:background="@color/white" android:textColor="@color/black" android:fontFamily="@font/got" /> <Button android:id="@+id/btnDivide" android:layout_height="100dp" android:layout_width="wrap_content" android:layout_row="0" android:layout_column="3" android:text="÷" android:textSize="25sp" android:background="@color/white" android:textColor="@color/blue" android:fontFamily="@font/got" /> <Button android:id="@+id/btn7" android:layout_height="100dp" android:layout_width="wrap_content" android:layout_row="1" android:layout_column="0" android:text="7" android:textSize="25sp" android:background="@color/white" android:textColor="@color/black" android:fontFamily="@font/got" /> <Button android:id="@+id/btn8" android:layout_height="100dp" android:layout_width="wrap_content" android:layout_row="1" android:layout_column="1" android:text="8" android:textSize="25sp" android:background="@color/white" android:textColor="@color/black" android:fontFamily="@font/got" /> <Button android:id="@+id/btn9" android:layout_width="wrap_content" android:layout_height="100dp" android:layout_row="1" android:layout_column="2" android:background="@color/white" android:fontFamily="@font/got" android:text="9" android:textColor="@color/black" android:textSize="25sp" /> <Button android:id="@+id/btnMultiply" android:layout_height="100dp" android:layout_width="wrap_content" android:layout_row="1" android:layout_column="3" android:text="x" android:textSize="25sp" android:background="@color/white" android:textColor="@color/blue" android:fontFamily="@font/got" /> <Button android:id="@+id/btn4" android:layout_height="100dp" android:layout_width="wrap_content" android:layout_row="2" android:layout_column="0" android:text="4" android:textSize="25sp" android:background="@color/white" android:textColor="@color/black" android:fontFamily="@font/got" /> <Button android:id="@+id/btn5" android:layout_height="100dp" android:layout_width="wrap_content" android:layout_row="2" android:layout_column="1" android:text="5" android:textSize="25sp" android:background="@color/white" android:textColor="@color/black" android:fontFamily="@font/got" /> <Button android:id="@+id/btn6" android:layout_height="100dp" android:layout_width="wrap_content" android:layout_row="2" android:layout_column="2" android:text="6" android:textSize="25sp" android:background="@color/white" android:textColor="@color/black" android:fontFamily="@font/got" /> <Button android:id="@+id/btnSubtraction" android:layout_height="100dp" android:layout_width="wrap_content" android:layout_row="2" android:layout_column="3" android:text="-" android:textSize="25sp" android:background="@color/white" android:textColor="@color/blue" android:fontFamily="@font/got" /> <Button android:id="@+id/btn1" android:layout_height="100dp" android:layout_width="wrap_content" android:layout_row="3" android:layout_column="0" android:text="1" android:textSize="25sp" android:background="@color/white" android:textColor="@color/black" android:fontFamily="@font/got" /> <Button android:id="@+id/btn2" android:layout_width="wrap_content" android:layout_height="100dp" android:layout_row="3" android:layout_column="1" android:background="@color/white" android:fontFamily="@font/got" android:text="2" android:textColor="@color/black" android:textSize="25sp" /> <Button android:id="@+id/btn3" android:layout_height="100dp" android:layout_width="wrap_content" android:layout_row="3" android:layout_column="2" android:text="3" android:textSize="25sp" android:background="@color/white" android:textColor="@color/black" android:fontFamily="@font/got" /> <Button android:id="@+id/btnAddition" android:layout_height="100dp" android:layout_width="wrap_content" android:layout_row="3" android:layout_column="3" android:text="+" android:textSize="25sp" android:background="@color/white" android:textColor="@color/blue" android:fontFamily="@font/got" /> <Button android:id="@+id/btn0" android:layout_height="100dp" android:layout_width="wrap_content" android:layout_row="4" android:layout_column="0" android:text="0" android:textSize="25sp" android:background="@color/white" android:textColor="@color/black" android:fontFamily="@font/got" /> <Button android:id="@+id/btnDot" android:layout_height="100dp" android:layout_width="wrap_content" android:layout_row="4" android:layout_column="1" android:layout_columnSpan="2" android:layout_columnWeight="1" android:text="." android:textSize="25sp" android:background="@color/white" android:textColor="@color/black" android:fontFamily="@font/got" /> <Button android:id="@+id/btnEqual" android:layout_height="100dp" android:layout_width="wrap_content" android:layout_row="4" android:layout_column="3" android:text="=" android:textSize="25sp" android:background="@color/blue" android:textColor="@color/black" android:fontFamily="@font/got" /> </GridLayout> </RelativeLayout> Input and Output Text Box Creation: <?xml version="1.0" encoding="utf-8"?> <LinearLayout android:layout_height="match_parent" android:layout_width="match_parent" android:orientation="vertical" android:background="@color/white" xmlns:android="http://schemas.android.com/apk/res/android"> <TextView android:layout_width="match_parent" android:layout_height="wrap_content" android:id="@+id/tvinput" android:text="" android:textSize="30dp" android:textColor="@color/black" android:padding="20dp" android:gravity="end" android:fontFamily="@font/got" /> <TextView android:layout_width="match_parent" android:layout_height="wrap_content" android:id="@+id/tvoutput" android:text="" android:textSize="30dp" android:textColor="@color/black" android:padding="20dp" android:gravity="end" /> </LinearLayout>AndroidManifest(File):<?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.example.calculator_01"> <application android:allowBackup="true" android:icon="@mipmap/ic_launcher" android:label="@string/app_name" android:roundIcon="@mipmap/ic_launcher_round" android:supportsRtl="true" android:theme="@style/AppTheme"> <activity android:name=".MainActivity"> <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </activity> </application> </manifest> Click on below link: https://youtu.be/rJhomr8b9c4 Android Studio Tool Download link given below: https://developer.android.com/studio Channel link: https://www.youtube.com/channel/UCX1KgUr3ZA6cCR81qGGpA6g?sub_confirmation=1
Good keep it up
Thank You