How to Create Simple Calculator application in android studio
Hello android developer. today I will tell you how to make a simple calculator app using android studio. By using my simple calculator source code, you can create a simple calculator app, simply copy the code and paste it in android studio and you can easily create a simple calculator app. Source code is provided below you can copy it and use it or you can use your creativity to add some extra features to it.
XML (activity_main.xml)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 |
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" android:layout_width="match_parent" android:layout_height="match_parent" xmlns:tools="http://schemas.android.com/tools" tools:context=".Basic_calculator"> <EditText android:id="@+id/textinput1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentEnd="true" android:layout_alignParentLeft="true" android:layout_alignParentRight="true" android:layout_alignParentStart="true" android:layout_alignParentTop="true" android:ems="10" android:inputType="numberSigned" app:layout_constraintBottom_toTopOf="@+id/textinput2" app:layout_constraintEnd_toEndOf="parent" app:layout_constraintStart_toStartOf="parent" app:layout_constraintTop_toTopOf="parent" /> <EditText android:id="@+id/textinput2" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentEnd="true" android:layout_alignParentLeft="true" android:layout_alignParentRight="true" android:layout_alignParentStart="true" android:layout_below="@+id/textinput1" android:ems="10" android:inputType="numberSigned" app:layout_constraintBottom_toTopOf="@+id/output" app:layout_constraintEnd_toEndOf="parent" app:layout_constraintStart_toStartOf="parent" app:layout_constraintTop_toBottomOf="@+id/textinput1" /> <TextView android:id="@+id/output" android:layout_width="210dp" android:layout_height="24dp" android:layout_alignParentEnd="true" android:layout_alignParentLeft="true" android:layout_alignParentRight="true" android:layout_alignParentStart="true" android:layout_below="@+id/textinput2" android:layout_marginBottom="8dp" android:text="" android:textColor="#000000" app:layout_constraintBottom_toTopOf="@+id/buttonSub" app:layout_constraintEnd_toEndOf="parent" app:layout_constraintStart_toStartOf="parent" app:layout_constraintTop_toBottomOf="@+id/textinput2" /> <Button android:id="@+id/buttonAdd" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentEnd="true" android:layout_alignParentLeft="true" android:layout_alignParentRight="true" android:layout_alignParentStart="true" android:layout_below="@+id/output" android:onClick="doAdd" android:text="Add" android:textStyle="bold" app:layout_constraintBottom_toTopOf="@+id/buttonDivide" app:layout_constraintEnd_toStartOf="@+id/buttonSub" app:layout_constraintStart_toStartOf="parent" app:layout_constraintTop_toBottomOf="@+id/output" /> <Button android:id="@+id/buttonSub" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentEnd="true" android:layout_alignParentLeft="true" android:layout_alignParentRight="true" android:layout_alignParentStart="true" android:layout_below="@+id/buttonAdd" android:onClick="doSub" android:text="Subtract" android:textStyle="bold" app:layout_constraintBottom_toTopOf="@+id/buttonMulty" app:layout_constraintEnd_toEndOf="parent" app:layout_constraintStart_toEndOf="@+id/buttonAdd" app:layout_constraintTop_toBottomOf="@+id/output" /> <Button android:id="@+id/buttonMulty" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentEnd="true" android:layout_alignParentLeft="true" android:layout_alignParentRight="true" android:layout_alignParentStart="true" android:layout_below="@+id/buttonSub" android:onClick="doMulty" android:text="Multiply" android:textStyle="bold" app:layout_constraintBottom_toBottomOf="parent" app:layout_constraintEnd_toEndOf="parent" app:layout_constraintStart_toEndOf="@+id/buttonDivide" app:layout_constraintTop_toBottomOf="@+id/buttonSub" /> <Button android:id="@+id/buttonDivide" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentEnd="true" android:layout_alignParentLeft="true" android:layout_alignParentRight="true" android:layout_alignParentStart="true" android:layout_below="@+id/buttonMulty" android:onClick="doDivide" android:text="Divide" android:textStyle="bold" app:layout_constraintBottom_toBottomOf="parent" app:layout_constraintEnd_toStartOf="@+id/buttonMulty" app:layout_constraintStart_toStartOf="parent" app:layout_constraintTop_toBottomOf="@+id/buttonAdd" /> </android.support.constraint.ConstraintLayout> |
JAVA (MainActivity.java)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 |
import android.support.v7.app.AppCompatActivity; import android.os.Bundle; import android.view.View; import android.widget.EditText; import android.widget.TextView; public class Basic_calculator extends AppCompatActivity { EditText text1,text2; TextView output; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_basic_calculator); text1=(EditText)findViewById(R.id.textinput1); text2=(EditText)findViewById(R.id.textinput2); output=(TextView) findViewById(R.id.output); } public void doAdd(View v) { try { int a=Integer.parseInt("" + text1.getText()); int b=Integer.parseInt("" + text2.getText()); int sum=a+b; output.setText("" + sum); } catch (Exception ex) { System.out.println(ex); output.setText(ex.getMessage()); } } public void doSub(View v) { try { int a=Integer.parseInt("" + text1.getText()); int b=Integer.parseInt("" + text2.getText()); int sub=a-b; output.setText("" + sub); } catch (Exception ex) { System.out.println(ex); output.setText(ex.getMessage()); } } public void doMulty(View v) { try { int a=Integer.parseInt("" + text1.getText()); int b=Integer.parseInt("" + text2.getText()); int mul=a*b; output.setText("" + mul); } catch (Exception ex) { System.out.println(ex); output.setText(ex.getMessage()); } } public void doDivide(View v) { try { int a=Integer.parseInt("" + text1.getText()); int b=Integer.parseInt("" + text2.getText()); int div=a/b; output.setText("" + div); } catch (Exception ex) { System.out.println(ex); output.setText(ex.getMessage()); } } } |