To Lecture Notes

IT 372 -- Apr 15, 2019

Review Questions

  1. How does an Android App know which startup activity file to use?
    Ans: The startup activity has these tags between <activity> and </activity>:
    <intent-filter>
        <action android:name="android.intent.action.MAIN" />
        <category android:name="android.intent.category.LAUNCHER" />
    </intent-filter>
    
  2. How does an Android App know which layout to use for the startup app.
    Ans: The following method call is included in the onClick event handler:
    setContentView(R.layout.activity_main);
    
  3. What does the gravity attribute do?
    Ans: It determines how the contents of the view are positioned within the view. Contrast this with the layout_gravity, which determines now the view is positioned within its parent.
  4. What is wrong with this linear layout?
    <!-- Main layout file -->
    <LinearLayout
        xmlns:android=
            "http://schemas.android.com/apk/res/android"
        xmlns:tools=
            "http://schemas.android.com/tools"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:padding="50dp"
        android:orientation="vertical"
        tools:content=".MainActivity">
    
    </LinearLayout>
    
    Ans: The validation header at the top is missing:
    <?xml version="1.0" encoding="utf-8"?>
    
    The validation header must be the first tag in the XML file. All other tags, even comments, must come after it.
  5. Set up a linear layout having horizontal orientation that contains two nested linear layouts each having vertical orientation. Each nested layout should have three buttons. The button texts are Button1, Button2, ... , Button 6
    Ans: See the Buttons Example (to be posted soon).

Extended Color Names

Setting the Background Color

  1. We know how to set the background color in the layout file:
    android:background="ffffd0"
    
    or
    android:background="@color/color1"
    
  2. To set or change the background color in the Java code, first obtain a View object for the layout or widget for which you wish to set the background color:
    LinearLayout layout =
        (LinearLayout) findViewById(R.id.main_layout);
    
    or
    Button btnSubmit =
        (Button) findViewById(R.id.submit);
    
    Then call the View setBackground method:
    layout.setBackgroundColor(Color.GREEN);
    
    or
    layout.setBackgroundColor(Color.parseColor("#ffffe0"));
    
    or
    layout.setBackgroundColor(getResources( ).
        getColor(R.color.color1));
    
  3. Practice Problem 1: Create an Android app with a button. When the button is pressed repeatedly, the background color of the linear layout cycles through three colors or your choice.

FontSizes Example

Future Example