Snackbars
provide lightweight feedback about an operation. They show a brief message
at the bottom of the screen on mobile and lower left on larger devices.
Snackbars appear above all other elements on screen and only one can be
displayed at a time.
They automatically disappear after a timeout
or after user interaction elsewhere on the screen, particularly after
interactions that summon a new surface or activity. Snackbars can be swiped
off screen.
It's a simplest example to show Snackbar using Kotlin. The
layout xml is same as last post "Simple example of Button and Toast".
MainActivity.kt
package android_er.blogspot.com.myapp01
import android.os.Bundle
import android.view.View
import android.widget.Button
import android.widget.Toast
import androidx.appcompat.app.AppCompatActivity
import com.google.android.material.snackbar.Snackbar
private lateinit var btnA: Button
private lateinit var btnB: Button
class MainActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
btnA = findViewById(R.id.btn_A)
btnB = findViewById(R.id.btn_B)
btnA.setOnClickListener(
{ view: View ->
Toast.makeText(
this,
"Button A pressed",
Toast.LENGTH_LONG,
).show()
},
)
btnB.setOnClickListener(
{ view: View ->
val snackbar = Snackbar
.make(btnB,
"android-er.blogspot.com",
Snackbar.LENGTH_LONG)
snackbar.show()
},
)
}
}