Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- Activity Main (XML)
- -------------------
- <?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="fill_parent"
- android:orientation="vertical"
- android:layout_height="wrap_content"
- android:gravity="center"
- tools:context="com.example.work.myapplication.MainActivity">
- <EditText
- android:id="@+id/disp"
- android:layout_width="fill_parent"
- android:layout_height="wrap_content"
- android:gravity="center"
- android:hint="Enter the values"
- android:textSize="30sp"
- android:layout_marginBottom="25dp"/>
- <LinearLayout
- android:layout_width="match_parent"
- android:layout_height="match_parent"
- android:orientation="horizontal"
- android:gravity="center">
- <Button
- android:id="@+id/one"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:text="1"/>
- <Button
- android:id="@+id/two"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:text="2"/>
- <Button
- android:id="@+id/three"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:text="3"/>
- <Button
- android:id="@+id/add"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:text="+"/>
- </LinearLayout>
- <LinearLayout
- android:layout_width="match_parent"
- android:layout_height="match_parent"
- android:orientation="horizontal"
- android:gravity="center">
- <Button
- android:id="@+id/four"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:text="4"/>
- <Button
- android:id="@+id/five"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:text="5"/>
- <Button
- android:id="@+id/six"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:text="6"/>
- <Button
- android:id="@+id/sub"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:text="-"/>
- </LinearLayout>
- <LinearLayout
- android:layout_width="match_parent"
- android:layout_height="match_parent"
- android:orientation="horizontal"
- android:gravity="center">
- <Button
- android:id="@+id/seven"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:text="7"/>
- <Button
- android:id="@+id/eight"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:text="8"/>
- <Button
- android:id="@+id/nine"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:text="9"/>
- <Button
- android:id="@+id/mul"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:text="*"/>
- </LinearLayout>
- <LinearLayout
- android:layout_width="match_parent"
- android:layout_height="match_parent"
- android:orientation="horizontal"
- android:gravity="center">
- <Button
- android:id="@+id/zero"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:text="0"/>
- <Button
- android:id="@+id/cancel"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:text="CLR"/>
- <Button
- android:id="@+id/equal"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:text="="/>
- <Button
- android:id="@+id/div"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:text="/"/>
- </LinearLayout>
- </LinearLayout>
- ============================
- Main_Activity (Java)
- --------------------------
- package com.example.work.myapplication;
- import android.support.v7.app.AppCompatActivity;
- import android.os.Bundle;
- import android.view.View;
- import android.widget.Button;
- import android.widget.EditText;
- public class MainActivity extends AppCompatActivity {
- Button one, two, three, four, five, six, seven, eight, nine, zero, add, sub, mul, div, cancel, equal;
- EditText disp;
- int a;
- int b;
- boolean addition, subtraction, multiplication, division;
- @Override
- protected void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.activity_main);
- one=(Button) findViewById(R.id.one);
- two=(Button) findViewById(R.id.two);
- three=(Button) findViewById(R.id.three);
- four=(Button) findViewById(R.id.four);
- five=(Button) findViewById(R.id.five);
- six=(Button) findViewById(R.id.six);
- seven=(Button) findViewById(R.id.seven);
- eight=(Button) findViewById(R.id.eight);
- nine=(Button) findViewById(R.id.nine);
- zero=(Button) findViewById(R.id.zero);
- add=(Button) findViewById(R.id.add);
- sub=(Button) findViewById(R.id.sub);
- mul=(Button) findViewById(R.id.mul);
- div=(Button) findViewById(R.id.div);
- cancel=(Button) findViewById(R.id.cancel);
- equal=(Button) findViewById(R.id.equal);
- disp=(EditText) findViewById(R.id.disp);
- one.setOnClickListener(new View.OnClickListener() {
- @Override
- public void onClick(View v) {
- disp.setText(disp.getText() + "1");
- }
- });
- two.setOnClickListener(new View.OnClickListener() {
- @Override
- public void onClick(View v) {
- disp.setText(disp.getText() + "2");
- }
- });
- three.setOnClickListener(new View.OnClickListener() {
- @Override
- public void onClick(View v) {
- disp.setText(disp.getText() + "3");
- }
- });
- four.setOnClickListener(new View.OnClickListener() {
- @Override
- public void onClick(View v) {
- disp.setText(disp.getText() + "4");
- }
- });
- five.setOnClickListener(new View.OnClickListener() {
- @Override
- public void onClick(View v) {
- disp.setText(disp.getText() + "5");
- }
- });
- six.setOnClickListener(new View.OnClickListener() {
- @Override
- public void onClick(View v) {
- disp.setText(disp.getText() + "6");
- }
- });
- seven.setOnClickListener(new View.OnClickListener() {
- @Override
- public void onClick(View v) {
- disp.setText(disp.getText() + "7");
- }
- });
- eight.setOnClickListener(new View.OnClickListener() {
- @Override
- public void onClick(View v) {
- disp.setText(disp.getText() + "8");
- }
- });
- nine.setOnClickListener(new View.OnClickListener() {
- @Override
- public void onClick(View v) {
- disp.setText(disp.getText() + "9");
- }
- });
- zero.setOnClickListener(new View.OnClickListener() {
- @Override
- public void onClick(View v) {
- disp.setText(disp.getText() + "0");
- }
- });
- add.setOnClickListener(new View.OnClickListener() {
- @Override
- public void onClick(View v) {
- if (disp == null)
- {
- disp.setText("");
- }
- else {
- a = Integer.parseInt(disp.getText() + "");
- addition = true;
- disp.setText(null);
- }
- }
- });
- sub.setOnClickListener(new View.OnClickListener() {
- @Override
- public void onClick(View v) {
- if (disp == null)
- {
- disp.setText("");
- }
- else {
- a = Integer.parseInt(disp.getText() + "");
- subtraction = true;
- disp.setText(null);
- }
- }
- });
- mul.setOnClickListener(new View.OnClickListener() {
- @Override
- public void onClick(View v) {
- if (disp == null)
- {
- disp.setText("");
- }
- else {
- a = Integer.parseInt(disp.getText() + "");
- multiplication = true;
- disp.setText(null);
- }
- }
- });
- div.setOnClickListener(new View.OnClickListener() {
- @Override
- public void onClick(View v) {
- if (disp == null)
- {
- disp.setText("");
- }
- else {
- a = Integer.parseInt(disp.getText() + "");
- division = true;
- disp.setText(null);
- }
- }
- });
- cancel.setOnClickListener(new View.OnClickListener() {
- @Override
- public void onClick(View v)
- {
- disp.setText("");
- }
- });
- equal.setOnClickListener(new View.OnClickListener() {
- @Override
- public void onClick(View v) {
- b = Integer.parseInt(disp.getText() + "");
- if (addition == true){
- disp.setText(a + b +"");
- addition=false;
- }
- if (subtraction == true){
- disp.setText(a - b +"");
- subtraction=false;
- }
- if (multiplication == true){
- disp.setText(a * b +"");
- multiplication=false;
- }
- if (division == true){
- disp.setText(a / b +"");
- division=false;
- }
- }
- });
- }
- }
- =====================
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement