Mobile App Dev 2021W: Tutorial 5: Difference between revisions
Line 9: | Line 9: | ||
# Change the format of the output Celsius to have four significant digits rather than a fixed two decimal places. | # Change the format of the output Celsius to have four significant digits rather than a fixed two decimal places. | ||
# Add a line below the Celsius conversion that gives the temperature in Kelvin. | # Add a line below the Celsius conversion that gives the temperature in Kelvin. | ||
# Make the temperatures bold only (the rest of the text should be as it was). Use [https://developer.android.com/reference/android/text/Html#fromHtml(java.lang.String,%20int) fromHtml] and the HTML bold tag b, e.g., "<b>32</b>". | # Make the temperatures bold only (the rest of the text should be as it was). Use [https://developer.android.com/reference/android/text/Html#fromHtml(java.lang.String,%20int) fromHtml] and the HTML bold tag b, e.g., "<b>32</b>". Use 0 for options. | ||
# Add a miles to kilometers converter to the same screen. It should be below the existing Fahrenheit converter. The input for miles and Fahrenheit should be separate. | # Add a miles to kilometers converter to the same screen. It should be below the existing Fahrenheit converter. The input for miles and Fahrenheit should be separate. | ||
# What are the keywords used in the MainActivity class? What are the explicit types? What are the implicit types? There are lots, so don't worry if you can't get them all. However, try to identify as many of them as you can. | # What are the keywords used in the MainActivity class? What are the explicit types? What are the implicit types? There are lots, so don't worry if you can't get them all. However, try to identify as many of them as you can. |
Revision as of 05:00, 28 February 2021
This tutorial is in development.
Tasks
For each task, explain what you did to accomplish the task or explain your attempts at an answer. Be sure to include references (links) to any resources that you used for each question, as well as the names of any collaborators.
Be sure to use this template when submitting answers on cuLearn. When a question asks for code, explain the changes you made and include relevant code snippets.
- Change the format of the output Celsius to have four significant digits rather than a fixed two decimal places.
- Add a line below the Celsius conversion that gives the temperature in Kelvin.
- Make the temperatures bold only (the rest of the text should be as it was). Use fromHtml and the HTML bold tag b, e.g., "<b>32</b>". Use 0 for options.
- Add a miles to kilometers converter to the same screen. It should be below the existing Fahrenheit converter. The input for miles and Fahrenheit should be separate.
- What are the keywords used in the MainActivity class? What are the explicit types? What are the implicit types? There are lots, so don't worry if you can't get them all. However, try to identify as many of them as you can.
- Out of the ones you identify, which keywords and types seem mysterious?
Code
app/java/carleton.comp1601.converter_1a/MainActivity
package carleton.comp1601.converter_1a
import android.os.Bundle
import android.text.Editable
import android.text.TextWatcher
import android.widget.EditText
import android.widget.TextView
import androidx.appcompat.app.AppCompatActivity
class MainActivity : AppCompatActivity() {
private lateinit var editTemperature: EditText
private lateinit var result: TextView
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
result = findViewById(R.id.result)
editTemperature = findViewById(R.id.editTemperature)
editTemperature.addTextChangedListener(watcher)
}
private val watcher = object : TextWatcher {
override fun afterTextChanged(s: Editable) {
val Fs = editTemperature.getText().toString()
var F = Fs.toDoubleOrNull()
if (F == null) {
result.setText("Please enter a number to convert.")
} else {
val C = (F - 32.0) * (5.0 / 9.0)
result.setText(String.format("%s Farenheit is %.2f Celsius", Fs, C))
}
}
override fun beforeTextChanged(s: CharSequence?, start: Int, count: Int, after: Int) {
}
override fun onTextChanged(s: CharSequence?, start: Int, before: Int, count: Int) {
}
}
}
app/res/layout/activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<LinearLayout
android:orientation="vertical"
android:padding="10sp"
android:gravity="center"
android:layout_width="match_parent"
android:layout_height="match_parent">
<EditText
android:id="@+id/editTemperature"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:ems="10"
android:gravity="center"
android:hint="Enter Farenheit"
android:inputType="numberSigned" />
<TextView
android:id="@+id/result"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/message"
android:textAppearance="@style/TextAppearance.AppCompat.Large" />
</LinearLayout>
</androidx.constraintlayout.widget.ConstraintLayout>
app/res/values/strings.xml
<resources>
<string name="app_name">Temperature Converter</string>
<string name="message">Enter a number please</string>
</resources>