diff --git a/Android - Assignment 1.pdf b/Android - Assignment 1.pdf new file mode 100644 index 0000000..22bd72e Binary files /dev/null and b/Android - Assignment 1.pdf differ diff --git a/app/.gitignore b/app/.gitignore new file mode 100644 index 0000000..42afabf --- /dev/null +++ b/app/.gitignore @@ -0,0 +1 @@ +/build \ No newline at end of file diff --git a/app/build.gradle.kts b/app/build.gradle.kts new file mode 100644 index 0000000..f86caae --- /dev/null +++ b/app/build.gradle.kts @@ -0,0 +1,42 @@ +plugins { + alias(libs.plugins.android.application) +} + +android { + namespace = "com.example.assignmentone" + compileSdk { + version = release(37) + } + + defaultConfig { + applicationId = "com.example.assignmentone" + minSdk = 34 + targetSdk = 37 + versionCode = 1 + versionName = "1.0" + + testInstrumentationRunner = "androidx.test.runner.AndroidJUnitRunner" + } + + buildTypes { + release { + optimization { + enable = false + } + } + } + compileOptions { + sourceCompatibility = JavaVersion.VERSION_11 + targetCompatibility = JavaVersion.VERSION_11 + } +} + +dependencies { + implementation(libs.activity.ktx) + implementation(libs.appcompat) + implementation(libs.constraintlayout) + implementation(libs.material) + testImplementation(libs.junit) + androidTestImplementation(libs.espresso.core) + androidTestImplementation(libs.ext.junit) +} \ No newline at end of file diff --git a/app/src/androidTest/java/com/example/assignmentone/ExampleInstrumentedTest.java b/app/src/androidTest/java/com/example/assignmentone/ExampleInstrumentedTest.java new file mode 100644 index 0000000..7856735 --- /dev/null +++ b/app/src/androidTest/java/com/example/assignmentone/ExampleInstrumentedTest.java @@ -0,0 +1,26 @@ +package com.example.assignmentone; + +import android.content.Context; + +import androidx.test.platform.app.InstrumentationRegistry; +import androidx.test.ext.junit.runners.AndroidJUnit4; + +import org.junit.Test; +import org.junit.runner.RunWith; + +import static org.junit.Assert.*; + +/** + * Instrumented test, which will execute on an Android device. + * + * @see Testing documentation + */ +@RunWith(AndroidJUnit4.class) +public class ExampleInstrumentedTest { + @Test + public void useAppContext() { + // Context of the app under test. + Context appContext = InstrumentationRegistry.getInstrumentation().getTargetContext(); + assertEquals("com.example.assignmentone", appContext.getPackageName()); + } +} \ No newline at end of file diff --git a/app/src/main/AndroidManifest.xml b/app/src/main/AndroidManifest.xml new file mode 100644 index 0000000..48283e9 --- /dev/null +++ b/app/src/main/AndroidManifest.xml @@ -0,0 +1,26 @@ + + + + + + + + + + + + + + \ No newline at end of file diff --git a/app/src/main/java/com/example/assignmentone/MainActivity.java b/app/src/main/java/com/example/assignmentone/MainActivity.java new file mode 100644 index 0000000..f58b232 --- /dev/null +++ b/app/src/main/java/com/example/assignmentone/MainActivity.java @@ -0,0 +1,90 @@ +package com.example.assignmentone; + +import android.annotation.SuppressLint; +import android.os.Bundle; +import android.view.View; +import android.widget.Button; +import android.widget.TextView; + +import androidx.activity.EdgeToEdge; +import androidx.appcompat.app.AppCompatActivity; +import androidx.core.graphics.Insets; +import androidx.core.view.ViewCompat; +import androidx.core.view.WindowInsetsCompat; + +import com.google.android.material.textfield.TextInputLayout; + +public class MainActivity extends AppCompatActivity { + + @Override + protected void onCreate(Bundle savedInstanceState) { + super.onCreate(savedInstanceState); + EdgeToEdge.enable(this); + setContentView(R.layout.activity_main); + ViewCompat.setOnApplyWindowInsetsListener(findViewById(R.id.main), (v, insets) -> { + Insets systemBars = insets.getInsets(WindowInsetsCompat.Type.systemBars()); + v.setPadding(systemBars.left, systemBars.top, systemBars.right, systemBars.bottom); + return insets; + }); + + TextInputLayout tempInput = findViewById(R.id.inputTemp); + Button CtoF = findViewById(R.id.but_cToF); + Button FtoC = findViewById(R.id.but_fToC); + Button reset = findViewById(R.id.but_reset); + TextView result = findViewById(R.id.result); + + CtoF.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 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); + + } catch (NumberFormatException e) { + tempInput.setError("Invalid number format"); + } + } + }); + + reset.setOnClickListener(new View.OnClickListener(){ + + @Override + public void onClick(View v) { + result.setText(""); + } + }); + } + +} \ No newline at end of file diff --git a/app/src/main/keepRules/rules.keep b/app/src/main/keepRules/rules.keep new file mode 100644 index 0000000..d7e081a --- /dev/null +++ b/app/src/main/keepRules/rules.keep @@ -0,0 +1,12 @@ +# Add project specific R8 rules here. +# AGP will combine all keep rule files in src/main/keepRules to pass to R8 +# +# For more details, see +# https://d.android.com/r/tools/r8/keep-rules + +# If your project uses WebView with JS, uncomment the following +# and specify the fully qualified class name to the JavaScript interface +# class: +#-keepclassmembers class fqcn.of.javascript.interface.for.webview { +# public *; +#} \ No newline at end of file diff --git a/app/src/main/res/drawable/ic_launcher_background.xml b/app/src/main/res/drawable/ic_launcher_background.xml new file mode 100644 index 0000000..07d5da9 --- /dev/null +++ b/app/src/main/res/drawable/ic_launcher_background.xml @@ -0,0 +1,170 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/app/src/main/res/drawable/ic_launcher_foreground.xml b/app/src/main/res/drawable/ic_launcher_foreground.xml new file mode 100644 index 0000000..2b068d1 --- /dev/null +++ b/app/src/main/res/drawable/ic_launcher_foreground.xml @@ -0,0 +1,30 @@ + + + + + + + + + + + \ No newline at end of file diff --git a/app/src/main/res/layout/activity_main.xml b/app/src/main/res/layout/activity_main.xml new file mode 100644 index 0000000..c2e2271 --- /dev/null +++ b/app/src/main/res/layout/activity_main.xml @@ -0,0 +1,99 @@ + + + + + + + + + + + + + + + + + +