diff --git a/app/src/main/java/com/example/assignmentone/MainActivity.java b/app/src/main/java/com/example/assignmentone/MainActivity.java
index 1d89d1c..9f8937f 100644
--- a/app/src/main/java/com/example/assignmentone/MainActivity.java
+++ b/app/src/main/java/com/example/assignmentone/MainActivity.java
@@ -4,6 +4,7 @@ import android.annotation.SuppressLint;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
+import android.widget.RadioButton;
import android.widget.TextView;
import androidx.activity.EdgeToEdge;
@@ -28,12 +29,12 @@ public class MainActivity extends AppCompatActivity {
});
TextInputLayout tempInput = findViewById(R.id.inputLayoutTemp);
- Button CtoF = findViewById(R.id.but_cToF);
- Button FtoC = findViewById(R.id.but_fToC);
+ RadioButton radCtoF = findViewById(R.id.radBut_CtoF);
+ Button calculate = findViewById(R.id.but_calc);
Button reset = findViewById(R.id.but_reset);
TextView result = findViewById(R.id.result);
- CtoF.setOnClickListener(new View.OnClickListener(){
+ calculate.setOnClickListener(new View.OnClickListener(){
@Override
public void onClick(View v) {
@@ -45,33 +46,18 @@ public class MainActivity extends AppCompatActivity {
return;
}
try {
- double c = Double.parseDouble(userInput);
- double f = c * (9.0/5.0) + 32;
- @SuppressLint("DefaultLocale") String resultString = String.format("%.2f °F", f);
- result.setText(resultString);
-
- } catch (NumberFormatException e) {
- tempInput.setError("Invalid number format");
- }
- }
- });
-
- FtoC.setOnClickListener(new View.OnClickListener(){
-
- @Override
- public void onClick(View v) {
- tempInput.setError(null);
- if (tempInput.getEditText() == null) return;
- String userInput = tempInput.getEditText().getText().toString().trim();
- if(userInput.isEmpty()) {
- tempInput.setError("Please enter a temperature");
- return;
- }
- try {
- double f = Double.parseDouble(userInput);
- double c = (5.0/9.0)*(f-32);
- @SuppressLint("DefaultLocale") String resultString = String.format("%.2f °C", c);
- result.setText(resultString);
+ double inputTemp = Double.parseDouble(userInput);
+ if (radCtoF.isChecked()) {
+ // Celsius to Fahrenheit
+ double f = inputTemp * (9.0/5.0) + 32;
+ @SuppressLint("DefaultLocale") String resultString = String.format("%.2f °F", f);
+ result.setText(resultString);
+ } else {
+ // Fahrenheit to Celsius
+ double c = (5.0/9.0) * (inputTemp - 32);
+ @SuppressLint("DefaultLocale") String resultString = String.format("%.2f °C", c);
+ result.setText(resultString);
+ }
} catch (NumberFormatException e) {
tempInput.setError("Invalid number format");
diff --git a/app/src/main/res/layout/activity_main.xml b/app/src/main/res/layout/activity_main.xml
index 573cdc9..cd4ac3f 100644
--- a/app/src/main/res/layout/activity_main.xml
+++ b/app/src/main/res/layout/activity_main.xml
@@ -7,20 +7,6 @@
android:layout_height="match_parent"
tools:context=".MainActivity">
-
-
+
+
+
+
+
+
+
+
+
+ app:layout_constraintTop_toBottomOf="@+id/radioGroup" />
-
-
-
-
-
+
\ No newline at end of file