- What is an Android activity?
Ans: It is a page of an app, as defined by the layout, together with the associated Java (or Kotlin) file.
- What are the units dp and sp?
Ans:
dp means device independent pixels. 1
dp = (1 / 160) in.
sp means scale independent units.
sp is used to measure fonts, dp is used to
measure everything else.
- What are the most common settings for the View attribute
android:layout_width
Ans: "match_parent" and "wrap_content".
match_parent means match the width of the parent; wrap_content means make it just big enough to hold its contents.
- How is a string defined in the strings.xml resource file? Ans:
<string name="greeting">Hello, World!</string>
- How is a string accessed from a resource file? Ans:
From a layout file:
android:text="@string/greeting"
- How is a string accessed from a Java activity file? Ans:
String s = getString(R.string.greeting);
- In the layout file, the id of the EditText widget is celsius;
the id of the TextView widget is fahrenheit. Write an event handler that
reads the Celsius temperature from the EditText widget and writes the
corresponding Fahrenheit temperature into the TextView widget.
If the input is not a number, write NaN into the
TextView widget.
Ans: Here is the version we discussed in class:
public void onClick(View view) {
EditText editTxtCel = (EditText) findViewById(R.id.cel);
TextView txtFahr = (TextView) findViewById(R.id.fahr);
int cel = 0;
boolean errorFlag = false;
String input = editTxtCel.getText( ).toString( );
try {
cel = Integer.parseInt(input);
}
catch(NumberFormatException e) {
errorFlag = true;
}
if (errorFlag) {
int fahr = 9 * cel / 5 + 32;
txtFahr.setText(fahr);
}
else {
txtFahr.setText("NaN");
}
}
- Look at the revised Magic8Ball Example.