- With which Modifier elements are you familier?
Check out this reference on JPC modifiers:
developer.android.com/develop/ui/compose/modifiers
Answer: Here are some of the Modifier elements that we have used in the examples:
.padding(all=30.dp)
.size(width=200.dp, height=80.dp)
.fillMaxSize(
.fillMaxHeight( )
.fillMaxWidth( )
.background(Color.White)
.border( ... )
.clip( ... )
.shadow( ... )
.clickable( { /* action */} )
.scrollable(scrollState)
- What does this statement do?
var textDisplay = remember { mutableStateOf("") }
Answer: textDisplay is a state variable, which causes JPC elements that use
this variable to be recomposed whenever the state variable is changed. You
use this state variable getter and setter like this:
text = textDisplay.value; // <-- Sample getter statement.
textDisplay.value = it; // <-- Sample setter statement.
- What is the difference between the preceding statement and this one?
var textDisplay by remember { mutableStateOf("") }
Answer: The by keyword makes it not necessary to use
textDisplay.value. These statements
can be used instead:
text = textDisplay; // <-- Sample getter statement.
textDisplay = it; // <-- Sample setter statement.
- How do state variables work in JPC?
Answer: a state variable is defined as in Exercise 2 or 3.
They are used to set what is displayed in a JPC element.
Whenever a state variable is changed, any elements that use
its value are recomposed, which means that they are redisplayed.
- What is trailing lambda notation for functions?
Answer: trailing lambda notation means that if the last parameter
of a function is a lambda function, it need not be included within the
parentheses of the function call.
// Traditional notation:
f(param1 = p1, param2 = p2, param3 = { /* lambda function */ })
// Trailing lambda notation:
f(param1 = p1, param2 = p2) {
/* lambda function */
}
- What does the Kotlin keyword let do? Answer -- consider these statements from
the InToCm Example:
valueCm.value = "NaN"
valueInch.value.toDoubleOrNull( )?.let {
valueCm.value = String.format("%.2f", it * 2.54)
}
If the statement in the let lambda function would cause an exception
it is simply not executed so that valueCm.value retains its
"NaN" value.
- How do you draw lines, circles, and rectangles on a Canvas
element? Answer -- some examples:
drawLine(color = Color(0xFFFF0000),
start = Offset(20f, 20f),
end = Offset(50f, 100f),
strokeWidth = 5f)
drawCircle(color : Color;
radius : 100f;
center : Offset(150f, 250f)
drawRect(color : Color.Green;
topLeft : Offset(50f, 80f),
size : Offset(200f, 150f)
}