Kotlin create a snackbar
Asked Answered
H

7

14

We are trying to create a Snackbar. The code from a Java app was converted using the Java to Kotlin converter in Android Studio. Next we looked at various examples on many different websites and even tried to implement the code from the book Kotlin Programming Cookbook. We will post all our non working examples below. Our question is how to create the proper syntax to show a Snackbar? We would like to click a btnSNACK with a onClick=onSNACK to show the Snackbar

This is our Java to Kotlin converter code we would really like to use this one

    fun onSNACK(view: View){
    //Snackbar(view)
    //val snackbar = Snackbar(view, "Permission Granted", Snackbar.LENGTH_LONG).setAction("Action", null).show()
        snackbar.make(view, "Replace with your own action", 
        snackbar.LENGTH_LONG).setAction("Action", null).show()    
        snackbar.setActionTextColor(Color.BLUE)
        val snackbarView = snackbar.getView()
        snackbarView.setBackgroundColor(Color.LTGRAY)
        val textView = 
        snackbarView.findViewById(android.support.design.R.id.snackbar_text)
        textView.setTextColor(Color.BLUE)
        textView.setTextSize(28f)
        snackbar.show()
}

Next Try was with this code

    class Snackbar{
    object LENGTH_LONG {
    }
    fun show() {
    }
}

fun onSNACK(view: View){
    snackbar = Snackbar.make(this, "Welcome to Android Teachers..!!", 
    Snackbar.LENGTH_LONG)
    snackbar.show()
}

Our layout is a RelativeLayout (RL) for the Activity that has the Snackbar

    class Snackbar(view: View?): Any() {
    object LENGTH_SHORT {}

fun View.snack(message: String, length: Int = Toast.LENGTH_LONG, f: Snackbar. 
() -> Unit) {
    val snack = Snackbar.make(this.findViewById(R.id.RL), message, length)
    snack.f()
    snack.show()
}

We thought this would work the first line of code was declared top level

    lateinit var snackbar: Snackbar//top level
fun onSNACK(){
    btnSNACK.setOnClickListener { view ->
        Snackbar.make(view, "Replace with your own action", 
        Snackbar.LENGTH_LONG)
                .setAction("Action", null).show()
    }
}

We used the class Snackbar with and without these various methods. We were able to remove all the red warnings in most of these examples but the work "make" just offers the same suggestion "change variable name" which makes no sense from our Kotlin novice point of view. We do not desire to use Anko plugin We also see no imports that refer to Snackbar Yes we have jetbrains stdlib v7 dependency no design dependency

Handtomouth answered 22/8, 2018 at 17:18 Comment(2)
What error do you get?Fraunhofer
@Fraunhofer The errors were so many I am not sure but unable to instantiate stick in my mink what remains of it I did look at the byte code and not sure how to read it But will explore furtherHandtomouth
P
26

Refer this for more details

and then here's your modified code which will show snack bar

fun onSNACK(view: View){
    //Snackbar(view)
    val snackbar = Snackbar.make(view, "Replace with your own action",
            Snackbar.LENGTH_LONG).setAction("Action", null)
    snackbar.setActionTextColor(Color.BLUE)
    val snackbarView = snackbar.view
    snackbarView.setBackgroundColor(Color.LTGRAY)
    val textView =
            snackbarView.findViewById(com.google.android.material.R.id.snackbar_text) as TextView
    textView.setTextColor(Color.BLUE)
    textView.textSize = 28f
    snackbar.show()
}
Performing answered 22/8, 2018 at 17:31 Comment(2)
Just after posting the question I realized that support design that jetbrains stdlib v7 has no design support Thanks and welcome to stack overflowHandtomouth
For those who want to use material dependency you can use this val textView = snackView.findViewById(com.google.android.material.R.id.snackbar_text) as TextViewWilhelmstrasse
D
8

In Kotlin like so

Snackbar.make(binding.root, "My Message", Snackbar.LENGTH_SHORT).show()
Dishrag answered 25/2, 2021 at 11:6 Comment(0)
K
3

i have a same problem but i use this to solve my problem

     val snack = Snackbar.make(View(this@MainActivity),"This is a simple Snackbar",Snackbar.LENGTH_LONG)
     snack.show()
Kandis answered 9/4, 2021 at 5:33 Comment(1)
This is worked for meGrijalva
H
2

In case someone does not need to customize it too much, you can use it directly, as follows:

view.snack("Your message")

To do so, just define an extension function (preferably in a separated file):

fun View.snack(message: String, duration: Int = Snackbar.LENGTH_LONG) {
    Snackbar.make(this, message, duration).show()
}
Hopeless answered 17/12, 2019 at 21:7 Comment(0)
H
1

We were very happy with @SSB answer. We noticed that we did not have a Action button so the code below provides the addition of a Action button and how to style it seems the snackbar_action in Support Design gets little or no mention on sites that try to provide example code. We are just calling another function when the DISMISS button is clicked.

    fun onSNACK(view: View){

    var AC:String
    AC = "DISMISS"

    val snackbar = Snackbar.make(view, "Click DISMISS to CLOSE", Snackbar.LENGTH_INDEFINITE)
            .setAction(AC,View.OnClickListener {weekDAY(null) })

    snackbar.setActionTextColor(Color.RED)
    val snackbarView = snackbar.view
    snackbarView.setBackgroundColor(Color.LTGRAY)
    val textView = snackbarView.findViewById(android.support.design.R.id.snackbar_text) as TextView
    val actionTextView = snackbarView.findViewById(android.support.design.R.id.snackbar_action)as TextView
    textView.setTextColor(Color.BLUE)
    textView.textSize = 28f
    actionTextView.textSize = 28f

    snackbar.show()
}

The two lines of code at the top were an experiment to see if text in the Snackbar could be set from a EditText. This design would permit a Snackbar that could be called from other functions in the Activity with optional wording

Handtomouth answered 22/8, 2018 at 22:18 Comment(0)
S
1

Simple way to show it in Koltin:

private fun showSnackBar() {
        val snack = Snackbar.make(rootView, "Sample snack bar message", Snackbar.LENGTH_INDEFINITE)
        snack.setAction("Click Me") {
            // TODO when you tap on "Click Me"
        }
        snack.show()
    }

Note: Replace rootView with your view ID present in the layout, and change LENGTH_INDEFINITE to LENGTH_SHORT for a short period of time or LENGTH_LONG to show for a long period of time and dismiss.

Sherborne answered 23/7, 2020 at 8:39 Comment(0)
D
1

Here is how,

Snackbar.make(requireView(), "Hello World", Snackbar.LENGTH_SHORT).show()
Dishrag answered 15/12, 2020 at 20:47 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.