Compare commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
4e44a1f8b0 | ||
|
|
88e1f88a14 | ||
|
|
b4752dabc7 |
@@ -4,7 +4,9 @@ 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 android.widget.Toast;
|
||||
|
||||
import androidx.activity.EdgeToEdge;
|
||||
import androidx.appcompat.app.AppCompatActivity;
|
||||
@@ -27,53 +29,41 @@ public class MainActivity extends AppCompatActivity {
|
||||
return insets;
|
||||
});
|
||||
|
||||
TextInputLayout tempInput = findViewById(R.id.inputTemp);
|
||||
Button CtoF = findViewById(R.id.but_cToF);
|
||||
Button FtoC = findViewById(R.id.but_fToC);
|
||||
TextInputLayout tempInput = findViewById(R.id.inputLayoutTemp);
|
||||
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) {
|
||||
assert tempInput.getEditText() != null;
|
||||
tempInput.setError(null);
|
||||
if (tempInput.getEditText() == null) return;
|
||||
String userInput = tempInput.getEditText().getText().toString().trim();
|
||||
if(userInput.isEmpty()) {
|
||||
tempInput.setError("Please enter a temperature");
|
||||
Toast.makeText(MainActivity.this, "Error: No value entered", Toast.LENGTH_SHORT).show();
|
||||
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) {
|
||||
// Handle cases where the input is not a valid decimal number
|
||||
tempInput.setError("Invalid number format");
|
||||
}
|
||||
}
|
||||
});
|
||||
|
||||
FtoC.setOnClickListener(new View.OnClickListener(){
|
||||
|
||||
@Override
|
||||
public void onClick(View v) {
|
||||
assert tempInput.getEditText() != null;
|
||||
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");
|
||||
Toast.makeText(MainActivity.this, "Error: Invalid number or special characters", Toast.LENGTH_SHORT).show();
|
||||
}
|
||||
}
|
||||
});
|
||||
@@ -83,6 +73,10 @@ public class MainActivity extends AppCompatActivity {
|
||||
@Override
|
||||
public void onClick(View v) {
|
||||
result.setText("");
|
||||
if (tempInput.getEditText() != null) {
|
||||
tempInput.getEditText().setText("");
|
||||
}
|
||||
tempInput.setError(null);
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
@@ -3,97 +3,141 @@
|
||||
xmlns:app="http://schemas.android.com/apk/res-auto"
|
||||
xmlns:tools="http://schemas.android.com/tools"
|
||||
android:id="@+id/main"
|
||||
android:layout_width="wrap_content"
|
||||
android:layout_width="match_parent"
|
||||
android:layout_height="match_parent"
|
||||
android:textAlignment="center"
|
||||
tools:context=".MainActivity">
|
||||
|
||||
<TextView
|
||||
android:id="@+id/result"
|
||||
android:layout_width="109dp"
|
||||
android:layout_height="28dp"
|
||||
android:layout_marginStart="4dp"
|
||||
android:layout_marginTop="28dp"
|
||||
android:textSize="20sp"
|
||||
app:layout_constraintStart_toEndOf="@+id/text_conversion"
|
||||
app:layout_constraintTop_toBottomOf="@+id/inputTemp" />
|
||||
|
||||
<TextView
|
||||
android:id="@+id/textView2"
|
||||
android:layout_width="410dp"
|
||||
android:layout_height="44dp"
|
||||
android:layout_width="match_parent"
|
||||
android:layout_height="wrap_content"
|
||||
android:layout_marginTop="16dp"
|
||||
android:text="Temperature Conversion"
|
||||
android:textAlignment="center"
|
||||
android:textSize="24sp"
|
||||
app:layout_constraintTop_toTopOf="parent"
|
||||
tools:layout_editor_absoluteX="8dp" />
|
||||
app:layout_constraintStart_toStartOf="parent"
|
||||
app:layout_constraintEnd_toEndOf="parent" />
|
||||
|
||||
<TextView
|
||||
android:id="@+id/text_Temp"
|
||||
android:layout_width="128dp"
|
||||
android:layout_width="wrap_content"
|
||||
android:layout_height="wrap_content"
|
||||
android:layout_marginStart="8dp"
|
||||
android:layout_marginTop="4dp"
|
||||
android:lineSpacingExtra="10sp"
|
||||
android:layout_marginStart="16dp"
|
||||
android:text="Temperature:"
|
||||
android:textAlignment="center"
|
||||
android:textSize="20sp"
|
||||
android:typeface="sans"
|
||||
app:layout_constraintBottom_toBottomOf="@+id/inputLayoutTemp"
|
||||
app:layout_constraintStart_toStartOf="parent"
|
||||
app:layout_constraintTop_toBottomOf="@+id/textView2" />
|
||||
app:layout_constraintTop_toTopOf="@+id/inputLayoutTemp" />
|
||||
|
||||
<com.google.android.material.textfield.TextInputLayout
|
||||
android:layout_width="408dp"
|
||||
android:id="@+id/inputLayoutTemp"
|
||||
android:layout_width="0dp"
|
||||
android:layout_height="wrap_content"
|
||||
android:layout_marginStart="8dp"
|
||||
android:layout_marginEnd="16dp"
|
||||
app:layout_constraintEnd_toEndOf="parent"
|
||||
tools:layout_editor_absoluteY="44dp">
|
||||
|
||||
</com.google.android.material.textfield.TextInputLayout>
|
||||
|
||||
<com.google.android.material.textfield.TextInputEditText
|
||||
android:id="@+id/inputTemp"
|
||||
android:layout_width="280dp"
|
||||
android:layout_height="41dp"
|
||||
android:hint="Enter Temperature"
|
||||
android:layout_marginTop="16dp"
|
||||
app:hintEnabled="false"
|
||||
app:layout_constraintEnd_toEndOf="parent"
|
||||
app:layout_constraintStart_toEndOf="@+id/text_Temp"
|
||||
app:layout_constraintTop_toBottomOf="@+id/textView2" />
|
||||
app:layout_constraintTop_toBottomOf="@+id/textView2">
|
||||
|
||||
<com.google.android.material.textfield.TextInputEditText
|
||||
android:id="@+id/inputTemp"
|
||||
android:layout_width="match_parent"
|
||||
android:layout_height="wrap_content"
|
||||
android:hint="Enter Temperature"
|
||||
android:textColorHint="#9E9E9E"
|
||||
android:inputType="numberDecimal|numberSigned" />
|
||||
</com.google.android.material.textfield.TextInputLayout>
|
||||
|
||||
<TextView
|
||||
android:id="@+id/text_conversion"
|
||||
android:layout_width="109dp"
|
||||
android:layout_height="28dp"
|
||||
android:layout_marginTop="40dp"
|
||||
android:layout_width="wrap_content"
|
||||
android:layout_height="wrap_content"
|
||||
android:layout_marginStart="16dp"
|
||||
android:layout_marginTop="24dp"
|
||||
android:text="Conversion:"
|
||||
android:textSize="20sp"
|
||||
app:layout_constraintTop_toBottomOf="@+id/text_Temp"
|
||||
tools:layout_editor_absoluteX="4dp" />
|
||||
app:layout_constraintStart_toStartOf="parent"
|
||||
app:layout_constraintTop_toBottomOf="@+id/inputLayoutTemp" />
|
||||
|
||||
<Button
|
||||
android:id="@+id/but_cToF"
|
||||
android:layout_width="409dp"
|
||||
android:layout_height="51dp"
|
||||
<TextView
|
||||
android:id="@+id/result"
|
||||
android:layout_width="0dp"
|
||||
android:layout_height="wrap_content"
|
||||
android:layout_marginStart="8dp"
|
||||
android:layout_marginEnd="16dp"
|
||||
android:textSize="20sp"
|
||||
app:layout_constraintBottom_toBottomOf="@+id/text_conversion"
|
||||
app:layout_constraintEnd_toEndOf="parent"
|
||||
app:layout_constraintStart_toEndOf="@+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="C to F"
|
||||
android:textColorLink="#A09F9F"
|
||||
app:layout_constraintTop_toBottomOf="@+id/text_conversion"
|
||||
tools:layout_editor_absoluteX="34dp" />
|
||||
android:text="Select Conversion:"
|
||||
android:textSize="18sp"
|
||||
app:layout_constraintStart_toStartOf="parent"
|
||||
app:layout_constraintTop_toBottomOf="@+id/text_conversion" />
|
||||
|
||||
<Button
|
||||
android:id="@+id/but_fToC"
|
||||
android:layout_width="409dp"
|
||||
android:layout_height="51dp"
|
||||
android:text="F to C"
|
||||
app:layout_constraintTop_toBottomOf="@+id/but_cToF"
|
||||
tools:layout_editor_absoluteX="0dp" />
|
||||
<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
|
||||
android:id="@+id/but_reset"
|
||||
android:layout_width="409dp"
|
||||
android:layout_height="51dp"
|
||||
android:layout_width="0dp"
|
||||
android:layout_height="wrap_content"
|
||||
android:layout_marginStart="16dp"
|
||||
android:layout_marginTop="24dp"
|
||||
android:layout_marginEnd="8dp"
|
||||
android:backgroundTint="#A9A7A7"
|
||||
android:text="Reset"
|
||||
app:layout_constraintTop_toBottomOf="@+id/but_fToC"
|
||||
tools:layout_editor_absoluteX="-13dp" />
|
||||
android:textColor="#FFFFFF"
|
||||
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_constraintHorizontal_bias="0.5"
|
||||
app:layout_constraintStart_toEndOf="@+id/but_reset"
|
||||
app:layout_constraintTop_toBottomOf="@+id/radioGroup" />
|
||||
|
||||
</androidx.constraintlayout.widget.ConstraintLayout>
|
||||
Reference in New Issue
Block a user