In: Computer Science
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 XML layout for this 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);
}
}
XML Code:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
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=".MainActivity"
android:orientation="vertical"
android:weightSum="100">
<TextView
android:id="@+id/display"
android:layout_width="match_parent"
android:layout_height="0px"
android:layout_weight="15"
android:text=""
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="0px"
android:layout_weight="20"
android:orientation="horizontal"
android:weightSum="100">
<Button
android:id="@+id/buttonadd"
android:layout_width="0px"
android:layout_height="match_parent"
android:layout_weight="25"
android:text="+"/>
<Button
android:id="@+id/button7"
android:layout_width="0px"
android:layout_height="match_parent"
android:layout_weight="25"
android:text="7"/>
<Button
android:id="@+id/button8"
android:layout_width="0px"
android:layout_height="match_parent"
android:layout_weight="25"
android:text="8"/>
<Button
android:id="@+id/button9"
android:layout_width="0px"
android:layout_height="match_parent"
android:layout_weight="25"
android:text="9"/>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="0px"
android:layout_weight="20"
android:orientation="horizontal"
android:weightSum="100">
<Button
android:id="@+id/buttonsubtract"
android:layout_width="0px"
android:layout_height="match_parent"
android:layout_weight="25"
android:text="-"/>
<Button
android:id="@+id/button4"
android:layout_width="0px"
android:layout_height="match_parent"
android:layout_weight="25"
android:text="4"/>
<Button
android:id="@+id/button5"
android:layout_width="0px"
android:layout_height="match_parent"
android:layout_weight="25"
android:text="5"/>
<Button
android:id="@+id/button6"
android:layout_width="0px"
android:layout_height="match_parent"
android:layout_weight="25"
android:text="6"/>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="0px"
android:layout_weight="20"
android:orientation="horizontal"
android:weightSum="100">
<Button
android:id="@+id/buttonmultiply"
android:layout_width="0px"
android:layout_height="match_parent"
android:layout_weight="25"
android:text="*"/>
<Button
android:id="@+id/button1"
android:layout_width="0px"
android:layout_height="match_parent"
android:layout_weight="25"
android:text="1"/>
<Button
android:id="@+id/button2"
android:layout_width="0px"
android:layout_height="match_parent"
android:layout_weight="25"
android:text="2"/>
<Button
android:id="@+id/button3"
android:layout_width="0px"
android:layout_height="match_parent"
android:layout_weight="25"
android:text="3"/>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="0px"
android:layout_weight="20"
android:orientation="horizontal"
android:weightSum="100">
<Button
android:id="@+id/buttondivide"
android:layout_width="0px"
android:layout_height="match_parent"
android:layout_weight="25"
android:text="/"/>
<Button
android:id="@+id/buttonclear"
android:layout_width="0px"
android:layout_height="match_parent"
android:layout_weight="25"
android:text="Clear"/>
<Button
android:id="@+id/button0"
android:layout_width="0px"
android:layout_height="match_parent"
android:layout_weight="25"
android:text="0"/>
<Button
android:id="@+id/buttonequals"
android:layout_width="0px"
android:layout_height="match_parent"
android:layout_weight="25"
android:text="="/>
</LinearLayout>
</LinearLayout>
Output: