Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?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"
- android:orientation="vertical"
- android:padding="10dp"
- android:gravity="center"
- tools:context=".MainActivity">
- <EditText
- android:layout_width="310dp"
- android:layout_height="80dp"
- android:hint="Enter a number."
- android:id="@+id/number1"
- android:inputType="numberDecimal"
- android:textColor="@color/colorPrimaryDark"
- android:textSize="30dp"
- android:textColorHint="@color/colorPrimary" />
- <EditText
- android:layout_width="310dp"
- android:layout_height="80dp"
- android:hint="Enter another number."
- android:id="@+id/number2"
- android:inputType="numberDecimal"
- android:textColor="@color/colorPrimaryDark"
- android:textSize="30dp"
- android:textColorHint="@color/colorPrimary">
- </EditText>
- <LinearLayout
- android:layout_width="305dp"
- android:layout_height="90dp"
- android:layout_marginTop="10dp"
- android:gravity="center"
- android:orientation="horizontal"
- android:background="#BAE9E0">
- <Button
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:id="@+id/sum"
- android:text="SUM"
- android:textSize="30dp"
- android:background="#fff"
- android:textColor="@color/colorPrimaryDark">
- </Button>
- <Button
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:layout_marginLeft="10dp"
- android:id="@+id/sub"
- android:text="SUB"
- android:textSize="30dp"
- android:background="#fff"
- android:textColor="@color/colorPrimaryDark">
- </Button>
- </LinearLayout>
- <TextView
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:id="@+id/result"
- android:text=" Result "
- android:background="#BAE9E0"
- android:textSize="35dp"
- android:textColor="@color/colorPrimaryDark"
- android:layout_marginTop="20dp">
- </TextView>
- <Button
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:id="@+id/reset"
- android:text=" Reset "
- android:layout_marginTop="30dp"
- android:textSize="30dp"
- android:background="@color/colorPrimaryDark"
- android:textColor="#fff">
- </Button>
- </LinearLayout>
- package com.example.calculator;
- import androidx.appcompat.app.AppCompatActivity;
- import android.os.Bundle;
- import android.view.View;
- import android.widget.Button;
- import android.widget.EditText;
- import android.widget.TextView;
- public class MainActivity extends AppCompatActivity {
- EditText num1, num2;
- Button sum, sub, reset;
- TextView result;
- float number1, number2, output;
- @Override
- protected void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.activity_main);
- num1 = (EditText) findViewById(R.id.number1);
- num2 = (EditText) findViewById(R.id.number2);
- sum = (Button) findViewById(R.id.sum);
- sub = (Button) findViewById(R.id.sub);
- result = (TextView) findViewById(R.id.result);
- reset = (Button) findViewById(R.id.reset);
- sum.setOnClickListener(new View.OnClickListener() {
- @Override
- public void onClick(View v) {
- number1 = Float.valueOf(num1.getText().toString());
- number2 = Float.valueOf(num2.getText().toString());
- output = (number1 + number2);
- result.setText(" " + String.valueOf(output) + " ");
- }
- });
- sub.setOnClickListener(new View.OnClickListener() {
- @Override
- public void onClick(View v) {
- number1 = Float.valueOf(num1.getText().toString());
- number2 = Float.valueOf(num2.getText().toString());
- output = (number1 - number2);
- result.setText(" " + String.valueOf(output) + " ");
- }
- });
- reset.setOnClickListener(new View.OnClickListener() {
- @Override
- public void onClick(View v) {
- num1.setText("");
- num2.setText("");
- result.setText(" Result ");
- }
- });
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement