- What XML attributes do you know for the LinearLayout and
TextView views? Answer:
background layout_width layout_height text textColor
textSize orientation gravity layout_margin
layout_marginLeft layout_marginTop layout_marginBottom
layout_marginRight id
- How does an Android app know which XML layout file to use when starting the app?
Answer: the statement
setContentView(R.layout.activity_main);
which is Line 6 of the onCreate method of
MainActivity.java.
- Where are the layout, activity, and resource files located for an Android app named
TextDisplay?
Answer:
Full path for activity_main.xml:
TextDisplay/app/src/main/res/layout/activity_main.xml
Full path for
MainActivity.java:
TextDisplay/app/src/main/java/it372/ssmith/textdisplay/ActivityMain.java
Full path for strings.xml:
TextDisplay/app/src/main/res/values/strings.xml
Full path for color.xml:
TextDisplay/app/src/main/res/values/colors.xml
- In this layout file, why does line 13 end with />
but line 7 only ends with >?
1 <LinearLayout
2 xmlns:android="http://schemas.android.com/apk/res/android"
3 xmlns:tools="http://schemas.android.com/tools"
4 android:layout_width="match_parent"
5 android:layout_height="match_parent"
6 android:gravity="center"
7 tools:context=".MainActivity">
8
9 <TextView
11 android:layout_width="wrap_content"
12 android:layout_height="wrap_content"
13 android:text="@string/greeting" />
14 </LinearLayout>
Answer: the TextView element does not contain any child
nodes, so it can be written as a combination start/end node
terminated with />. However, the
LinearLayout element does contain the TextView node so it
cannot be written as a combination start/end tag. The
</LinearLayout> tag terminates the linear layout after the
TextView tag.
- What is Hungarian notation?
Answer: It is when the name of a widget (Android name for a control) has a 3 or 4
letter prefix that indicates the widget type, for example txt for TextView, btn
for button, spnr for Spinner. Often hungarian notation is used for Java variable names,
or the ids if widgets in the layout file.
Hungarian notation was advocated by Charles Simonyi, who was the chief software architect
at Microsoft in the early days.
- Use an object from the Random class to output a random integer between
0 and n - 1? Answer:
import java.util.Random;
---------------------------------
Random r = new Random( );
int randomValue = r.nextInt(n);
System.out.println(n);
- Implement an Android app named ClickMe with a Button widget having text
"Click Me". When the
button is clicked, the button text changes to
"I've been clicked."
Source Code Files:
strings.xml
colors.xml
activity_main.xml
MainActivity.java
- Create an app named TriColor that consists of a linear layout with blue background and no wigits.
Set up an event handler, so that each time the linear
layout is clicked, the background color changes blue to red, red to lime, or
lime to blue.
Answer: Use an int global instance variable in the
MainActivity class to keep track of the background
color. Use the codes Red=0, Green=1, and Blue=2.
Source Code Files:
colors.xml
activity_main.xml
MainActivity.java