Android program to create a Calculator
In this android program, we will be creating a calculator.
For the design of the calculator, we will be using a linear layout.
For the functionality, we will be using four boolean variables add, sub, div and mul. We will set them to true if we click on any of these buttons on the calculator. The rest of the logic is simple, which is basically use of formula for each operation.
Steps to create the application:-
- Open Android Studio and create a new Android application and name it as “UnitConverter” and company domain as codedost so your package will be automatically set.
- Open an Empty Activity and name it as MainActivity.
- Copy the contents of res/layout/activity_main.xml file.
- Run the application to launch Android emulator or you can run it on your mobile also(which is way faster).
XML File(res/layout/activity_main.xml):
<?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:id="@+id/activity_main" android:layout_width="match_parent" android:layout_height="match_parent" tools:context="codedost.calculator.MainActivity"> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="vertical" android:id="@+id/layout_text1"> <EditText android:layout_width="match_parent" android:layout_height="150dp" android:id="@+id/edittext" android:textSize="40sp" /> </LinearLayout> <LinearLayout android:layout_width="match_parent" android:layout_height="match_parent" android:layout_below="@+id/layout_text1" android:orientation="vertical" android:id="@+id/text_layout2" android:weightSum="15"> <LinearLayout android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="horizontal" android:layout_weight="3" android:weightSum="12"> <Button android:layout_width="match_parent" android:layout_height="match_parent" android:layout_weight="3" android:text="7" android:textSize="20sp" android:id="@+id/n7"/> <Button android:layout_width="match_parent" android:layout_height="match_parent" android:layout_weight="3" android:text="8" android:textSize="20sp" android:id="@+id/n8"/> <Button android:layout_width="match_parent" android:layout_height="match_parent" android:layout_weight="3" android:text="9" android:textSize="20sp" android:id="@+id/n9"/> <Button android:layout_width="match_parent" android:layout_height="match_parent" android:layout_weight="3" android:text="+" android:textSize="20sp" android:id="@+id/nadd"/> </LinearLayout> <LinearLayout android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="horizontal" android:layout_weight="3" android:weightSum="12"> <Button android:layout_width="match_parent" android:layout_height="match_parent" android:layout_weight="3" android:text="4" android:textSize="20sp" android:id="@+id/n4"/> <Button android:layout_width="match_parent" android:layout_height="match_parent" android:layout_weight="3" android:text="5" android:textSize="20sp" android:id="@+id/n5"/> <Button android:layout_width="match_parent" android:layout_height="match_parent" android:layout_weight="3" android:text="6" android:textSize="20sp" android:id="@+id/n6"/> <Button android:layout_width="match_parent" android:layout_height="match_parent" android:layout_weight="3" android:text="-" android:textSize="20sp" android:id="@+id/nsub"/> </LinearLayout> <LinearLayout android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="horizontal" android:layout_weight="3" android:weightSum="12"> <Button android:layout_width="match_parent" android:layout_height="match_parent" android:layout_weight="3" android:text="1" android:textSize="20sp" android:id="@+id/n1"/> <Button android:layout_width="match_parent" android:layout_height="match_parent" android:layout_weight="3" android:text="2" android:textSize="20sp" android:id="@+id/n2"/> <Button android:layout_width="match_parent" android:layout_height="match_parent" android:layout_weight="3" android:text="3" android:textSize="20sp" android:id="@+id/n3"/> <Button android:layout_width="match_parent" android:layout_height="match_parent" android:layout_weight="3" android:text="/" android:textSize="20sp" android:id="@+id/ndiv"/> </LinearLayout> <LinearLayout android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="horizontal" android:layout_weight="3" android:weightSum="12"> <Button android:layout_width="match_parent" android:layout_height="match_parent" android:layout_weight="3" android:text="." android:textSize="20sp" android:id="@+id/ndec"/> <Button android:layout_width="match_parent" android:layout_height="match_parent" android:layout_weight="3" android:text="0" android:textSize="20sp" android:id="@+id/n0"/> <Button android:layout_width="match_parent" android:layout_height="match_parent" android:layout_weight="3" android:text="=" android:textSize="20sp" android:id="@+id/nequal"/> <Button android:layout_width="match_parent" android:layout_height="match_parent" android:layout_weight="3" android:text="X" android:textSize="20sp" android:id="@+id/nmul"/> </LinearLayout> <LinearLayout android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="horizontal" android:layout_weight="3" android:weightSum="12"> <Button android:layout_width="match_parent" android:layout_height="match_parent" android:text="AC" android:layout_weight="3" android:textSize="24sp" android:id="@+id/clr"/> <Button android:layout_width="match_parent" android:layout_height="match_parent" android:text="Sin()" android:layout_weight="3" android:textSize="24sp" android:id="@+id/Sin"/> <Button android:layout_width="match_parent" android:layout_height="match_parent" android:text="Cos()" android:layout_weight="3" android:textSize="24sp" android:id="@+id/Cos"/> <Button android:layout_width="match_parent" android:layout_height="match_parent" android:text="sqrt" android:layout_weight="3" android:textSize="24sp" android:id="@+id/sqrt"/> </LinearLayout> </LinearLayout> </RelativeLayout>
MainActivity.java
package codedost.calculator; import android.support.v7.app.AppCompatActivity; import android.os.Bundle; import android.text.Editable; import android.view.View; import android.widget.Button; import android.widget.EditText; import android.widget.TextView; import android.widget.Toast; public class MainActivity extends AppCompatActivity { Button mbutton0,mbutton1,mbutton2,mbutton3,mbutton4,mbutton5,mbutton6,mbutton7,mbutton8,mbutton9,mbuttonadd,mbuttonsub,mbuttondiv,mbuttonmul,mbuttondec,mbuttonequal,mbuttonclr,mbuttonsqrt,mbuttonsin,mbuttoncos; EditText et; Double a,b; boolean add,sub,mul,div; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); et = (EditText) findViewById(R.id.edittext); mbutton0 = (Button) findViewById(R.id.n0); mbutton1 = (Button) findViewById(R.id.n1); mbutton2 = (Button) findViewById(R.id.n2); mbutton3 = (Button) findViewById(R.id.n3); mbutton4 = (Button) findViewById(R.id.n4); mbutton5 = (Button) findViewById(R.id.n5); mbutton6 = (Button) findViewById(R.id.n6); mbutton7 = (Button) findViewById(R.id.n7); mbutton8 = (Button) findViewById(R.id.n8); mbutton9 = (Button) findViewById(R.id.n9); mbuttonadd = (Button) findViewById(R.id.nadd); mbuttonsub = (Button) findViewById(R.id.nsub); mbuttondiv = (Button) findViewById(R.id.ndiv); mbuttonmul = (Button) findViewById(R.id.nmul); mbuttondec = (Button) findViewById(R.id.ndec); mbuttonequal = (Button) findViewById(R.id.nequal); mbuttonclr = (Button) findViewById(R.id.clr); mbuttonsin = (Button) findViewById(R.id.Sin); mbuttoncos = (Button) findViewById(R.id.Cos); mbuttonsqrt = (Button) findViewById(R.id.sqrt); mbutton0.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { et.setText(et.getText()+"0"); } }); mbutton1.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { et.setText(et.getText()+"1"); } }); mbutton2.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { et.setText(et.getText()+"2"); } }); mbutton3.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { et.setText(et.getText()+"3"); } }); mbutton4.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { et.setText(et.getText()+"4"); } }); mbutton5.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { et.setText(et.getText()+"5"); } }); mbutton6.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { et.setText(et.getText()+"6"); } }); mbutton7.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { et.setText(et.getText()+"7"); } }); mbutton8.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { et.setText(et.getText()+"8"); } }); mbutton9.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { et.setText(et.getText()+"9"); } }); mbuttondec.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { et.setText(et.getText()+"."); } }); mbuttonadd.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { a=Double.parseDouble(et.getText()+""); add=true; et.setText(null); } }); mbuttonsub.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { a=Double.parseDouble(et.getText()+""); sub=true; et.setText(null); } }); mbuttonmul.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { a=Double.parseDouble(et.getText()+""); mul=true; et.setText(null); } }); mbuttondiv.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { a=Double.parseDouble(et.getText()+""); div=true; et.setText(null); } }); mbuttonclr.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { et.setText(null); } }); mbuttonsin.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { a=Double.parseDouble(et.getText()+""); Double x=Math.sin(Math.toRadians(a)); et.setText(String.valueOf(x)); } }); mbuttoncos.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { a=Double.parseDouble(et.getText()+""); Double x=Math.cos(Math.toRadians(a)); et.setText(String.valueOf(x)); } }); mbuttoncos.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { a=Double.parseDouble(et.getText()+""); Double x=Math.cos(Math.toRadians(a)); et.setText(String.valueOf(x)); } }); mbuttonsqrt.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { a=Double.parseDouble(et.getText()+""); Double x=Math.sqrt(a); et.setText(String.valueOf(x)); } }); mbuttonequal.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { b=Double.parseDouble(et.getText()+""); if(add==true) { et.setText(""+(a+b)); add=false; } if(sub==true) { et.setText((a-b)+""); sub=false; } if(mul==true) { et.setText((a*b)+""); mul=false; } if(div==true) { if(b==0) { et.setText("Infinite"); } else { et.setText((a / b) + ""); } div=false; } } }); } }
Output