Experiment No. 4 Aim: Theory:: Write A Mobile Application To Calculator
Experiment No. 4 Aim: Theory:: Write A Mobile Application To Calculator
Experiment No. 4
Theory: As with all applications, we should always start with building the interface. For the
calculator app, we require the following elements.
1.TextView
2.Buttons
That’s it. We use the textView to show users the numbers they have written and also to display
the result of the calculations we are going to do.
Each of the numbers will have a different button, from 0-9 and we will have four additional
buttons for addition, subtraction, multiplication and division. Other than this, we require a button
to calculate the operation, a button for decimals, and a button to clear the display.
Requirements
1. EditText: To display numbers and the result.
2. Button: Numbers 0-9
3. Button: Operational buttons (+, -, *, /, =)
4. Button: Decimal button
5. Button: Clear display button.
Here’s a screenshot of what the code at the bottom would look like, if you were to paste it in your
activity_main.xml file.
Button btn_1, btn_2, btn_3, btn_4, btn_5, btn_6, btn_7, btn_8, btn_9, btn_0, btn_Add, btn_Sub,
btn_Mul, btn_Div, btn_calc, btn_dec, btn_clear;
Step 2: Fetching values from the elements into the working of our app
Inside the onCreate function, we will fetch all values and assign it to our objects. We do this by
the following code
btn_0 = (Button) findViewById(R.id.btn_0);
Here, we are fetching the value of ‘btn_0’, which is the ID of our button zero, and storing it in the
object ‘btn_0’ that we created earlier. Though, they are named the same, Android Studio has the
ability to identify the correct one. But if it gets confusing for you, feel free to give either one of
them any other names.
27 | P a g e
Step 3: Changing the EditText value by pressing the buttons.
Here’s the code
btn_1.setOnClickListener(new View.OnClickListener()
{ @Override
public void onClick(View v)
{ ed1.setText(ed1.getText()
+"1");
}
});
This is what happens when you push one. The editText, first already fetches the value that it is
already displaying, null in this case, and then adds 1 to it. If we don’t fetch the existing value
then it will become impossible to enter multiple digit numbers.
Result:
Conclusion: In this experiment we learn make mobile application to calculator
29 | P a g e
Questionnaire
An arithmetic operation is specified by combining operands with one arithmetic operator. Arithmetic
operations can also be specified by the ADD, SUBTRACT, DIVIDE, and MULTIPLY built-in
functions.
5) Write code for Changing the EditText value by pressing the buttons.
1. Start your EditText, the ID is come to your xml id. EditText myText = (EditText)findViewById(R.
id. my_text_id);
2. String text = "here put the text that you want"
3. myText.setText(text); //variable from point 2.
30 | P a g e
8) Which function is use to fetch all the values?
1. You'll bring the data, we'll bring the app. Link your Google Sheets or
an Excel file via OneDrive, Dropbox, etc. ...
2. Now, you are behind the wheel. ...
3. Your prototype is ready… ...
4. Publish your app. ...
5. Share and manage your app. ...
6. Make your app even better.