247
CHAPTER 9: User Interface
menuInflater.inflate(popup, menu)
show()
}
}
As usual, you also need to define the menu inside the res/menu resources. For the example,
it’s a file called popup.xml (as shown in the first argument to inflate(...)).
Progress Bars
Showing progress bars is a good way to improve the user experience
for tasks that are
taking a couple of seconds. To implement progress bars, add
a ProgressBar view to your
layout. It doesn’t matter whether you do that inside the XML layout file or from inside the
Kotlin program. In XML you write the following:
android:id="@+id/progressBar"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
/>
for an indeterminate bar (if you can’t tell the progress as a percentage value) or the following
for a determinate progress bar (you know the percentage while updating the progress bar).
android:id="@+id/progressBar"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:progress="0"
/>
Inside the code you toggle the visibility of an indeterminate progress bar by using this:
progressBar.visibility = View.INVISIBLE
// or .. = View.VISIBLE
To set the progress value
for a determinate progress bar, see the following example:
with(progressBar) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O)
min = 0
max = 100
}
var value = 0
Thread {
while(value < 100) {
value += 1
Thread.sleep(200)
runOnUiThread {