Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,23 +1,22 @@
package com.example.android.mycalculator;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

public class MainActivity extends AppCompatActivity {

int result=0;
EditText edt1;
EditText edt2;
Button btn1;
Button btn2;
Button btn3;
Button btn4;
Button btn5;
TextView tv1;
EditText edt1;
EditText edt2;
Button btn1;
Button btn2;
Button btn3;
Button btn4;
Button btn5;
TextView tv1;


@Override
Expand All @@ -31,80 +30,57 @@ protected void onCreate(Bundle savedInstanceState) {
btn3 = findViewById(R.id.button3);
btn4 = findViewById(R.id.button4);
btn5 = findViewById(R.id.button5);
tv1= findViewById(R.id.textView1);
tv1 = findViewById(R.id.textView1);

btn1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
int num1 = Integer.parseInt(edt1.getText().toString());
int num2 = Integer.parseInt(edt2.getText().toString());

String str1 = edt1.getText().toString();
int num1 = Integer.parseInt(str1);

String str2 = edt2.getText().toString();
int num2 = Integer.parseInt(str2);

result = num1 + num2;

int result = num1 + num2;
displayOutput(result);
}
});

btn2.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
String str1 = edt1.getText().toString();
int num1 = Integer.parseInt(str1);

String str2 = edt2.getText().toString();
int num2 = Integer.parseInt(str2);

result = num1 - num2;
int num1 = Integer.parseInt(edt1.getText().toString());
int num2 = Integer.parseInt(edt2.getText().toString());

int result = num1 - num2;
displayOutput(result);

}
});

btn3.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
int num1 = Integer.parseInt(edt1.getText().toString());
int num2 = Integer.parseInt(edt2.getText().toString());

String str1 = edt1.getText().toString();
int num1 = Integer.parseInt(str1);

String str2 = edt2.getText().toString();
int num2 = Integer.parseInt(str2);

result = num1 * num2;
int result = num1 * num2;
displayOutput(result);
}
});

btn4.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
int num1 = Integer.parseInt(edt1.getText().toString());
int num2 = Integer.parseInt(edt2.getText().toString());

String str1 = edt1.getText().toString();
int num1 = Integer.parseInt(str1);

String str2 = edt2.getText().toString();
int num2 = Integer.parseInt(str2);

result = num1 / num2;

}
});

btn5.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {

displayOutput(result);

int result = num1 / num2;
displayOutput(result);
}
});
}


private void displayOutput(int result){
tv1.setText(result);
private void displayOutput(int result) {
tv1.setText(Integer.toString(result));
}

}
5 changes: 3 additions & 2 deletions app/src/main/res/layout/activity_main.xml
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@
android:layout_marginTop="8dp"
android:layout_marginEnd="16dp"
android:layout_marginBottom="8dp"
android:inputType="number"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toStartOf="@+id/textView"
app:layout_constraintHorizontal_bias="0.216"
Expand All @@ -49,7 +50,7 @@
android:layout_marginBottom="8dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"

android:inputType="number"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.307" />

Expand Down Expand Up @@ -149,7 +150,7 @@
android:layout_marginEnd="8dp"
android:layout_marginBottom="8dp"
android:text="0"
android:textSize="30dp"
android:textSize="30sp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
Expand Down