Timer and animationThe Android mini-app frameworkTaps, double-taps, and flingsDialogs


We already met the toast, which displays a (short) message temporarily. There are three other methods that display dialogs similar to the ones described earlier:

However, unlike the cs109ui package, these methods do not wait for the user interaction to finish: On Android, the method will return immediately. To handle the user's response, you need to provide a function object that receives the response as an argument (for askYesNo, this is a Boolean, for inputString this is a String).

The example code shows how this should be used (dialogs.kt):

// Different dialogs

import org.otfried.cs109.Context
import org.otfried.cs109.MiniApp

import org.otfried.cs109.Canvas
import org.otfried.cs109.Color
import org.otfried.cs109.DrawStyle
import org.otfried.cs109.TextAlign

class Main(val ctx: Context) : MiniApp {
  init {
    ctx.setTitle("Dialog demo")
    ctx.onTap { x, y -> tapped(x, y) }

  fun tapped(x: Double, y: Double) {
    if (y < 100)
      ctx.toast("This is a toast!")
    else if (y < 200)
      ctx.showMessage("This is a message.  I can tell you a lot!")
    else if (y < 300)
      ctx.askYesNo("This is a question. Do you know the answer?") {
        answer -> if (answer) ctx.toast("Good!") }
      ctx.inputString("Tell me your name!") {
        s -> ctx.setTitle("Hello $s") }

  override fun onDraw(canvas: Canvas) {
    val x = canvas.width / 2.0
    canvas.clear(Color(255, 255, 192))
    canvas.drawText("Toast", x, 50.0, TextAlign.CENTER)
    canvas.drawText("Message", x, 150.0, TextAlign.CENTER)
    canvas.drawText("Yes or No", x, 250.0, TextAlign.CENTER)
    canvas.drawText("Name", x, 350.0, TextAlign.CENTER)
Timer and animationThe Android mini-app frameworkTaps, double-taps, and flingsDialogs