Advertisement
Tusohian

Calculator

Oct 25th, 2019
857
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 10.95 KB | None | 0 0
  1. Activity Main (XML)
  2. -------------------
  3.  
  4.  
  5. <?xml version="1.0" encoding="utf-8"?>
  6. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  7.     xmlns:app="http://schemas.android.com/apk/res-auto"
  8.     xmlns:tools="http://schemas.android.com/tools"
  9.     android:layout_width="fill_parent"
  10.     android:orientation="vertical"
  11.     android:layout_height="wrap_content"
  12.     android:gravity="center"
  13.     tools:context="com.example.work.myapplication.MainActivity">
  14.  
  15.  
  16.     <EditText
  17.         android:id="@+id/disp"
  18.         android:layout_width="fill_parent"
  19.         android:layout_height="wrap_content"
  20.         android:gravity="center"
  21.         android:hint="Enter the values"
  22.         android:textSize="30sp"
  23.         android:layout_marginBottom="25dp"/>
  24.  
  25.     <LinearLayout
  26.         android:layout_width="match_parent"
  27.         android:layout_height="match_parent"
  28.         android:orientation="horizontal"
  29.         android:gravity="center">
  30.  
  31.         <Button
  32.             android:id="@+id/one"
  33.             android:layout_width="wrap_content"
  34.             android:layout_height="wrap_content"
  35.             android:text="1"/>
  36.  
  37.         <Button
  38.             android:id="@+id/two"
  39.             android:layout_width="wrap_content"
  40.             android:layout_height="wrap_content"
  41.             android:text="2"/>
  42.  
  43.         <Button
  44.             android:id="@+id/three"
  45.             android:layout_width="wrap_content"
  46.             android:layout_height="wrap_content"
  47.             android:text="3"/>
  48.  
  49.         <Button
  50.             android:id="@+id/add"
  51.             android:layout_width="wrap_content"
  52.             android:layout_height="wrap_content"
  53.             android:text="+"/>
  54.  
  55.     </LinearLayout>
  56.  
  57.     <LinearLayout
  58.         android:layout_width="match_parent"
  59.         android:layout_height="match_parent"
  60.         android:orientation="horizontal"
  61.         android:gravity="center">
  62.  
  63.         <Button
  64.             android:id="@+id/four"
  65.             android:layout_width="wrap_content"
  66.             android:layout_height="wrap_content"
  67.             android:text="4"/>
  68.  
  69.         <Button
  70.             android:id="@+id/five"
  71.             android:layout_width="wrap_content"
  72.             android:layout_height="wrap_content"
  73.             android:text="5"/>
  74.  
  75.         <Button
  76.             android:id="@+id/six"
  77.             android:layout_width="wrap_content"
  78.             android:layout_height="wrap_content"
  79.             android:text="6"/>
  80.  
  81.         <Button
  82.             android:id="@+id/sub"
  83.             android:layout_width="wrap_content"
  84.             android:layout_height="wrap_content"
  85.             android:text="-"/>
  86.  
  87.     </LinearLayout>
  88.  
  89.     <LinearLayout
  90.         android:layout_width="match_parent"
  91.         android:layout_height="match_parent"
  92.         android:orientation="horizontal"
  93.         android:gravity="center">
  94.  
  95.         <Button
  96.             android:id="@+id/seven"
  97.             android:layout_width="wrap_content"
  98.             android:layout_height="wrap_content"
  99.             android:text="7"/>
  100.  
  101.         <Button
  102.             android:id="@+id/eight"
  103.             android:layout_width="wrap_content"
  104.             android:layout_height="wrap_content"
  105.             android:text="8"/>
  106.  
  107.         <Button
  108.             android:id="@+id/nine"
  109.             android:layout_width="wrap_content"
  110.             android:layout_height="wrap_content"
  111.             android:text="9"/>
  112.  
  113.         <Button
  114.             android:id="@+id/mul"
  115.             android:layout_width="wrap_content"
  116.             android:layout_height="wrap_content"
  117.             android:text="*"/>
  118.  
  119.     </LinearLayout>
  120.  
  121.     <LinearLayout
  122.         android:layout_width="match_parent"
  123.         android:layout_height="match_parent"
  124.         android:orientation="horizontal"
  125.         android:gravity="center">
  126.  
  127.         <Button
  128.             android:id="@+id/zero"
  129.             android:layout_width="wrap_content"
  130.             android:layout_height="wrap_content"
  131.             android:text="0"/>
  132.  
  133.         <Button
  134.             android:id="@+id/cancel"
  135.             android:layout_width="wrap_content"
  136.             android:layout_height="wrap_content"
  137.             android:text="CLR"/>
  138.  
  139.         <Button
  140.             android:id="@+id/equal"
  141.             android:layout_width="wrap_content"
  142.             android:layout_height="wrap_content"
  143.             android:text="="/>
  144.  
  145.         <Button
  146.             android:id="@+id/div"
  147.             android:layout_width="wrap_content"
  148.             android:layout_height="wrap_content"
  149.             android:text="/"/>
  150.  
  151.     </LinearLayout>
  152.  
  153.  
  154. </LinearLayout>
  155.  
  156.  
  157.  
  158.  
  159.  
  160. ============================
  161.  
  162. Main_Activity (Java)
  163. --------------------------
  164.  
  165. package com.example.work.myapplication;
  166.  
  167. import android.support.v7.app.AppCompatActivity;
  168. import android.os.Bundle;
  169. import android.view.View;
  170. import android.widget.Button;
  171. import android.widget.EditText;
  172.  
  173. public class MainActivity extends AppCompatActivity {
  174.  
  175.     Button one, two, three, four, five, six, seven, eight, nine, zero, add, sub, mul, div, cancel, equal;
  176.     EditText disp;
  177.     int a;
  178.     int b;
  179.     boolean addition, subtraction, multiplication, division;
  180.  
  181.     @Override
  182.     protected void onCreate(Bundle savedInstanceState) {
  183.         super.onCreate(savedInstanceState);
  184.         setContentView(R.layout.activity_main);
  185.  
  186.  
  187.         one=(Button) findViewById(R.id.one);
  188.         two=(Button) findViewById(R.id.two);
  189.         three=(Button) findViewById(R.id.three);
  190.         four=(Button) findViewById(R.id.four);
  191.         five=(Button) findViewById(R.id.five);
  192.         six=(Button) findViewById(R.id.six);
  193.         seven=(Button) findViewById(R.id.seven);
  194.         eight=(Button) findViewById(R.id.eight);
  195.         nine=(Button) findViewById(R.id.nine);
  196.         zero=(Button) findViewById(R.id.zero);
  197.         add=(Button) findViewById(R.id.add);
  198.         sub=(Button) findViewById(R.id.sub);
  199.         mul=(Button) findViewById(R.id.mul);
  200.         div=(Button) findViewById(R.id.div);
  201.         cancel=(Button) findViewById(R.id.cancel);
  202.         equal=(Button) findViewById(R.id.equal);
  203.         disp=(EditText) findViewById(R.id.disp);
  204.  
  205.         one.setOnClickListener(new View.OnClickListener() {
  206.             @Override
  207.             public void onClick(View v) {
  208.                 disp.setText(disp.getText() + "1");
  209.             }
  210.         });
  211.  
  212.         two.setOnClickListener(new View.OnClickListener() {
  213.             @Override
  214.             public void onClick(View v) {
  215.                 disp.setText(disp.getText() + "2");
  216.             }
  217.         });
  218.  
  219.         three.setOnClickListener(new View.OnClickListener() {
  220.             @Override
  221.             public void onClick(View v) {
  222.                 disp.setText(disp.getText() + "3");
  223.             }
  224.         });
  225.  
  226.         four.setOnClickListener(new View.OnClickListener() {
  227.             @Override
  228.             public void onClick(View v) {
  229.                 disp.setText(disp.getText() + "4");
  230.             }
  231.         });
  232.  
  233.         five.setOnClickListener(new View.OnClickListener() {
  234.             @Override
  235.             public void onClick(View v) {
  236.                 disp.setText(disp.getText() + "5");
  237.             }
  238.         });
  239.  
  240.         six.setOnClickListener(new View.OnClickListener() {
  241.             @Override
  242.             public void onClick(View v) {
  243.                 disp.setText(disp.getText() + "6");
  244.             }
  245.         });
  246.  
  247.         seven.setOnClickListener(new View.OnClickListener() {
  248.             @Override
  249.             public void onClick(View v) {
  250.                 disp.setText(disp.getText() + "7");
  251.             }
  252.         });
  253.  
  254.         eight.setOnClickListener(new View.OnClickListener() {
  255.             @Override
  256.             public void onClick(View v) {
  257.                 disp.setText(disp.getText() + "8");
  258.             }
  259.         });
  260.  
  261.         nine.setOnClickListener(new View.OnClickListener() {
  262.             @Override
  263.             public void onClick(View v) {
  264.                 disp.setText(disp.getText() + "9");
  265.             }
  266.         });
  267.  
  268.         zero.setOnClickListener(new View.OnClickListener() {
  269.             @Override
  270.             public void onClick(View v) {
  271.                 disp.setText(disp.getText() + "0");
  272.             }
  273.         });
  274.  
  275.         add.setOnClickListener(new View.OnClickListener() {
  276.             @Override
  277.             public void onClick(View v) {
  278.                 if (disp == null)
  279.                 {
  280.                     disp.setText("");
  281.                 }
  282.                 else {
  283.                     a = Integer.parseInt(disp.getText() + "");
  284.                     addition = true;
  285.                     disp.setText(null);
  286.                 }
  287.             }
  288.         });
  289.  
  290.  
  291.         sub.setOnClickListener(new View.OnClickListener() {
  292.             @Override
  293.             public void onClick(View v) {
  294.                 if (disp == null)
  295.                 {
  296.                     disp.setText("");
  297.                 }
  298.                 else {
  299.                     a = Integer.parseInt(disp.getText() + "");
  300.                     subtraction = true;
  301.                     disp.setText(null);
  302.                 }
  303.             }
  304.         });
  305.  
  306.         mul.setOnClickListener(new View.OnClickListener() {
  307.             @Override
  308.             public void onClick(View v) {
  309.                 if (disp == null)
  310.                 {
  311.                     disp.setText("");
  312.                 }
  313.                 else {
  314.                     a = Integer.parseInt(disp.getText() + "");
  315.                     multiplication = true;
  316.                     disp.setText(null);
  317.                 }
  318.             }
  319.         });
  320.  
  321.         div.setOnClickListener(new View.OnClickListener() {
  322.             @Override
  323.             public void onClick(View v) {
  324.                 if (disp == null)
  325.                 {
  326.                     disp.setText("");
  327.                 }
  328.                 else {
  329.                     a = Integer.parseInt(disp.getText() + "");
  330.                     division = true;
  331.                     disp.setText(null);
  332.                 }
  333.             }
  334.         });
  335.  
  336.  
  337.         cancel.setOnClickListener(new View.OnClickListener() {
  338.             @Override
  339.             public void onClick(View v)
  340.             {
  341.                 disp.setText("");
  342.             }
  343.         });
  344.  
  345.  
  346.  
  347.         equal.setOnClickListener(new View.OnClickListener() {
  348.             @Override
  349.             public void onClick(View v) {
  350.                 b = Integer.parseInt(disp.getText() + "");
  351.  
  352.                 if (addition == true){
  353.                     disp.setText(a + b +"");
  354.                     addition=false;
  355.                 }
  356.  
  357.                 if (subtraction == true){
  358.                     disp.setText(a - b +"");
  359.                     subtraction=false;
  360.                 }
  361.  
  362.                 if (multiplication == true){
  363.                     disp.setText(a * b +"");
  364.                     multiplication=false;
  365.                 }
  366.  
  367.                 if (division == true){
  368.                     disp.setText(a / b +"");
  369.                     division=false;
  370.                 }
  371.             }
  372.         });
  373.  
  374.  
  375.  
  376.  
  377.  
  378.  
  379.  
  380.  
  381.     }
  382. }
  383.  
  384.  
  385. =====================
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement