2 Commits
Author SHA1 Message Date
Rapturate 98e6b78e1e Updated Comments 2026-08-28 10:55:24 -04:00
Rapturate 9393a247af UI: Redo styling and logic fixes on Part1 branch 2026-08-28 10:45:18 -04:00
2 changed files with 59 additions and 63 deletions
@@ -1,12 +1,16 @@
/*
Assignment 1 (part1)
Assignment_1
Lew Price
*/
package com.example.assignmentone; package com.example.assignmentone;
import android.annotation.SuppressLint; 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 android.widget.Toast;
import androidx.activity.EdgeToEdge; import androidx.activity.EdgeToEdge;
import androidx.appcompat.app.AppCompatActivity; import androidx.appcompat.app.AppCompatActivity;
@@ -30,12 +34,12 @@ public class MainActivity extends AppCompatActivity {
}); });
TextInputLayout tempInput = findViewById(R.id.inputLayoutTemp); TextInputLayout tempInput = findViewById(R.id.inputLayoutTemp);
RadioButton radCtoF = findViewById(R.id.radBut_CtoF); Button CtoF = findViewById(R.id.but_cToF);
Button calculate = findViewById(R.id.but_calc); Button FtoC = findViewById(R.id.but_fToC);
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);
calculate.setOnClickListener(new View.OnClickListener(){ CtoF.setOnClickListener(new View.OnClickListener(){
@Override @Override
public void onClick(View v) { public void onClick(View v) {
@@ -44,26 +48,39 @@ public class MainActivity extends AppCompatActivity {
String userInput = tempInput.getEditText().getText().toString().trim(); String userInput = tempInput.getEditText().getText().toString().trim();
if(userInput.isEmpty()) { if(userInput.isEmpty()) {
tempInput.setError("Please enter a temperature"); tempInput.setError("Please enter a temperature");
Toast.makeText(MainActivity.this, "Error: No value entered", Toast.LENGTH_SHORT).show();
return; return;
} }
try { try {
double inputTemp = Double.parseDouble(userInput); double c = Double.parseDouble(userInput);
if (radCtoF.isChecked()) { double f = c * (9.0/5.0) + 32;
// 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 {
// 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) { } catch (NumberFormatException e) {
tempInput.setError("Invalid number format"); tempInput.setError("Invalid number format");
Toast.makeText(MainActivity.this, "Error: Invalid number or special characters", Toast.LENGTH_SHORT).show(); }
}
});
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);
} catch (NumberFormatException e) {
tempInput.setError("Invalid number format");
} }
} }
}); });
+23 -44
View File
@@ -74,70 +74,49 @@
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 <Button
android:id="@+id/txt_select" android:id="@+id/but_cToF"
android:layout_width="wrap_content" android:layout_width="0dp"
android:layout_height="wrap_content" android:layout_height="wrap_content"
android:layout_marginStart="16dp" android:layout_marginStart="16dp"
android:layout_marginTop="24dp" android:layout_marginTop="24dp"
android:text="Select Conversion:" android:layout_marginEnd="16dp"
android:textSize="18sp" android:backgroundTint="#A9A7A7"
android:text="C to F"
android:textColor="#FFFFFF"
app:cornerRadius="0dp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent" app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/text_conversion" /> app:layout_constraintTop_toBottomOf="@+id/text_conversion" />
<RadioGroup <Button
android:id="@+id/radioGroup" android:id="@+id/but_fToC"
android:layout_width="wrap_content" android:layout_width="0dp"
android:layout_height="wrap_content" android:layout_height="wrap_content"
android:layout_marginStart="16dp" android:layout_marginStart="16dp"
android:layout_marginEnd="16dp"
android:layout_marginTop="8dp" android:layout_marginTop="8dp"
android:backgroundTint="#A9A7A7"
android:text="F to C"
android:textColor="#FFFFFF"
app:cornerRadius="0dp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent" app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/txt_select"> app:layout_constraintTop_toBottomOf="@+id/but_cToF" />
<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="0dp" android:layout_width="0dp"
android:layout_height="wrap_content" android:layout_height="wrap_content"
android:layout_marginStart="16dp" android:layout_marginStart="16dp"
android:layout_marginTop="24dp" android:layout_marginEnd="16dp"
android:layout_marginEnd="8dp" android:layout_marginTop="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_constraintTop_toBottomOf="@+id/radioGroup" />
<Button
android:id="@+id/but_calc"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginStart="8dp"
android:layout_marginTop="24dp"
android:layout_marginEnd="16dp"
android:backgroundTint="#A9A7A7"
android:text="Calculate"
android:textColor="#FFFFFF"
app:cornerRadius="0dp"
app:layout_constraintEnd_toEndOf="parent" app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.5" app:layout_constraintStart_toStartOf="parent"
app:layout_constraintStart_toEndOf="@+id/but_reset" app:layout_constraintTop_toBottomOf="@+id/but_fToC" />
app:layout_constraintTop_toBottomOf="@+id/radioGroup" />
</androidx.constraintlayout.widget.ConstraintLayout> </androidx.constraintlayout.widget.ConstraintLayout>