To fix the “Val cannot be reassigned” error, you need to create a new Toast object with the required gravity and duration. Here’s the updated code:
val toast = Toast.makeText(
this,
"Toast at top!",
Toast.LENGTH_SHORT
)
toast.setGravity(Gravity.TOP, 0, 0)
toast.show()
Note that setGravity() is used instead of directly modifying the gravity property, and it takes two additional arguments for x and y offsets, which can be set to 0 in this case.