- What is Hungarian notation?
Ans: 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.
Usually hungarian notation is used for Java variable names.
- Why should literal strings not be displayed on an app layout? What is the alternative?
Ans: literal strings make the app code less flexible. A better alternative is to define strings
in the strings.xml resource file and reference them in the Java source code.
- 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"
From a Java activity file:
String s = getString(R.string.greeting);
- List some plausable attributes, methods, and events for a BasketballPlayer class.
Attribute is another name for instance variable. Ans:
Attributes | Methods | Events |
height | shoot | Hear ref's whistle |
weight | dribble | Hear buzzer |
name | run | See other player become open |
jerseyNumber | pass | Player sees that he is open |
isRookie | stop | See incoming ball |
- List some plausable attributes, methods, and events for a TextView class. Ans:
Attributes | Methods | Events |
id | move | click |
text | resize | longClick |
textColor | setFocus | swipe |
backgroundColor | | gotFocus |
margin | | lostFocus |
- What is an event handler? How do you set one up?
Ans: an event handler is a method that executes when an event occurs. Suppose that the event handler in the
Java Activity file is defined like this:
public void myEventHandler(View view) {
// Body of event handler goes here.
}
Then in the layout file, the event handler is connected to a buttons click event like this:
androidandroid:onClick="myEventHandler"
- In an event handler, how can you access and change the text that is displayed in a
TextView control?. Ans:
// Access the text:
String s = txtValue.getText( );
// Change the text:
txtValue.setText(s);