Follow Android 007, get a full set of Free Android development learning materials

What is a Toast

Toast is a UI control that displays prompt messages for a short time and automatically displays them. The default is usually displayed at the bottom of the screen.

Based on the sample

  1. Display default toast
  • Effect:

  • Code:
Toast.makeText(this, text, Toast.LENGTH_SHORT).show()
Copy the code
  1. Display toast at the top
  • Effect:

  • Code:
private fun showToastOnTop(text: String) {
    val toast = Toast.makeText(this, text, Toast.LENGTH_SHORT)
    toast.setGravity(Gravity.TOP, 0.0)
    toast.show()
}
Copy the code
  1. Center the toast
  • Effect:

  • Code:
private fun showToastAtCenter(text: String) {
    val toast = Toast.makeText(this, text, Toast.LENGTH_SHORT)
    toast.setGravity(Gravity.CENTER, 0.0)
    toast.show()
}
Copy the code

MakeText Parameter description

parameter use
The first parameter Pass in a Context object, usually the Activity Context, or the Application Context
Second parameter The string to display
The third parameter Optional values: toast.length_short (displays 2 seconds), toast.length_long (displays 3.5 seconds)

Complete source code

Gitee.com/cxyzy1/toas…


Android development tutorial series summary

Development language learning

Kotlin language basics

UI control learning series

UI control _TextView UI control _EditText UI control _Button UI control _ImageView UI control _RadioButton UI control _CheckBox UI control _ProgressBar

Follow the headlines to get the latest articles: