To Lecture Notes

IT 372 -- Apr 10, 2019

Review Questions

  1. 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.
  2. 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.
  3. 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.
  4. How is a string defined in the strings.xml resource file? Ans:
    <string name="greeting">Hello, World!</string>
    
  5. How is a string accessed from a resource file? Ans:
    From a layout file:
    android:text="@string/greeting"
    
  6. How is a string accessed from a Java activity file? Ans:
    String s = getString(R.string.greeting);
    
  7. 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");
        }
    }
    
  8. Look at the revised Magic8Ball Example.

BeerAdvisor Example