Advertisement
Guest User

Hridoy - Wireless Projects - Calculator

a guest
Oct 14th, 2019
110
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 4.80 KB | None | 0 0
  1. <?xml version="1.0" encoding="utf-8"?>
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  3.     xmlns:app="http://schemas.android.com/apk/res-auto"
  4.     xmlns:tools="http://schemas.android.com/tools"
  5.     android:layout_width="match_parent"
  6.     android:layout_height="match_parent"
  7.     android:orientation="vertical"
  8.     android:padding="10dp"
  9.     android:gravity="center"
  10.     tools:context=".MainActivity">
  11.  
  12.     <EditText
  13.         android:layout_width="310dp"
  14.         android:layout_height="80dp"
  15.         android:hint="Enter a number."
  16.         android:id="@+id/number1"
  17.         android:inputType="numberDecimal"
  18.         android:textColor="@color/colorPrimaryDark"
  19.         android:textSize="30dp"
  20.         android:textColorHint="@color/colorPrimary" />
  21.  
  22.     <EditText
  23.         android:layout_width="310dp"
  24.         android:layout_height="80dp"
  25.         android:hint="Enter another number."
  26.         android:id="@+id/number2"
  27.         android:inputType="numberDecimal"
  28.         android:textColor="@color/colorPrimaryDark"
  29.         android:textSize="30dp"
  30.         android:textColorHint="@color/colorPrimary">
  31.     </EditText>
  32.  
  33.     <LinearLayout
  34.         android:layout_width="305dp"
  35.         android:layout_height="90dp"
  36.         android:layout_marginTop="10dp"
  37.         android:gravity="center"
  38.         android:orientation="horizontal"
  39.         android:background="#BAE9E0">
  40.  
  41.         <Button
  42.             android:layout_width="wrap_content"
  43.             android:layout_height="wrap_content"
  44.             android:id="@+id/sum"
  45.             android:text="SUM"
  46.             android:textSize="30dp"
  47.             android:background="#fff"
  48.             android:textColor="@color/colorPrimaryDark">
  49.         </Button>
  50.  
  51.         <Button
  52.             android:layout_width="wrap_content"
  53.             android:layout_height="wrap_content"
  54.             android:layout_marginLeft="10dp"
  55.             android:id="@+id/sub"
  56.             android:text="SUB"
  57.             android:textSize="30dp"
  58.             android:background="#fff"
  59.             android:textColor="@color/colorPrimaryDark">
  60.         </Button>
  61.  
  62.     </LinearLayout>
  63.  
  64.     <TextView
  65.         android:layout_width="wrap_content"
  66.         android:layout_height="wrap_content"
  67.         android:id="@+id/result"
  68.         android:text=" Result "
  69.         android:background="#BAE9E0"
  70.         android:textSize="35dp"
  71.         android:textColor="@color/colorPrimaryDark"
  72.         android:layout_marginTop="20dp">
  73.     </TextView>
  74.  
  75.  
  76.     <Button
  77.         android:layout_width="wrap_content"
  78.         android:layout_height="wrap_content"
  79.         android:id="@+id/reset"
  80.         android:text=" Reset "
  81.         android:layout_marginTop="30dp"
  82.         android:textSize="30dp"
  83.         android:background="@color/colorPrimaryDark"
  84.         android:textColor="#fff">
  85.     </Button>
  86.  
  87.  
  88. </LinearLayout>
  89.  
  90.  
  91.  
  92.  
  93.  
  94.  
  95.  
  96. package com.example.calculator;
  97.  
  98. import androidx.appcompat.app.AppCompatActivity;
  99.  
  100. import android.os.Bundle;
  101. import android.view.View;
  102. import android.widget.Button;
  103. import android.widget.EditText;
  104. import android.widget.TextView;
  105.  
  106. public class MainActivity extends AppCompatActivity {
  107.  
  108.     EditText num1, num2;
  109.     Button sum, sub, reset;
  110.     TextView result;
  111.     float number1, number2, output;
  112.  
  113.     @Override
  114.     protected void onCreate(Bundle savedInstanceState) {
  115.         super.onCreate(savedInstanceState);
  116.         setContentView(R.layout.activity_main);
  117.  
  118.         num1 = (EditText) findViewById(R.id.number1);
  119.         num2 = (EditText) findViewById(R.id.number2);
  120.         sum = (Button) findViewById(R.id.sum);
  121.         sub = (Button) findViewById(R.id.sub);
  122.         result = (TextView) findViewById(R.id.result);
  123.         reset = (Button) findViewById(R.id.reset);
  124.  
  125.  
  126.         sum.setOnClickListener(new View.OnClickListener() {
  127.             @Override
  128.             public void onClick(View v) {
  129.                 number1 = Float.valueOf(num1.getText().toString());
  130.                 number2 = Float.valueOf(num2.getText().toString());
  131.                 output = (number1 + number2);
  132.                 result.setText(" " + String.valueOf(output) + " ");
  133.             }
  134.         });
  135.  
  136.         sub.setOnClickListener(new View.OnClickListener() {
  137.             @Override
  138.             public void onClick(View v) {
  139.                 number1 = Float.valueOf(num1.getText().toString());
  140.                 number2 = Float.valueOf(num2.getText().toString());
  141.                 output = (number1 - number2);
  142.                 result.setText(" " + String.valueOf(output) + " ");
  143.             }
  144.         });
  145.  
  146.         reset.setOnClickListener(new View.OnClickListener() {
  147.             @Override
  148.             public void onClick(View v) {
  149.                 num1.setText("");
  150.                 num2.setText("");
  151.                 result.setText(" Result ");
  152.             }
  153.         });
  154.     }
  155.  
  156.  
  157. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement