top of page

Android App Dev: How to make a calculator

Hey guys! This is Amey Lokhande. Okay, lets begin making some apps which will be useful to us in our day-to-day life. In this post, I will be guiding you how to develop a Calculator Android App.

Here are the steps you need to do:

1. First of all, take two EditText boxes for taking the two numbers as input. You can do that by dragging the EditText boxes. ALWAYS REMEMBER THAT THERE IS AN ID ASSOCIATED WITH EVERYTHING YOU DO WHILE DESIGNING THE LAYOUT OF YOUR APP. Also you can change the text in it by changing the text properties.

2. Take three buttons for doing addition, subtraction and multiplication. That can be done by dragging the button widget. Text in the button can be changed by changing the text properties. HERE ALSO REMEMBER THE IDs ASSOCIATED WITH EACH BUTTON.

3. Now drag a simple TextView widget to display the result. Okay, so we have designed our Calculator. Finally the app will look like this:

Now lets see the java code in the MainActivity.java which will decide how the Calculator works.

You can open MainActivity.java from app->java->com.example.USERNAME.APPNAME. It can be found on the left side of your screen.

Already Android Studio has given us the code for running a basic app. But we need to edit it accordingly.

1. First of all, we need to create a new method named as OnButtonClick below the OnCreate method. Then create the instances for the EditTexts and TextView. The code for it is show below.

2. Then create new integer variables for storing the numbers and then their value after applying various operations to them. And then set the TextView variable 't' to operations variable.

3. Now you must be thinking what will happen when we click the button. Well, by the code so far we have written, nothing will happen if we dont set the OnClick Button Method inside that Button's properties.

4. Similarly write the code for minus and multiply button. Thats all. We have designed the code.

The entire java code will be something like this:

public class MainActivity extends AppCompatActivity {

@Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); } public void OnButtonClick1(View v){ EditText e1 = (EditText)findViewById(R.id.editText); EditText e2 = (EditText)findViewById(R.id.editText2); TextView t = (TextView)findViewById(R.id.textView2); int num1 = Integer.parseInt(e1.getText().toString()); int num2 = Integer.parseInt(e2.getText().toString()); int oper = num1+num2; t.setText(Integer.toString(oper)); } public void OnButtonClick2(View v){ EditText e1 = (EditText)findViewById(R.id.editText); EditText e2 = (EditText)findViewById(R.id.editText2); TextView t = (TextView)findViewById(R.id.textView2); int num1 = Integer.parseInt(e1.getText().toString()); int num2 = Integer.parseInt(e2.getText().toString()); int oper = num1-num2; t.setText(Integer.toString(oper)); } public void OnButtonClick3(View v){ EditText e1 = (EditText)findViewById(R.id.editText); EditText e2 = (EditText)findViewById(R.id.editText2); TextView t = (TextView)findViewById(R.id.textView2); int num1 = Integer.parseInt(e1.getText().toString()); int num2 = Integer.parseInt(e2.getText().toString()); int oper = num1*num2; t.setText(Integer.toString(oper)); } }

Also you need to import the following libraries from Android Studio to make the App work without any error.

import android.support.annotation.IntegerRes; import android.support.v7.app.AppCompatActivity; import android.os.Bundle; import android.view.View; import android.widget.Button; import android.widget.EditText; import android.widget.TextView;

Some screenshots of the working app:

Thats done!

Thanks!!!

bottom of page