MainActivity.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/activity_main"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="com.practice.bhaumik.calculator.MainActivity">
<TextView
android:layout_width="match_parent"
android:layout_height="70sp"
android:textSize="50sp"
android:textAlignment="textEnd"
android:id="@+id/tv_input"
/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="80sp"
android:text="7"
android:textSize="25sp"
android:id="@+id/btn7"
android:minHeight="60sp"/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="80sp"
android:text="8"
android:textSize="25sp"
android:id="@+id/btn8"
android:minHeight="60sp"
android:layout_toRightOf="@+id/btn7"/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="80sp"
android:text="9"
android:textSize="25sp"
android:id="@+id/btn9"
android:minHeight="60sp"
android:layout_toRightOf="@+id/btn8"/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="80sp"
android:text="Del"
android:textSize="18sp"
android:minHeight="60sp"
android:id="@+id/btn_del"
android:layout_toRightOf="@+id/btn9"/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="150sp"
android:text="4"
android:textSize="25sp"
android:minHeight="60sp"
android:id="@+id/btn4"
/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="150sp"
android:text="5"
android:textSize="25sp"
android:minHeight="60sp"
android:id="@+id/btn5"
android:layout_toRightOf="@+id/btn4"
/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="150sp"
android:text="6"
android:textSize="25sp"
android:minHeight="60sp"
android:id="@+id/btn6"
android:layout_toRightOf="@+id/btn5"
/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="150sp"
android:text="/"
android:textSize="22sp"
android:minHeight="60sp"
android:id="@+id/btn_divide"
android:layout_toRightOf="@+id/btn6"
/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="220sp"
android:text="1"
android:textSize="22sp"
android:minHeight="60sp"
android:id="@+id/btn1"
/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="220sp"
android:text="2"
android:textSize="22sp"
android:minHeight="60sp"
android:id="@+id/btn2"
android:layout_toRightOf="@+id/btn1"
/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="220sp"
android:text="3"
android:textSize="22sp"
android:minHeight="60sp"
android:id="@+id/btn3"
android:layout_toRightOf="@+id/btn2"
/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="220sp"
android:text="X"
android:textSize="22sp"
android:minHeight="60sp"
android:id="@+id/btn_multiply"
android:layout_toRightOf="@+id/btn3"
/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="290sp"
android:text="."
android:textSize="22sp"
android:minHeight="60sp"
android:id="@+id/btn_dot"
/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="290sp"
android:text="0"
android:textSize="22sp"
android:minHeight="60sp"
android:id="@+id/btn0"
android:layout_toRightOf="@+id/btn_dot"
/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="290sp"
android:text="="
android:textSize="22sp"
android:minHeight="60sp"
android:id="@+id/btn_equal"
android:layout_toRightOf="@+id/btn0"
/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="290sp"
android:text="+"
android:textSize="22sp"
android:minHeight="60sp"
android:id="@+id/btn_plus"
android:layout_toRightOf="@+id/btn_equal"
/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="360sp"
android:text="-"
android:textSize="22sp"
android:minHeight="60sp"
android:id="@+id/btn_minus"
android:layout_toRightOf="@+id/btn_equal"
/>
</RelativeLayout>
MainActivity.java
package com.practice.bhaumik.calculator;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
public class MainActivity extends AppCompatActivity {
float num1 = 0,num2 = 0;
TextView textView;
Button b1,b2,b3,b4,b5,b6,b7,b8,b9,b0,dot,equal,plus,minus,mul,div,del;
boolean add,sub,multi,division;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
textView = (TextView) findViewById(R.id.tv_input);
b1 = (Button) findViewById(R.id.btn1);
b2 = (Button) findViewById(R.id.btn2);
b3 = (Button) findViewById(R.id.btn3);
b4 = (Button) findViewById(R.id.btn4);
b5 = (Button) findViewById(R.id.btn5);
b6 = (Button) findViewById(R.id.btn6);
b7 = (Button) findViewById(R.id.btn7);
b8 = (Button) findViewById(R.id.btn8);
b9 = (Button) findViewById(R.id.btn9);
b0 = (Button) findViewById(R.id.btn0);
equal = (Button) findViewById(R.id.btn_equal);
plus = (Button) findViewById(R.id.btn_plus);
minus = (Button) findViewById(R.id.btn_minus);
mul = (Button) findViewById(R.id.btn_multiply);
div = (Button) findViewById(R.id.btn_divide);
del = (Button) findViewById(R.id.btn_del);
dot = (Button) findViewById(R.id.btn_dot);
b1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
textView.setText(textView.getText() + "1");
}
});
b2.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
textView.setText(textView.getText() + "2");
}
});
b3.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
textView.setText(textView.getText() + "3");
}
});
b4.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
textView.setText(textView.getText() + "4");
}
});
b5.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
textView.setText(textView.getText() + "5");
}
});
b6.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
textView.setText(textView.getText() + "6");
}
});
b7.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
textView.setText(textView.getText() + "7");
}
});
b8.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
textView.setText(textView.getText() + "8");
}
});
b9.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
textView.setText(textView.getText() + "9");
}
});
b0.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
textView.setText(textView.getText() + "0");
}
});
dot.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
textView.setText(textView.getText() + ".");
}
});
plus.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
num1 = Float.parseFloat(textView.getText() + "");
add = true;
textView.setText("");
}
});
minus.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
num1 = Float.parseFloat(textView.getText() + "");
sub = true;
textView.setText("");
}
});
mul.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
num1 = Float.parseFloat(textView.getText() + "");
multi = true;
textView.setText("");
}
});
div.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
num1 = Float.parseFloat(textView.getText() + "");
division = true;
textView.setText("");
}
});
del.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
textView.setText("");
}
});
equal.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
num2 = Float.parseFloat(textView.getText() + "");
if(add == true)
{
textView.setText(String.valueOf(num1 + num2));
add = false;
}
if(sub == true)
{
textView.setText(String.valueOf(num1 - num2));
sub = false;
}
if(multi == true)
{
textView.setText(String.valueOf(num1 * num2));
multi = false;
}
if(division == true)
{
textView.setText(String.valueOf(num1 / num2));
division = false;
}
}
});
}
}
0 comments:
Post a Comment