Answered step by step
Verified Expert Solution
Question
1 Approved Answer
How to program a simple calculator using Android Studio? My code so far in XML: xml version= 1.0 encoding= utf-8 ?> < android.support.constraint.ConstraintLayout xmlns: android
How to program a simple calculator using Android Studio? My code so far in XML:
xml version="1.0" encoding="utf-8"?> <android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" tools:context="com.example.gangstatop.calculator.MainActivity" tools:layout_editor_absoluteY="90dp" tools:layout_editor_absoluteX="0dp"> <TextView android:id="@+id/outPut" android:layout_width="wrap_content" android:layout_height="wrap_content" android:gravity="end" android:text="TextView" android:textAlignment="viewEnd" tools:layout_constraintTop_creator="1" tools:layout_constraintRight_creator="1" android:layout_marginEnd="39dp" app:layout_constraintRight_toRightOf="parent" android:layout_marginTop="39dp" app:layout_constraintTop_toTopOf="parent" android:layout_marginRight="39dp" /> <LinearLayout android:layout_width="0dp" android:layout_height="wrap_content" android:layout_marginTop="255dp" android:orientation="vertical" app:layout_constraintLeft_toLeftOf="parent" app:layout_constraintRight_toRightOf="parent" app:layout_constraintTop_toTopOf="parent" tools:layout_constraintRight_creator="1" android:layout_marginStart="8dp" android:layout_marginEnd="8dp" tools:layout_constraintLeft_creator="1" android:layout_marginLeft="8dp" android:layout_marginRight="8dp"> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="horizontal" > <Button android:id="@+id/clear" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_weight="1" android:text="AC" /> <Button android:id="@+id/plusorminus" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_weight="1" android:text="+/-" /> <Button android:id="@+id/backspace" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_weight="1" android:text="<-" /> <Button android:id="@+id/divide" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_weight="1" android:text="" /> LinearLayout> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="horizontal"> <Button android:id="@+id/seven" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_weight="1" android:text="7" /> <Button android:id="@+id/eight" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_weight="1" android:text="8" /> <Button android:id="@+id/nine" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_weight="1" android:text="9" /> <Button android:id="@+id/multi" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_weight="1" android:text="*" /> LinearLayout> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="horizontal"> <Button android:id="@+id/four" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_weight="1" android:text="4" /> <Button android:id="@+id/five" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_weight="1" android:text="5" /> <Button android:id="@+id/six" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_weight="1" android:text="6" /> <Button android:id="@+id/minus" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_weight="1" android:text="-" /> LinearLayout> <LinearLayout android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="horizontal"> <Button android:id="@+id/one" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_weight="1" android:text="1" /> <Button android:id="@+id/two" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_weight="1" android:text="2" /> <Button android:id="@+id/three" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_weight="1" android:text="3" /> <Button android:id="@+id/plus" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_weight="1" android:text="+" /> LinearLayout> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="horizontal"> <Button android:id="@+id/zero" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_weight="1" android:text="0" /> <Button android:id="@+id/dot" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_weight="1" android:text="." /> <Button android:id="@+id/equal" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_weight="1" android:text="=" /> LinearLayout> LinearLayout> <TextView android:id="@+id/textView" android:layout_width="wrap_content" android:layout_height="wrap_content" android:gravity="end" android:text="TextView" android:textAlignment="viewEnd" tools:layout_constraintTop_creator="1" tools:layout_constraintRight_creator="1" android:layout_marginEnd="39dp" app:layout_constraintRight_toRightOf="parent" android:layout_marginTop="39dp" app:layout_constraintTop_toTopOf="parent" android:layout_marginRight="39dp" /> android.support.constraint.ConstraintLayout> Then my main activity code:
package com.example.gangstatop.calculator; import android.support.v7.app.AppCompatActivity; import android.os.Bundle; import android.view.View; import android.widget.TextView; import android.widget.Button; import org.w3c.dom.Text; public class MainActivity extends AppCompatActivity { private Button button0, button1, button2, button3, button4, button5, button6, button7, button8, button9; private Button buttonAdd, buttonSub, buttonMulitiply, buttonDivide, buttonBack, buttonClear, buttonSwitch, buttonPeriod, buttonEqual; private TextView results, finalResults; private double firstValue, secondValue; private char OPERATION; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); initialize(); results.setText("0"); finalResults.setText(""); pressButton(); } private void initialize() { //Number buttons button0 = (Button)findViewById(R.id.zero); button1 = (Button)findViewById(R.id.one); button2 = (Button)findViewById(R.id.two); button3 = (Button)findViewById(R.id.three); button4 = (Button)findViewById(R.id.four); button5 = (Button)findViewById(R.id.five); button6 = (Button)findViewById(R.id.six); button7 = (Button)findViewById(R.id.seven); button8 = (Button)findViewById(R.id.eight); button9 = (Button)findViewById(R.id.nine); //Function buttons buttonAdd = (Button)findViewById(R.id.plus); buttonSub = (Button)findViewById(R.id.minus); buttonMulitiply = (Button)findViewById(R.id.multi); buttonDivide = (Button)findViewById(R.id.divide); //Misc function buttons buttonSwitch = (Button)findViewById(R.id.plusorminus); buttonClear = (Button)findViewById(R.id.clear); buttonPeriod = (Button)findViewById(R.id.dot); buttonEqual = (Button)findViewById(R.id.equal); //Textview displaying results results = (TextView)findViewById(R.id.outPut); finalResults = (TextView)findViewById(R.id.textView); } public void pressButton () { /************************************************************ * Override the onClick function to display all numbers in * calculator. */ //results.setText(""); button0.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { results.setText(results.getText().toString() + "0"); }} ); button1.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { results.setText(results.getText().toString() + "1"); }} ); button2.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v){ results.setText(results.getText().toString() + "2"); }} ); button3.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { results.setText(results.getText().toString() + "3"); }} ); button4.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v){ results.setText(results.getText().toString() + "4"); }} ); button5.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { results.setText(results.getText().toString() + "5"); }} ); button6.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v){ results.setText(results.getText().toString() + "6"); }} ); button7.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { results.setText(results.getText().toString() + "7"); }} ); button8.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v){ results.setText(results.getText().toString() + "8"); }} ); button9.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v){ results.setText(results.getText().toString() + "9"); }} ); buttonAdd.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v){ operation(); OPERATION = '+'; finalResults.setText(String.valueOf(firstValue)); results.setText(null); }} ); /* buttonBack.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v){ if (results.length() > 0) { CharSequence digit = results.getText().toString(); results.setText(digit.subSequence(0, digit.length()-1)); } else { results.setText("0"); finalResults.setText("null"); } }} ); */ } public void operation () { firstValue = Double.parseDouble(results.getText().toString()); secondValue = Double.parseDouble(results.getText().toString()); switch (OPERATION) { case '+': firstValue = firstValue - secondValue; break; case '-': firstValue = firstValue - secondValue; break; case '*': firstValue = firstValue * secondValue; break; case '/': firstValue = firstValue / secondValue; case '=': break; } } } manifest file:
xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.example.gangstatop.calculator"> <application android:allowBackup="true" android:icon="@mipmap/ic_launcher" android:label="@string/app_name" android:roundIcon="@mipmap/ic_launcher_round" android:supportsRtl="true" android:theme="@style/AppTheme"> <activity android:name=".MainActivity"> <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> intent-filter> activity> application> manifest>
Test cases:
1. 5+2=7 2. 6-3=3 3. 4*8=32 4. 15/3=5 5. -1.5+6=4.5 6. 19 - 27.2 = -8.2 7. 55555 * 99999 = 5.555444e9 8. 1 / 10000000 = 1e-7
9. 56+34+14+5.5=109.5 10. 44 + (Clear) 13 + 45(backspace) = 17 (Should compute 13 + 4)
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started