Question

In: Computer Science

On Android Studio, create a 4 function calculator. The only buttons you need are 0-9, *,...

On Android Studio, create a 4 function calculator. The only buttons you need are 0-9, *, /, +, -, a clear, and enter button. Having this java code, I need the XML code that organizes the layout of the calculator.

JAVA CODE -

package com.example.10012698.calculatorproject;

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

import java.util.ArrayList;
import java.util.StringTokenizer;
public class MainActivity extends AppCompatActivity implements View.OnClickListener {

Button button0, button1, button2, button3, button4, button5, button6,
button7, button8, button9, buttonadd, buttonsubtract, buttonmultiply,
buttondivide, buttonequals, buttonclear;
TextView display;
String displaytext="";
double result;
double x, y;
ArrayList<String> list;


@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

button0 = (Button) findViewById(R.id.button0);
button1 = (Button) findViewById(R.id.button1);
button2 = (Button) findViewById(R.id.button2);
button3 = (Button) findViewById(R.id.button3);
button4 = (Button) findViewById(R.id.button4);
button5 = (Button) findViewById(R.id.button5);
button6 = (Button) findViewById(R.id.button6);
button7 = (Button) findViewById(R.id.button7);
button8 = (Button) findViewById(R.id.button8);
button9 = (Button) findViewById(R.id.button9);
buttonadd = (Button) findViewById(R.id.buttonadd);
buttonsubtract = (Button) findViewById(R.id.buttonsubtract);
buttonmultiply = (Button) findViewById(R.id.buttonmultiply);
buttondivide = (Button) findViewById(R.id.buttondivide);
buttonclear = (Button) findViewById(R.id.buttonclear);
buttonequals = (Button) findViewById(R.id.buttonequals);
display = (TextView) findViewById(R.id.display);
display.setOnClickListener(this);
list = new ArrayList<>();
}

public void onClick(View view)
{
if(!(view.equals(buttonclear)&&!(view.equals(buttonequals))))
{
display.setText(display.getText()+""+((Button)view).getText());

if(displaytext.equals("0"))
{
display.setText(" ");
}

if(view.equals(buttonclear))
{
display.setText(" ");
}

if(view.equals(buttonequals))
{
displaytext= displaytext.substring(0,displaytext.length()-1);
StringTokenizer operators= new StringTokenizer(displaytext, "+-*/",true);

while(operators.hasMoreTokens())
{
list.add(operators.nextToken());
}

for(int j=0; j<list.size()-1; j++)
{
if (list.get(j).equals("*") || list.get(j).equals("/"))
{
x = Double.parseDouble(list.get(j - 1));
y = Double.parseDouble(list.get(j + 1));

if (list.get(j).equals("*"))
{
result+=(x*y);
}

if (list.get(j).equals("/"))
{
result+=(x/y);
}
}
}
for(int k=0;k<list.size()-1;k++)
{
if (list.get(k).equals("+") || list.get(k).equals("-"))
{
x = Double.parseDouble(list.get(k - 1));
y = Double.parseDouble(list.get(k + 1));

if (list.get(k).equals("+"))
{
result+=(x+y);
}

if (list.get(k).equals("-"))
{
result+=(x-y);
}
}
}
}
display.setText(""+result);


}
}

Solutions

Expert Solution

<?xml version="1.0" encoding="utf-8"?>

