UI: Refactored Part2 to use RadioButtons and a single Calculate button

This commit is contained in:
Rapturate
2026-08-28 10:47:01 -04:00
parent b4752dabc7
commit 88e1f88a14
2 changed files with 73 additions and 76 deletions
@@ -4,6 +4,7 @@ import android.annotation.SuppressLint;
import android.os.Bundle; import android.os.Bundle;
import android.view.View; import android.view.View;
import android.widget.Button; import android.widget.Button;
import android.widget.RadioButton;
import android.widget.TextView; import android.widget.TextView;
import androidx.activity.EdgeToEdge; import androidx.activity.EdgeToEdge;
@@ -28,12 +29,12 @@ public class MainActivity extends AppCompatActivity {
}); });
TextInputLayout tempInput = findViewById(R.id.inputLayoutTemp); TextInputLayout tempInput = findViewById(R.id.inputLayoutTemp);
Button CtoF = findViewById(R.id.but_cToF); RadioButton radCtoF = findViewById(R.id.radBut_CtoF);
Button FtoC = findViewById(R.id.but_fToC); Button calculate = findViewById(R.id.but_calc);
Button reset = findViewById(R.id.but_reset); Button reset = findViewById(R.id.but_reset);
TextView result = findViewById(R.id.result); TextView result = findViewById(R.id.result);
CtoF.setOnClickListener(new View.OnClickListener(){ calculate.setOnClickListener(new View.OnClickListener(){
@Override @Override
public void onClick(View v) { public void onClick(View v) {
@@ -45,33 +46,18 @@ public class MainActivity extends AppCompatActivity {
return; return;
} }
try { try {
double c = Double.parseDouble(userInput); double inputTemp = Double.parseDouble(userInput);
double f = c * (9.0/5.0) + 32; if (radCtoF.isChecked()) {
// Celsius to Fahrenheit
double f = inputTemp * (9.0/5.0) + 32;
@SuppressLint("DefaultLocale") String resultString = String.format("%.2f °F", f); @SuppressLint("DefaultLocale") String resultString = String.format("%.2f °F", f);
result.setText(resultString); result.setText(resultString);
} else {
} catch (NumberFormatException e) { // Fahrenheit to Celsius
tempInput.setError("Invalid number format"); double c = (5.0/9.0) * (inputTemp - 32);
}
}
});
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); @SuppressLint("DefaultLocale") String resultString = String.format("%.2f °C", c);
result.setText(resultString); result.setText(resultString);
}
} catch (NumberFormatException e) { } catch (NumberFormatException e) {
tempInput.setError("Invalid number format"); tempInput.setError("Invalid number format");
+57 -46
View File
@@ -7,20 +7,6 @@
android:layout_height="match_parent" android:layout_height="match_parent"
tools:context=".MainActivity"> tools:context=".MainActivity">
<Button
android:id="@+id/but_calc"
android:layout_width="153dp"
android:layout_height="39dp"
android:layout_marginStart="32dp"
android:backgroundTint="#A9A7A7"
android:text="Calculate"
android:textColor="#FFFFFF"
app:cornerRadius="0dp"
app:layout_constraintBottom_toBottomOf="@+id/but_reset"
app:layout_constraintStart_toEndOf="@+id/but_reset"
app:layout_constraintTop_toTopOf="@+id/but_reset"
app:layout_constraintVertical_bias="0.0" />
<TextView <TextView
android:id="@+id/textView2" android:id="@+id/textView2"
android:layout_width="match_parent" android:layout_width="match_parent"
@@ -88,45 +74,70 @@
app:layout_constraintStart_toEndOf="@+id/text_conversion" app:layout_constraintStart_toEndOf="@+id/text_conversion"
app:layout_constraintTop_toTopOf="@+id/text_conversion" /> app:layout_constraintTop_toTopOf="@+id/text_conversion" />
<TextView
android:id="@+id/txt_select"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="16dp"
android:layout_marginTop="24dp"
android:text="Select Conversion:"
android:textSize="18sp"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/text_conversion" />
<RadioGroup
android:id="@+id/radioGroup"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="16dp"
android:layout_marginTop="8dp"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/txt_select">
<RadioButton
android:id="@+id/radBut_CtoF"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="C to F"
android:checked="true" />
<RadioButton
android:id="@+id/radBut_FtoC"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="F to C" />
</RadioGroup>
<Button <Button
android:id="@+id/but_reset" android:id="@+id/but_reset"
android:layout_width="153dp" android:layout_width="0dp"
android:layout_height="39dp" android:layout_height="wrap_content"
android:layout_marginStart="36dp" android:layout_marginStart="16dp"
android:layout_marginTop="24dp"
android:layout_marginEnd="8dp"
android:backgroundTint="#A9A7A7" android:backgroundTint="#A9A7A7"
android:text="Reset" android:text="Reset"
android:textColor="#FFFFFF" android:textColor="#FFFFFF"
app:cornerRadius="0dp" app:cornerRadius="0dp"
app:layout_constraintEnd_toStartOf="@+id/but_calc"
app:layout_constraintHorizontal_bias="0.5"
app:layout_constraintStart_toStartOf="parent" app:layout_constraintStart_toStartOf="parent"
tools:layout_editor_absoluteY="307dp" /> app:layout_constraintTop_toBottomOf="@+id/radioGroup" />
<RadioButton <Button
android:id="@+id/radBut_CtoF" android:id="@+id/but_calc"
style="@style/Widget.Material3.CompoundButton.RadioButton" android:layout_width="0dp"
android:layout_width="75dp" android:layout_height="wrap_content"
android:layout_height="34dp" android:layout_marginStart="8dp"
android:text="C to F" android:layout_marginTop="24dp"
tools:layout_editor_absoluteX="26dp" android:layout_marginEnd="16dp"
tools:layout_editor_absoluteY="216dp" /> android:backgroundTint="#A9A7A7"
android:text="Calculate"
<RadioButton android:textColor="#FFFFFF"
android:id="@+id/radBut_FtoC" app:cornerRadius="0dp"
style="@style/Widget.Material3.CompoundButton.RadioButton" app:layout_constraintEnd_toEndOf="parent"
android:layout_width="75dp" app:layout_constraintHorizontal_bias="0.5"
android:layout_height="34dp" app:layout_constraintStart_toEndOf="@+id/but_reset"
android:text="F to C" app:layout_constraintTop_toBottomOf="@+id/radioGroup" />
app:layout_constraintTop_toBottomOf="@+id/radBut_CtoF"
tools:layout_editor_absoluteX="26dp" />
<TextView
android:id="@+id/txt_select"
android:layout_width="151dp"
android:layout_height="29dp"
android:layout_marginTop="8dp"
android:gravity="bottom"
android:text="Select Conversion:"
android:textSize="15dp"
app:layout_constraintTop_toBottomOf="@+id/text_conversion"
tools:layout_editor_absoluteX="16dp" />
</androidx.constraintlayout.widget.ConstraintLayout> </androidx.constraintlayout.widget.ConstraintLayout>