View Course Path

Performing Arithmetic Operations in Android Studio

What will you learn?

  1. Diving into the Java part of Android App development
  2. Processing data from the text field in the app
  3. Converting one data type to another
  4. Using toast to display some output

Building the interface

Building the interface is the first part of any app building process. In more complex applications, the design is first created on paper before the actual interface building on Android Studio. But since we are building a fairly simple application, with just one activity and a few lines of code, we don’t need to design anything on paper.

The components we will need to build the interface are:

  • Text > Plain Text
  • Widgets > Button
  • Widgets > TextView (optional)

For our ease, we will use two linear layouts. In one layout, we will convert the weight from Kilograms to Pounds, and in the second layout, we’ll convert from Pounds to Kilograms.

The interface we built is shown below with the code to create it underneath.

arithmetic operations in android - weight converter android tutorial

Coding the functionality

In your MainActivity.java file, which you can find under app > javapackageName > MainActivity, you’ll see the code that’s behind the main activity.

We’ll write our code inside the “public class MainActivity extends AppCompatActity{ //your code here }“.

You will find the function onCreate( ) here, which is always called when your app is created. We will create two new functions that we are calling through the buttons we defined in the interface earlier. Through the properties section of the button we assigned:

  • the ‘btnKg‘ the onClick function ‘cnvPounds‘.
  • the ‘btnPound‘ the onClick function ‘cnvKgs

Keeping this in mind, we can define a new function inside the class MainActivity.

What are we doing inside this UserDefined function?

Get data from the text field:

Firstly we need to get the data we entered in the Plain Text field. We do this by first defining a variable of type EditText.

Next, we have to get the value the user will enter. We do this by using the findViewById() function.

The code looks like this:

EditText editTextVariable = (EditText) findViewById (R.id.textFieldName);

Now the variable editTextVariable will have the value entered by the user in the text field with the id textFieldName.

Convert this data into a String data type:

We first define a String variable. This variable will hold the value from editTextVariable in the form of a string. We do this by using two functions, getText() and toString().

The code looks like this:

String stringVariable = editTextVariable.getText().toString();

Now, stringVariable holds the data in String form. But since we are dealing with numbers, we have to use the Double data type to hold this value.

Convert String data type into Double data type:

Just like with the conversion to a string, we first define a variable of a double datatype. Then we parse the value from the string. The syntax for this looks like this:

Double doubleVariable = Double.parseDouble(stringVariable);

We have the value from the input field stored in our Double variable. It’s also in the data type that we need, i.e., Double. So we can now perform operations on it.

The optimal code:

We don’t need to define the string variable because we can get similar results from the following code too:

EditText editTextVariable = (EditText) findViewById (R.id.textFieldName);
Double doubleVariable = Double.parseDouble(editTextVariable.getText().toString());

Both the methods are correct so any one can be used.

Arithmetic operation:

Quite similar to how we use arithmetic operators in C, we can perform arithmetic operations on our variable using our standard syntax ( +, -, *, /, %). 1 kg is approximately equal to 2.20462 lbs. So we just multiply our variable with 2.20462 and store the new value in another variable. Alternatively, we can also update our original doubleVariable because we don’t need to use it ahead in our app. The code looks like this:

Double newVariable = doubleVariable * 2.20462;

or

doubleVariable = doubleVariable * 2.20462;

Displaying the output in a toast:

Toast is the easiest way to display information in an Android app. What is toast? It’s the message the system displays when your phone connects to a WiFi network. For our weight converter app, we will display the output in a toast. We use the following code.

Toast.makeText(Context , Text , Duration).show();

Here we have to mention three parameters.

Context: You have to mention the context in which you are making the toast. If you are in the inner class, you can use the function getApplicationContext().

Text: In this parameter, you should enter the data to be displayed in the toast. We can display anything inside “, “and can even concatenate it with our doubleVariable.

Duration: This parameter lets us describe the duration for which the toast will be displayed. We do this by either Toast.LENGTH_LONG or Toast.LENGTH_SHORT. 

With this, we have completed our onClick function. We can go through the same steps to convert the weight from pounds to kilograms.

Given below is the complete code to our app.

Related courses for this will be up soon!

2 thoughts on “Performing Arithmetic Operations in Android Studio

  1. Hi Asem!

    Thank you so much for sharing this project, i’ts helping me a lot with some work im doing.
    I have a question,
    i got multiple textInputs and i need to turn all of them into usable variables in order to make the calculation when a hit a single button.

    how should i structure the code to make this?

    1. I’m glad you are finding this code helpful!

      If there’s only one button in your app, and you are taking input from users, you can take as many inputs you want and define them in the main function of the button. Line number 12 in the code above is the function for converting to pounds.

      Similarly, you can create a function and define all of the inputs in that function. They will all be available to use within that function.

      I hope that helps!

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.