<androidx.constraintlayout.widget.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"

    android:background="#8BC34A"

    android:backgroundTint="@android:color/darker_gray"

    tools:context=".MainActivity">

    <!-- Text View to display "gfg_myFirstApp "-->

    <TextView

        android:id="@+id/textView"

        android:layout_width="133dp"

        android:layout_height="28dp"

        android:layout_marginStart="139dp"

        android:layout_marginLeft="139dp"

        android:layout_marginTop="16dp"

        android:layout_marginEnd="139dp"

        android:layout_marginRight="139dp"

        android:layout_marginBottom="559dp"

        <!-- providing the green colour to the background -->

        android:background="#0F9D58"

        android:text="gfg_myFirstApp"

        android:textAppearance="@style/TextAppearance.AppCompat.Medium"

        app:layout_constraintBottom_toBottomOf="parent"

        app:layout_constraintEnd_toEndOf="parent"

        app:layout_constraintStart_toStartOf="parent"

        app:layout_constraintTop_toTopOf="parent" />

    <!-- Text View to display our basic heading of "calculator"-->

    <TextView

        android:layout_width="194dp"

        android:layout_height="43dp"

        android:layout_marginStart="114dp"

        android:layout_marginLeft="114dp"

        android:layout_marginTop="58dp"

        android:layout_marginEnd="103dp"

        android:layout_marginRight="103dp"

        android:layout_marginBottom="502dp"

        android:scrollbarSize="30dp"

        android:text="   Calculator"

        android:textAppearance="@style/TextAppearance.AppCompat.Body1"

        android:textSize="30dp"

        app:layout_constraintBottom_toBottomOf="parent"

        app:layout_constraintEnd_toEndOf="parent"

        app:layout_constraintStart_toStartOf="parent"

        app:layout_constraintTop_toTopOf="parent" />

    <!-- Edit Text View to input the values -->

    <EditText

        android:id="@+id/num1"

        android:layout_width="364dp"

        android:layout_height="28dp"

        android:layout_marginStart="72dp"

        android:layout_marginTop="70dp"

        android:layout_marginEnd="71dp"

        android:layout_marginBottom="416dp"

        android:background="@android:color/white"

        android:ems="10"

        android:hint="Number1(0)"

        android:inputType="number"

        app:layout_constraintBottom_toBottomOf="parent"

        app:layout_constraintEnd_toEndOf="parent"

        app:layout_constraintStart_toStartOf="parent"

        app:layout_constraintTop_toTopOf="parent" />

    <!-- Edit Text View to input 2nd value-->

    <EditText

        android:id="@+id/num2"

        android:layout_width="363dp"

        android:layout_height="30dp"

        android:layout_marginStart="72dp"

        android:layout_marginTop="112dp"

        android:layout_marginEnd="71dp"

        android:layout_marginBottom="374dp"

        android:background="@android:color/white"

        android:ems="10"

        android:hint="number2(0)"

        android:inputType="number"

        app:layout_constraintBottom_toBottomOf="parent"

        app:layout_constraintEnd_toEndOf="parent"

        app:layout_constraintStart_toStartOf="parent"

        app:layout_constraintTop_toTopOf="parent" />

    <!-- Text View to display result -->

    <TextView

        android:id="@+id/result"

        android:layout_width="356dp"

        android:layout_height="71dp"

        android:layout_marginStart="41dp"

        android:layout_marginTop="151dp"

        android:layout_marginEnd="48dp"

        android:layout_marginBottom="287dp"

        android:background="@android:color/white"

        android:text="result"

        android:textColorLink="#673AB7"

        android:textSize="25sp"

        app:layout_constraintBottom_toBottomOf="parent"

        app:layout_constraintEnd_toEndOf="parent"

        app:layout_constraintStart_toStartOf="parent"

        app:layout_constraintTop_toTopOf="parent" />

    <!-- A button to perform 'sum' operation -->

    <Button

        android:id="@+id/sum"

        android:layout_width="wrap_content"

        android:layout_height="wrap_content"

        android:layout_marginStart="16dp"

        android:layout_marginTop="292dp"

        android:layout_marginEnd="307dp"

        android:layout_marginBottom="263dp"

        android:backgroundTint="@android:color/holo_red_light"

        android:onClick="doSum"

        android:text="+"

        app:layout_constraintBottom_toBottomOf="parent"

        app:layout_constraintEnd_toEndOf="parent"

        app:layout_constraintStart_toStartOf="parent"

        app:layout_constraintTop_toTopOf="parent" />

    <!-- A button to perform subtraction operation. -->

    <Button

        android:id="@+id/sub"

        android:layout_width="wrap_content"

        android:layout_height="wrap_content"

        android:layout_marginStart="210dp"

        android:layout_marginTop="292dp"

        android:layout_marginEnd="113dp"

        android:layout_marginBottom="263dp"

        android:backgroundTint="@android:color/holo_red_light"

        android:onClick="doSub"

        android:text="-"

        app:layout_constraintBottom_toBottomOf="parent"

        app:layout_constraintEnd_toEndOf="parent"

        app:layout_constraintStart_toStartOf="parent"

        app:layout_constraintTop_toTopOf="parent" />

     

    <!-- A button to perform division. -->

    <Button

        android:id="@+id/div"

        android:layout_width="wrap_content"

        android:layout_height="wrap_content"

        android:layout_marginStart="307dp"

        android:layout_marginTop="292dp"

        android:layout_marginEnd="16dp"

        android:layout_marginBottom="263dp"

        android:backgroundTint="@android:color/holo_red_light"

        android:onClick="doDiv"

        android:text="/"

        app:layout_constraintBottom_toBottomOf="parent"

        app:layout_constraintEnd_toEndOf="parent"

        app:layout_constraintHorizontal_bias="0.0"

        app:layout_constraintStart_toStartOf="parent"

        app:layout_constraintTop_toTopOf="parent" />

     

    <!-- A button to perform multiplication. -->

    <Button

        android:id="@+id/mul"

        android:layout_width="wrap_content"

        android:layout_height="wrap_content"

        android:layout_marginStart="16dp"

        android:layout_marginTop="356dp"

        android:layout_marginEnd="307dp"

        android:layout_marginBottom="199dp"

        android:backgroundTint="@android:color/holo_red_light"

        android:onClick="doMul"

        android:text="x"

        app:layout_constraintBottom_toBottomOf="parent"

        app:layout_constraintEnd_toEndOf="parent"

        app:layout_constraintStart_toStartOf="parent"

        app:layout_constraintTop_toTopOf="parent" />

    

    <!-- A button to perform a modulus function. -->

    <Button

        android:id="@+id/button"

        android:layout_width="92dp"

        android:layout_height="48dp"

        android:layout_marginStart="113dp"

        android:layout_marginTop="356dp"

        android:layout_marginEnd="206dp"

        android:layout_marginBottom="199dp"

        android:backgroundTint="@android:color/holo_red_light"

        android:onClick="doMod"

        android:text="%(mod)"

        app:layout_constraintBottom_toBottomOf="parent"

        app:layout_constraintEnd_toEndOf="parent"

        app:layout_constraintStart_toStartOf="parent"

        app:layout_constraintTop_toTopOf="parent" />

    

    <!-- A button to perform a power function. -->

    <Button

        android:id="@+id/pow"

        android:layout_width="wrap_content"

        android:layout_height="wrap_content"

        android:layout_marginStart="113dp"

        android:layout_marginTop="292dp"

        android:layout_marginEnd="210dp"

        android:layout_marginBottom="263dp"

        android:backgroundTint="@android:color/holo_red_light"

        android:onClick="doPow"

        android:text="n1^n2"

        app:layout_constraintBottom_toBottomOf="parent"

        app:layout_constraintEnd_toEndOf="parent"

        app:layout_constraintStart_toStartOf="parent"

        app:layout_constraintTop_toTopOf="parent" />

