> For the complete documentation index, see [llms.txt](https://jobinjj.gitbook.io/tutorials/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://jobinjj.gitbook.io/tutorials/android-snackbar.md).

# Android snackbar

When Google announced Material Design on 2014, They have added many new components including android snackbar. It is a simple popup message just like toast but it can include actions. Just like toasts, we can customize the look of the snackbar.

**Steps included**

1. Add gradle dependency
2. Create simple snackbar
3. Add click action
4. Customize android snackbar

### 1. Adding material dependency

In the app level **build.gradle** located under the app folder, add following lines in between dependency tag.

{% embed url="<https://gist.github.com/6a58b564795a4184a38356f3fbfa4739>" %}

### 2. Create simple snackbar

First we have to assign a id for parent layout in **activity\_main.xml**. I have simply gave "parent" id to our top level constraint layout.

{% embed url="<https://gist.github.com/27cc45bfe0b8c9752a92cdc9bab07eff>" %}

Fetch reference to the parent layout in a ConstraintLayout object.

{% embed url="<https://gist.github.com/f3c40013b0936ed502db9ac3da932171>" %}

Then we will call make() method of the snackbar object with required parameters like text and duration and parent layout. and finally call the show() method to actually show the snackbar.

{% embed url="<https://gist.github.com/d7c14536f680ff90d778ea40a1e48d1c>" %}

### 3. Add click action

You can add click action by calling setAction() on the snackbar object.

{% embed url="<https://gist.github.com/5494bda547ce6071f0f5c6622e174c52>" %}

### 4. Custom Android snackbar

Customize action button color simply by calling setActionTextColor() on snackbar object. Pass a color as parameter. Below, I have passed a constant as color you can use your own custom color by using getResources().getColor(R.color.colorAccent) inside setActionTextColor() method.

{% embed url="<https://gist.github.com/3173d9ee3e2921b784eab663ba755bd1>" %}

{% file src="/files/-Lx0uvtn0uxlp5WDIjP8" %}
