Vector
Vector

Reputation: 3235

Kotlin create a snackbar

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

Upvotes: 15

Views: 51294

Answers (7)

Thiago
Thiago

Reputation: 13302

In Kotlin like so

Snackbar.make(binding.root, "My Message", Snackbar.LENGTH_SHORT).show()

Upvotes: 8

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()

Upvotes: 3

Thiago
Thiago

Reputation: 13302

Here is how,

Snackbar.make(requireView(), "Hello World", Snackbar.LENGTH_SHORT).show()

Upvotes: 1

Shailendra Madda
Shailendra Madda

Reputation: 21551

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.

Upvotes: 1

SSB
SSB

Reputation: 1034

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()
}

Upvotes: 26

JCarlosR
JCarlosR

Reputation: 1663

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()
}

Upvotes: 2

Vector
Vector

Reputation: 3235

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

Upvotes: 1

Related Questions