Home » Android Kotlin – Change Snackbar Text and Background Color

Android Kotlin – Change Snackbar Text and Background Color

change snackbar color android kotlin

In this post, we will learn how to change the background and text color of Android Snackbar using Kotlin.

The default Android Snackbar with an action button will look as shown in the below image.

android default snackbar kotlin

In our example, we will make the Snackbar look like this.

android snackbar color changed kotlin

To change text, action button, and background color of the Snackbar we are going to use of setTextColor(), setActionTextColor(),and setBackgroundColor() methods.

In the example below, I have defined a simple button in an activity. when this button is clicked the Snackbar will be displayed to the user.

activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<androidx.coordinatorlayout.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/clContainer"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity">
    <Button
        android:id="@+id/buttonShowSnackBar"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Show Snackbar"
        android:layout_gravity="center"/>
</androidx.coordinatorlayout.widget.CoordinatorLayout>

In the below code, I have used snackBar.setActionTextColor(Color.parseColor("#721b65")) to change the action button text color of Snackbar. snackBar.view.setBackgroundColor(Color.parseColor("#1eb2a6")) is used change the background color and snackBar.setTextColor(Color.parseColor("#ffe2ff")) is used to text color.

MainActivity.kt

package com.example.snackbarbgexample
import android.graphics.Color
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.widget.Toast
import com.google.android.material.snackbar.Snackbar
import kotlinx.android.synthetic.main.activity_main.*
class MainActivity : AppCompatActivity() {
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)
        buttonShowSnackBar.setOnClickListener {
            val snackBar = Snackbar.make(clContainer,"SnackBar Text and Background Color changed",Snackbar.LENGTH_LONG)
            snackBar.setAction("OK"){
                        Toast.makeText(this, "Thank You", Toast.LENGTH_LONG).show()
                    }
            snackBar.setActionTextColor(Color.parseColor("#721b65"))
            snackBar.view.setBackgroundColor(Color.parseColor("#1eb2a6"))
            snackBar.setTextColor(Color.parseColor("#ffe2ff"))
            snackBar.show()
        }
    }
}

Output:

android snackbar change text background color

That’s it, we changed the text and background color of the Android Snackbar.

Leave a Reply

Your email address will not be published.