fahad005

snackbar action (kotlin)

Oct 26th, 2021
917
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. package com.example.firstapplication
  2.  
  3. import androidx.appcompat.app.AppCompatActivity
  4. import android.os.Bundle
  5. import android.widget.Button
  6. import androidx.coordinatorlayout.widget.CoordinatorLayout
  7. import com.google.android.material.snackbar.Snackbar
  8.  
  9. class MainActivity : AppCompatActivity() {
  10.  
  11.     override fun onCreate(savedInstanceState: Bundle?) {
  12.         super.onCreate(savedInstanceState)
  13.         setContentView(R.layout.activity_main)
  14.  
  15.         // this part
  16.         val btn = findViewById<Button>(R.id.button) // getting reference of button
  17.         btn.setOnClickListener{
  18.             // getting reference of layout
  19.             val coordinatorLayout = findViewById<CoordinatorLayout>(R.id.coordinatorLayout)
  20.             // setting snackbar
  21.             val snackbar = Snackbar.make(coordinatorLayout, "This is snackbar!", Snackbar.LENGTH_SHORT)
  22.             // adding a button(Undo) in snackbar and setting action if it is clicked
  23.             snackbar.setAction("Undo") {
  24.                 // showing another snackbar if the button of previous snackbar is clicked
  25.                 Snackbar.make(coordinatorLayout, "Undo action", Snackbar.LENGTH_SHORT).show()
  26.             }
  27.             // showing the snackbar
  28.             snackbar.show()
  29.         }
  30.     }
  31. }
RAW Paste Data