</androidx.constraintlayout.widget.ConstraintLayout>


Related Solutions

On Android Studio, create a 4 function calculator. The only buttons you need are 0-9, *,...
On Android Studio, create a 4 function calculator. The only buttons you need are 0-9, *, /, +, -, a clear, and enter button. Use StringTokenizer or String.split for the calculator code.
On Android Studio, create a 4 function calculator. The only buttons you need are 0-9, *,...
On Android Studio, create a 4 function calculator. The only buttons you need are 0-9, *, /, +, -, a clear, and enter button. Having this java code below, I need the XML code that organizes the layout of the calculator (using either linearLayout or relativeLayout). As shown in the JAVA Code - Button button0, button1, button2, button3, button4, button5, button6, button7, button8, button9, buttonadd, buttonsubtract, buttonmultiply, buttondivide, buttonequals, buttonclear and TextView display - is what is needed in the...
I need the java code for a 4-function calculator app on android studio (do this on...
I need the java code for a 4-function calculator app on android studio (do this on android studio). Please make sure all the requirements shown below are followed (such as the error portion and etc). The topic of this app is to simply create a 4 function calculator which calculates math expressions (add, subtract, multiply, and divide numbers). The requirements are the following : - The only buttons needed are 0-9, *, /, +, -, a clear, and enter button...
I need the java code for a 4 function calculator app on android studio (do this...
I need the java code for a 4 function calculator app on android studio (do this on android studio) - The requirements are the following : - The only buttons needed are 0-9, *, /, +, -, a clear, and enter button - Implement the onclicklistener on the main activity - The calcuator should use order of operations (PEMDAS) - It should be able to continue from a previous answer (Ex: If you type 2+6 the calculator will display 8....
Using Android Studio, create a one java android app that has 4 buttons:- Change Color Button...
Using Android Studio, create a one java android app that has 4 buttons:- Change Color Button - When the Change Color button is clicked, it changes the current activity background to a randomly selected color Speak Button - When the Speak button is clicked, it opens a new activity named SpeakActivity. On the SpeakActivity there are three controls: EditText, Button (Speak) and Button (Back). The Speak button uses the Text to Speech service to say the text entered in EditText....
IN ANDROID STUDIO, you will create a mortgage calculator appthat allows the user to enter...
IN ANDROID STUDIO, you will create a mortgage calculator app that allows the user to enter a purchase price, down-payment amount, and an interest rate.Based on these values, the app should calculate the loan amount (purchase price minus down payment) and display the monthly payment for 10, 20, and 30-year loans.Allow the user to select a custom loan duration (in years) by using a SeekBar and display the monthly payment for that custom loan duration.Assignment deliverables (all in a ZIP...
I am doing a 4 function calculator app in java on android studio which calculates math...
I am doing a 4 function calculator app in java on android studio which calculates math expressions (add, subtract, multiply, and divide). I ONLY NEED THE CODE THE EQUAL BUTTON. When the user clicks the equal button, it should take the entire sequence of numbers and operators on the screen, such as 1+2*5 , and save them into a String [I am pretty sure this can be done using the toString() call]. Then, the String should be split using StringTokenizer,...
I need the JAVA code for a 4 function calculator app on andriod studio - The...
I need the JAVA code for a 4 function calculator app on andriod studio - The requirements are the following : - The only buttons needed are 0-9, *, /, +, -, a clear, and enter button - Implement the onclicklistener on the main activity - The calcuator should use order of operations (PEMDAS) - It should be able to continue from a previous answer (Ex: If you type 2+6 the calculator will display 8. If you then multiple by...
Android Studio (Java) I'm trying to create a simple calculator. I want to put out a...
Android Studio (Java) I'm trying to create a simple calculator. I want to put out a message if they try to divide by 0. I have this so far. What code should I put? divide.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { if (number1.getText().length() != 0 && number2.getText().length() != 0) { double n1= Double.parseDouble(number1.getText().toString()); double n2= Double.parseDouble(number2.getText().toString()); double res= n1 / n2; result.setText(String.valueOf(res)); } else { Toast.makeText(view.getContext(), "Please enter the numbers properly", Toast.LENGTH_SHORT).show(); } } });
For this IP, you will create a very simple drawing app using Android Studio. The purpose...
For this IP, you will create a very simple drawing app using Android Studio. The purpose of this assignment is to give you more building blocks to use when programming apps. For full credit for this assignment, you should complete the following: Create a menu and display menu items on the app bar Detect when the user touches the screen and moves a finger Be able to change the color and width of a line Be able to save an...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT