<p>This article is about Android Snackbar and how to use it in android application with simple examples in Java and Kotlin.</p>



<figure class="wp-block-embed-youtube wp-block-embed is-type-video is-provider-youtube wp-embed-aspect-16-9 wp-has-aspect-ratio"><div class="wp-block-embed__wrapper">
<amp-youtube layout="responsive" width="1200" height="675" data-videoid="dtV0xJQz_eA" title="Android Snackbar Example(Java/Kotlin)"><a placeholder href="https://youtu.be/dtV0xJQz_eA"><img src="https://i.ytimg.com/vi/dtV0xJQz_eA/hqdefault.jpg" layout="fill" object-fit="cover" alt="Android Snackbar Example(Java/Kotlin)"></a></amp-youtube>
</div></figure>



<div class="wp-block-buttons aligncenter is-layout-flex wp-block-buttons-is-layout-flex">
<div class="wp-block-button is-style-outline is-style-outline--1"><a class="wp-block-button__link has-white-color has-text-color has-background" href="https://github.com/arunk7839/AndroidSnackbarDemo" style="background-color:#560399" target="_blank" rel="noreferrer noopener"><strong>DOWNLOAD CODE</strong></a></div>
</div>



<h3 class="wp-block-heading"><strong><span style="color:#530599" class="has-inline-color">Snackbar</span></strong></h3>



<p><span style="color:#530599" class="has-inline-color"><strong>Snackbar</strong></span> is an Android Material Design component . It provide lightweight response about an operation by showing a short message at the bottom of the screen on mobile and lower left on larger devices. </p>



<p>A Snackbar can contain an optional action button to perform action, such as undoing an action or retrying an action.</p>



<p>Only one snackbar will be shown at a time. </p>



<p>Snackbar disappear either after a timeout or after a user interaction elsewhere on the screen (particularly after interactions that invites a new surface or activity), but can also be swiped off the screen.</p>



<p><span class="has-inline-color has-vivid-red-color"><strong>Note:</strong> ;</span>Snackbars work best if they are displayed inside of a <strong>CoordinatorLayout</strong>. ;CoordinatorLayout ;allows the snackbar to enable behavior like swipe-to-dismiss, as well as automatically moving widgets like <strong>FloatingActionButton</strong>.</p>



<h3 class="wp-block-heading"><strong><span style="color:#520599" class="has-inline-color">Simple Snackbar</span></strong></h3>



<p>The below code snippet is an example of Simple Snackbar</p>



<pre class="wp-block-preformatted">Snackbar snackbar = Snackbar.<em>make</em>(<strong>constraintLayout</strong>, <strong>"Simple Snackbar "</strong>, Snackbar.<strong><em>LENGTH_LONG</em></strong>);
snackbar.show();</pre>



<p>The ;<strong><span style="color:#04603e" class="has-inline-color">make</span></strong> ;function accepts three parameters: </p>



<p><strong><span style="color:#04603e" class="has-inline-color">View</span></strong> ;: Used<em> </em>to<em> </em>find a parent view to hold Snackbar&#8217;s view.</p>



<p><strong><span style="color:#04603e" class="has-inline-color">CharSequence</span></strong>: The text to show on snackbar.</p>



<p><strong><span style="color:#04603e" class="has-inline-color">Duration</span></strong> : How long to display the message. ;It can be LENGTH_SHORT, LENGTH_LONG, LENGTH_INDEFINITE ;or a custom duration in milliseconds.</p>



<ul class="wp-block-list"><li><em><strong><span style="color:#530599" class="has-inline-color">LENGTH_SHORT</span></strong>: </em>Show the Snackbar for a short period of time.</li></ul>



<ul class="wp-block-list"><li><em><strong><span style="color:#530599" class="has-inline-color">LENGTH_LONG</span></strong>: </em>Show the Snackbar for a long period of time.</li></ul>



<ul class="wp-block-list"><li><em><strong><span style="color:#530599" class="has-inline-color">LENGTH_INDEFINITE</span></strong>: </em>Show the snackbar until it&#8217;s either dismissed or another snackbar is shown.</li></ul>



<p><strong><span style="color:#04603e" class="has-inline-color">show()</span></strong>: Used to display the Snackbar on the screen.</p>



<div class="wp-block-image"><figure class="aligncenter size-large is-resized"><img src="https://c1ctech.com/wp-content/uploads/2020/09/Screen-Shot-2020-09-10-at-12.13.30-PM.png" alt="" class="wp-image-2135" width="513" height="161"/><figcaption><strong><span class="has-inline-color has-vivid-red-color">Android Default Snackbar</span></strong></figcaption></figure></div>



<h3 class="wp-block-heading"><span style="color:#520599" class="has-inline-color"><strong>Snackbar with Action</strong></span></h3>



<p>You can add an action to a ;Snackbar , allowing the user to respond to your message. </p>



<p>To add an action, use the ;<strong><span style="color:#04603e" class="has-inline-color">setAction</span></strong> ;method on the object returned from ;<strong>make</strong>. Snackbars are automatically dismissed when the action is clicked.</p>



<p><span style="color:#530599" class="has-inline-color"><strong>setAction()</strong></span>: Set the action to be displayed in ;the Snackbar. It takes two parameters:</p>



<ul class="wp-block-list"><li><strong><span style="color:#04603e" class="has-inline-color">text</span></strong>: Text to display for the action</li></ul>



<ul class="wp-block-list"><li><strong><span style="color:#04603e" class="has-inline-color">listener</span></strong>: callback to be invoked when the action is clicked.</li></ul>



<p>The below code snippet is an example of how to show a snackbar with a message and an action:</p>



<pre class="wp-block-preformatted">Snackbar snackbarWithAction = Snackbar.<em>make</em>(<strong>constraintLayout</strong>, <strong>"Contact removed"</strong>, Snackbar.<strong><em>LENGTH_LONG</em></strong>);

<strong><span style="color:#04603e" class="has-inline-color">//get snackbar view</span></strong>
View snackbarView = snackbarWithAction.getView();

<strong><span style="color:#04603e" class="has-inline-color">//get snackbar child views</span></strong>
TextView snackbarText = (TextView) snackbarView.findViewById(R.id.<strong><em>snackbar_text</em></strong>);
TextView snackbarActionText = (TextView) snackbarView.findViewById(R.id.<strong><em>snackbar_action</em></strong>);

<em><strong><span style="color:#04603e" class="has-inline-color">//change snackbar text color</span></strong>
</em>snackbarText.setTextColor((Color.<strong><em>CYAN</em></strong>));

<em><strong><span style="color:#04603e" class="has-inline-color">//change snackbar action button text color</span></strong>
</em>snackbarActionText.setTextColor((Color.<strong><em>RED</em></strong>));

<strong><span style="color:#04603e" class="has-inline-color">//use setAction() to add action UNDO</span></strong>
snackbarWithAction.<strong>setAction</strong>(<strong>"UNDO"</strong>, <strong>new </strong>View.OnClickListener() {
 @Override
 <strong>public void </strong>onClick(View view) {

 Snackbar snackbar = Snackbar.<em>make</em>(<strong>constraintLayout</strong>, <strong>"The contact is restored"</strong>, Snackbar.<strong><em>LENGTH_LONG</em></strong>);

 View snackbarView = snackbar.getView();

 <em><strong><span style="color:#04603e" class="has-inline-color">//change snackbar background color</span></strong> </em>
<em> </em> snackbarView.setBackgroundColor(getResources()
 .getColor(R.color.<strong><em>green</em></strong>));

 TextView snackbarText = (TextView) snackbarView.findViewById(R.id.<strong><em>snackbar_text</em></strong>);

 <em><strong><span style="color:#04603e" class="has-inline-color">//change snackbar text color</span></strong>
 </em>snackbarText.setTextColor((Color.<strong><em>WHITE</em></strong>));

 <em><strong><span style="color:#04603e" class="has-inline-color">//change snackbar text gravity</span></strong>
 </em><strong>if </strong>(Build.VERSION.<strong><em>SDK_INT </em></strong>>;= Build.VERSION_CODES.<strong><em>JELLY_BEAN_MR1</em></strong>)
 snackbarText.setTextAlignment(View.<strong><em>TEXT_ALIGNMENT_CENTER</em></strong>);
 <strong>else </strong>{
 snackbarText.setGravity(Gravity.<strong><em>CENTER_HORIZONTAL</em></strong>);
 }
 snackbar.show();
 }
});
<strong><span style="color:#04603e" class="has-inline-color">//show snackbar</span></strong>
snackbarWithAction.show();</pre>



<div class="wp-block-image"><figure class="aligncenter size-large is-resized"><img src="https://c1ctech.com/wp-content/uploads/2020/09/Screen-Shot-2020-09-10-at-12.14.02-PM.png" alt="" class="wp-image-2136" width="516" height="202"/><figcaption><strong><span class="has-inline-color has-vivid-red-color">Snackbar with Action</span></strong></figcaption></figure></div>



<div class="wp-block-image"><figure class="aligncenter size-large is-resized"><img src="https://c1ctech.com/wp-content/uploads/2020/09/Screen-Shot-2020-09-10-at-12.14.26-PM.png" alt="" class="wp-image-2137" width="518" height="174"/><figcaption><strong><span class="has-inline-color has-vivid-red-color">Show snackbar with text (center) on click of UNDO button</span></strong></figcaption></figure></div>



<h3 class="wp-block-heading"><span style="color:#520599" class="has-inline-color"><strong>Snackbar with Custom View</strong></span></h3>



<p>Android Snackbar allows you to create a customized layout for your snackbar. </p>



<p>To create a custom layout, define a View layout, in XML. The following snippet contains a customized layout for snackbar (saved as ;layout/custom_snackbar.xml):</p>



<pre class="wp-block-preformatted"><;<strong>androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
 xmlns:app="http://schemas.android.com/apk/res-auto"
 android:layout_width="match_parent"
 android:layout_height="wrap_content"
 android:id="@+id/custom_snackbar_container"
 android:background="@android:color/holo_orange_light"</strong>>;

 <;<strong>ImageView
 android:id="@+id/iv_custom_view"
 android:layout_width="wrap_content"
 android:layout_height="wrap_content"
 android:layout_marginRight="@dimen/margin_10dp"
 app:layout_constraintBottom_toBottomOf="parent"
 app:layout_constraintEnd_toStartOf="@+id/tv_custom_view"
 app:layout_constraintHorizontal_bias="0.5"
 app:layout_constraintHorizontal_chainStyle="packed"
 app:layout_constraintStart_toStartOf="parent"
 app:layout_constraintTop_toTopOf="parent"
 app:srcCompat="@android:drawable/ic_dialog_alert" </strong>/>;

 <;<strong>TextView
 android:id="@+id/tv_custom_view"
 android:layout_width="wrap_content"
 android:layout_height="wrap_content"
 android:text="@string/no_external_storage_available"
 android:textColor="@android:color/black"
 android:textSize="@dimen/textsize_15sp"
 app:layout_constraintBottom_toBottomOf="parent"
 app:layout_constraintEnd_toEndOf="parent"
 app:layout_constraintHorizontal_bias="0.5"
 app:layout_constraintStart_toEndOf="@+id/iv_custom_view"
 app:layout_constraintTop_toTopOf="parent" </strong>/>;

<;/<strong>androidx.constraintlayout.widget.ConstraintLayout</strong>>;</pre>



<p>You must use the ID of the ConstraintLayout element ( &#8220;<strong>custom_snackbar_container</strong>&#8220;) and the ID of the XML layout file &#8220;<strong>custom_snackbar.xml</strong>&#8221; to inflate the layout, as shown here:</p>



<pre class="wp-block-preformatted"><em><strong><span style="color:#04603e" class="has-inline-color">//get created custom layout of snackbar</span></strong>
</em>View <strong>customSnackBarView</strong> = getLayoutInflater().inflate(R.layout.<strong><em>custom_snackbar</em></strong>, (ViewGroup) findViewById(R.id.<strong><em>custom_snackbar_container</em></strong>));</pre>



<p>Now pass this ;<strong>View</strong> object (created using custom layout) to the ;<strong>addView()</strong> method.</p>



<pre class="wp-block-preformatted">Snackbar customSnackBar = Snackbar.<em>make</em>(<strong>constraintLayout</strong>, <strong>""</strong>, Snackbar.<strong><em>LENGTH_LONG</em></strong>);

<strong>Snackbar.SnackbarLayout layout = (Snackbar.SnackbarLayout) customSnackBar.getView();</strong>

<em><span style="color:#04603e" class="has-inline-color"><strong>//get snackbar custom layout</strong></span>
</em>View customSnackBarView = getLayoutInflater().inflate(R.layout.<strong><em>custom_snackbar</em></strong>, (ViewGroup) findViewById(R.id.<strong><em>custom_snackbar_container</em></strong>));

<em><strong><span style="color:#04603e" class="has-inline-color">//add snackbar custom layout</span></strong>
</em><strong>layout.addView(customSnackBarView, 0);</strong>

customSnackBar.show();</pre>



<div class="wp-block-image"><figure class="aligncenter size-large is-resized"><img src="https://c1ctech.com/wp-content/uploads/2020/09/Screen-Shot-2020-09-10-at-12.14.49-PM.png" alt="" class="wp-image-2138" width="524" height="182"/><figcaption><strong><span class="has-inline-color has-vivid-red-color">Snackbar with custom layout</span></strong></figcaption></figure></div>



<p></p>



<h3 class="wp-block-heading"><strong><span style="color:#520599" class="has-inline-color">Creating New Project</span></strong></h3>



<p><strong>1</strong>. In ;<strong><span style="color:#04603e" class="has-inline-color">Android Studio</span></strong>, go to ;<strong><span style="color:#04603e" class="has-inline-color">File ⇒ New Project</span></strong>, fill all the details required to create a new project and then click on <strong><span style="color:#04603e" class="has-inline-color">finish</span></strong>.</p>



<p><strong>2</strong>. Open ;<strong><span style="color:#04603e" class="has-inline-color">build.gradle</span></strong> (app level), add the material dependency as shown below and then <strong>sync</strong> your project:</p>



<p><strong><span style="color:#520599" class="has-inline-color">build.gradle</span></strong></p>



<pre class="wp-block-preformatted"><code>dependencies <strong>{
 <span style="color:#04603e" class="has-inline-color"> implementation 'com.google.android.material:material:1.2.0'</span>
}</strong></code></pre>



<p><strong>3</strong>. Rename the layout file <strong>activity_main.xml</strong> as <strong><span style="color:#04603e" class="has-inline-color">activity_snackbar.xml</span></strong> and add the below code. This layout file consist of three buttons, <strong>SIMPLE SNACKBAR</strong> (To show Android standard snackbar), <strong>SNACKBAR WITH ACTION</strong> (To show snackbar with action), <strong>SNACKBAR WITH CUSTOM VIEW</strong> (To show snackbar with custom view).</p>



<p><strong><span style="color:#520599" class="has-inline-color">activity_snackbar.xml</span></strong></p>



<pre class="wp-block-preformatted"><em><;?</em><strong>xml version="1.0" encoding="utf-8"</strong><em>?>;
</em><;<strong>androidx.constraintlayout.widget.ConstraintLayout 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/constraintLayout"
 android:layout_width="match_parent"
 android:layout_height="match_parent"
 android:layout_margin="@dimen/margin_20dp"
 tools:context=".SnackbarActivityJava"</strong>>;

 <;<strong>Button
 android:id="@+id/btn_simple_snackbar"
 android:layout_width="match_parent"
 android:layout_height="wrap_content"
 android:text="@string/simple_snackbar"
 android:padding="@dimen/padding_12dp"
 app:layout_constraintBottom_toTopOf="@+id/btn_snackbar_with_action"
 app:layout_constraintEnd_toEndOf="parent"
 app:layout_constraintStart_toStartOf="parent"
 app:layout_constraintTop_toTopOf="parent"
 app:layout_constraintVertical_chainStyle="packed" </strong>/>;


 <;<strong>Button
 android:id="@+id/btn_snackbar_with_action"
 android:layout_width="match_parent"
 android:layout_height="wrap_content"
 android:layout_marginTop="@dimen/margin_10dp"
 android:layout_marginBottom="@dimen/margin_10dp"
 android:text="@string/snackbar_with_action"
 android:padding="@dimen/padding_12dp"
 app:layout_constraintBottom_toTopOf="@+id/btn_snackbar_with_custom_view"
 app:layout_constraintEnd_toEndOf="@+id/btn_simple_snackbar"
 app:layout_constraintHorizontal_bias="0.5"
 app:layout_constraintStart_toStartOf="@+id/btn_simple_snackbar"
 app:layout_constraintTop_toBottomOf="@+id/btn_simple_snackbar" </strong>/>;

 <;<strong>Button
 android:id="@+id/btn_snackbar_with_custom_view"
 android:layout_width="match_parent"
 android:layout_height="wrap_content"
 android:text="@string/snackbar_with_custom_view"
 android:padding="@dimen/padding_12dp"
 app:layout_constraintBottom_toBottomOf="parent"
 app:layout_constraintEnd_toEndOf="@+id/btn_snackbar_with_action"
 app:layout_constraintHorizontal_bias="0.5"
 app:layout_constraintStart_toStartOf="@+id/btn_snackbar_with_action"
 app:layout_constraintTop_toBottomOf="@+id/btn_snackbar_with_action" </strong>/>;

<;/<strong>androidx.constraintlayout.widget.ConstraintLayout</strong>>;</pre>



<p><strong>4. </strong>Create a new layout file <strong>custom_snackbar.xml</strong> (layout->;New->;Layout Resource File) and add the below code. This layout file represents the custom layout of snackbar.</p>



<p><strong><span style="color:#520599" class="has-inline-color">custom_snackbar.xml</span></strong></p>
<!-- WP QUADS Content Ad Plugin v. 2.0.98.1 -->
<div class="quads-location quads-ad2" id="quads-ad2" style="float:none;margin:0px;">

</div>




<pre class="wp-block-preformatted"><em><;?</em><strong>xml version</strong><strong>="1.0" </strong><strong>encoding</strong><strong>="utf-8"</strong><em>?>;<br></em><;<strong>androidx.constraintlayout.widget.ConstraintLayout </strong><strong>xmlns:</strong><strong>android</strong><strong>="http://schemas.android.com/apk/res/android"<br></strong><strong> </strong><strong>xmlns:</strong><strong>app</strong><strong>="http://schemas.android.com/apk/res-auto"<br></strong><strong> </strong><strong>android</strong><strong>:layout_width</strong><strong>="match_parent"<br></strong><strong> </strong><strong>android</strong><strong>:layout_height</strong><strong>="wrap_content"<br></strong><strong> </strong><strong>android</strong><strong>:id</strong><strong>="@+id/custom_snackbar_container"<br></strong><strong> </strong><strong>android</strong><strong>:background</strong><strong>="@android:color/holo_orange_light"</strong>>;<br><br> <;<strong>ImageView<br></strong><strong> </strong><strong>android</strong><strong>:id</strong><strong>="@+id/iv_custom_view"<br></strong><strong> </strong><strong>android</strong><strong>:layout_width</strong><strong>="wrap_content"<br></strong><strong> </strong><strong>android</strong><strong>:layout_height</strong><strong>="wrap_content"<br></strong><strong> </strong><strong>android</strong><strong>:layout_marginRight</strong><strong>="@dimen/margin_10dp"<br></strong><strong> </strong><strong>app</strong><strong>:layout_constraintBottom_toBottomOf</strong><strong>="parent"<br></strong><strong> </strong><strong>app</strong><strong>:layout_constraintEnd_toStartOf</strong><strong>="@+id/tv_custom_view"<br></strong><strong> </strong><strong>app</strong><strong>:layout_constraintHorizontal_bias</strong><strong>="0.5"<br></strong><strong> </strong><strong>app</strong><strong>:layout_constraintHorizontal_chainStyle</strong><strong>="packed"<br></strong><strong> </strong><strong>app</strong><strong>:layout_constraintStart_toStartOf</strong><strong>="parent"<br></strong><strong> </strong><strong>app</strong><strong>:layout_constraintTop_toTopOf</strong><strong>="parent"<br></strong><strong> </strong><strong>app</strong><strong>:srcCompat</strong><strong>="@android:drawable/ic_dialog_alert" </strong>/>;<br><br> <;<strong>TextView<br></strong><strong> </strong><strong>android</strong><strong>:id</strong><strong>="@+id/tv_custom_view"<br></strong><strong> </strong><strong>android</strong><strong>:layout_width</strong><strong>="wrap_content"<br></strong><strong> </strong><strong>android</strong><strong>:layout_height</strong><strong>="wrap_content"<br></strong><strong> </strong><strong>android</strong><strong>:text</strong><strong>="@string/no_external_storage_available"<br></strong><strong> </strong><strong>android</strong><strong>:textColor</strong><strong>="@android:color/black"<br></strong><strong> </strong><strong>android</strong><strong>:textSize</strong><strong>="@dimen/textsize_15sp"<br></strong><strong> </strong><strong>app</strong><strong>:layout_constraintBottom_toBottomOf</strong><strong>="parent"<br></strong><strong> </strong><strong>app</strong><strong>:layout_constraintEnd_toEndOf</strong><strong>="parent"<br></strong><strong> </strong><strong>app</strong><strong>:layout_constraintHorizontal_bias</strong><strong>="0.5"<br></strong><strong> </strong><strong>app</strong><strong>:layout_constraintStart_toEndOf</strong><strong>="@+id/iv_custom_view"<br></strong><strong> </strong><strong>app</strong><strong>:layout_constraintTop_toTopOf</strong><strong>="parent" </strong>/>;<br><br><;/<strong>androidx.constraintlayout.widget.ConstraintLayout</strong>>;</pre>



<h4 class="wp-block-heading"><span style="color:#520599" class="has-inline-color"><strong>Complete code in Java</strong></span></h4>



<p>Create a new activity <strong><span style="color:#04603e" class="has-inline-color">SnackbarActivityJava.java</span></strong> This activity contains three buttons with click listener to show the different ways of implementing the Snackbar in <strong>Java</strong>.</p>



<p><strong><span style="color:#520599" class="has-inline-color">SnackbarActivityJava.java</span></strong></p>



<pre class="wp-block-preformatted"><strong>package </strong>com.c1ctech.androidsnackbardemo;

<strong>import </strong>android.graphics.Color;
<strong>import </strong>android.os.Build;
<strong>import </strong>android.os.Bundle;
<strong>import </strong>android.view.Gravity;
<strong>import </strong>android.view.View;
<strong>import </strong>android.view.ViewGroup;
<strong>import </strong>android.widget.Button;
<strong>import </strong>android.widget.ImageView;
<strong>import </strong>android.widget.TextView;

<strong>import </strong>com.google.android.material.snackbar.Snackbar;

<strong>import </strong>androidx.appcompat.app.AppCompatActivity;
<strong>import </strong>androidx.constraintlayout.widget.ConstraintLayout;

<strong>public class </strong>SnackbarActivityJava <strong>extends </strong>AppCompatActivity <strong>implements </strong>View.OnClickListener {

 <strong>private </strong>Button <strong>btnSimpleSnackbar</strong>, <strong>btnSnackbarWithAction</strong>, <strong>btnCustomSnackbar</strong>;
 <strong>private </strong>ConstraintLayout <strong>constraintLayout</strong>;

 @Override
 <strong>protected void </strong>onCreate(Bundle savedInstanceState) {
 <strong>super</strong>.onCreate(savedInstanceState);
 setContentView(R.layout.<strong><em>activity_snackbar</em></strong>);

 <em><strong><span style="color:#04603e" class="has-inline-color">//get parent view by its id</span></strong>
 </em><strong>constraintLayout </strong>= findViewById(R.id.<strong><em>constraintLayout</em></strong>);

 <em><strong><span style="color:#04603e" class="has-inline-color">//get child views of constraintLayout by its id</span></strong>
 </em><strong>btnSimpleSnackbar </strong>= findViewById(R.id.<strong><em>btn_simple_snackbar</em></strong>);
 <strong>btnSnackbarWithAction </strong>= findViewById(R.id.<strong><em>btn_snackbar_with_action</em></strong>);
 <strong>btnCustomSnackbar </strong>= findViewById(R.id.<strong><em>btn_snackbar_with_custom_view</em></strong>);

 <em><span style="color:#04603e" class="has-inline-color"><strong>//setting listener to each button</strong></span>
 </em><strong>btnSimpleSnackbar</strong>.setOnClickListener(<strong>this</strong>);
 <strong>btnSnackbarWithAction</strong>.setOnClickListener(<strong>this</strong>);
 <strong>btnCustomSnackbar</strong>.setOnClickListener(<strong>this</strong>);
 }

 @Override
 <strong>public void </strong>onClick(View view) {

 <strong>int </strong>id = view.getId();
 <strong>switch </strong>(id) {
 <strong>case </strong>R.id.<strong><em>btn_simple_snackbar</em></strong>:
 Snackbar.<em>make</em>(<strong>constraintLayout</strong>, <strong>"Simple Snackbar "</strong>, Snackbar.<strong><em>LENGTH_LONG</em></strong>).show();
 <strong>break</strong>;

 <strong>case </strong>R.id.<strong><em>btn_snackbar_with_action</em></strong>:
 Snackbar snackbarWithAction = Snackbar.<em>make</em>(<strong>constraintLayout</strong>, <strong>"Contact removed"</strong>, Snackbar.<strong><em>LENGTH_LONG</em></strong>);

 <em><strong><span style="color:#04603e" class="has-inline-color">//get snackbar root view</span></strong>
 </em>View snackbarView = snackbarWithAction.getView();

 <em><span style="color:#04603e" class="has-inline-color"><strong>//getting child views of snackbar</strong></span>
 </em>TextView snackbarText = (TextView) snackbarView.findViewById(R.id.<strong><em>snackbar_text</em></strong>);
 TextView snackbarActionText = (TextView) snackbarView.findViewById(R.id.<strong><em>snackbar_action</em></strong>);

 <em><strong><span style="color:#04603e" class="has-inline-color">//change snackbar text color</span></strong>
 </em>snackbarText.setTextColor((Color.<strong><em>CYAN</em></strong>));

 <em><strong><span style="color:#04603e" class="has-inline-color">//change snackbar action button text color</span></strong>
 </em>snackbarActionText.setTextColor((Color.<strong><em>RED</em></strong>));

 snackbarWithAction.setAction(<strong>"UNDO"</strong>, <strong>new </strong>View.OnClickListener() {
 @Override
 <strong>public void </strong>onClick(View view) {

 Snackbar snackbar = Snackbar.<em>make</em>(<strong>constraintLayout</strong>, <strong>"The contact is restored"</strong>, Snackbar.<strong><em>LENGTH_LONG</em></strong>);

 View snackbarView = snackbar.getView();

 <em><strong><span style="color:#04603e" class="has-inline-color">//change snackbar background color</span></strong>
 </em>snackbarView.setBackgroundColor(getResources().getColor(R.color.<strong><em>green</em></strong>));

 TextView snackbarText = (TextView) snackbarView.findViewById(R.id.<strong><em>snackbar_text</em></strong>);

 <em><strong><span style="color:#04603e" class="has-inline-color">//change snackbar text color</span></strong>
 </em>snackbarText.setTextColor((Color.<strong><em>WHITE</em></strong>));

 <em><strong><span style="color:#04603e" class="has-inline-color">//change snackbar text gravity</span></strong>
 </em><strong>if </strong>(Build.VERSION.<strong><em>SDK_INT </em></strong>>;= Build.VERSION_CODES.<strong><em>JELLY_BEAN_MR1</em></strong>)
 snackbarText.setTextAlignment(View.<strong><em>TEXT_ALIGNMENT_CENTER</em></strong>);
 <strong>else </strong>{
 snackbarText.setGravity(Gravity.<strong><em>CENTER_HORIZONTAL</em></strong>);
 }
 <em><strong><span style="color:#04603e" class="has-inline-color">//show snackbar</span></strong>
 </em>snackbar.show();
 }
 });
 <em><strong><span style="color:#04603e" class="has-inline-color">//show snackbar</span></strong>
 </em>snackbarWithAction.show();
 <strong>break</strong>;

 <strong>case </strong>R.id.<strong><em>btn_snackbar_with_custom_view</em></strong>:

 Snackbar customSnackBar = Snackbar.<em>make</em>(<strong>constraintLayout</strong>, <strong>""</strong>, Snackbar.<strong><em>LENGTH_LONG</em></strong>);

 Snackbar.SnackbarLayout layout = (Snackbar.SnackbarLayout) customSnackBar.getView();

 <em><strong><span style="color:#04603e" class="has-inline-color">//get created custom layout of snackbar</span></strong>
 </em>View customSnackBarView = getLayoutInflater().inflate(R.layout.<strong><em>custom_snackbar</em></strong>, (ViewGroup) findViewById(R.id.<strong><em>custom_snackbar_container</em></strong>));

 <em><span style="color:#04603e" class="has-inline-color"><strong>//getting views from snackbar custom layout</strong></span>
 </em>TextView textView = (TextView) customSnackBarView.findViewById(R.id.<strong><em>tv_custom_view</em></strong>);
 ImageView imageView = (ImageView) customSnackBarView.findViewById(R.id.<strong><em>iv_custom_view</em></strong>);

 <em><strong><span style="color:#04603e" class="has-inline-color">//set text in snackbar custom layout textview</span></strong>
 </em>textView.setText(getResources().getText(R.string.<strong><em>no_external_storage_available</em></strong>));

 layout.setPadding(0, 0, 0, 0);

 <em><span style="color:#04603e" class="has-inline-color"><strong>//add snackbar custom layout</strong></span>
 </em>layout.addView(customSnackBarView, 0);

 <em><span style="color:#04603e" class="has-inline-color"><strong>//show snackbar</strong></span>
 </em>customSnackBar.show();
 <strong>break</strong>;

 }
 }
}

</pre>



<p>When you run the application it will look like this:</p>



<figure class="wp-block-image size-large is-resized"><img src="https://c1ctech.com/wp-content/uploads/2020/09/Screenshot_1599719772-576x1024.png" alt="" class="wp-image-2133" width="514" height="914"/></figure>



<h4 class="wp-block-heading"><span style="color:#520599" class="has-inline-color"><strong>Complete code in Kotlin</strong></span></h4>



<p>Create a new activity <strong>SnackbarActivityKotlin.kt</strong> (New->;Activity->;Empty Activity->;select language kotlin) and then click on <strong>finish</strong>.This activity contains three buttons with click listener to show the different ways of implementing the snackbar in <strong>Kotlin</strong>.</p>



<p><strong><span style="color:#520599" class="has-inline-color">SnackbarActivityKotlin.kt</span></strong></p>



<pre class="wp-block-preformatted"><strong>package </strong>com.c1ctech.androidsnackbardemo

<strong>import </strong>android.graphics.Color
<strong>import </strong>android.os.Build
<strong>import </strong>android.os.Bundle
<strong>import </strong>android.view.Gravity
<strong>import </strong>android.view.View
<strong>import </strong>android.view.ViewGroup
<strong>import </strong>android.widget.Button
<strong>import </strong>android.widget.ImageView
<strong>import </strong>android.widget.TextView
<strong>import </strong>androidx.appcompat.app.AppCompatActivity
<strong>import </strong>androidx.constraintlayout.widget.ConstraintLayout
<strong>import </strong>com.google.android.material.snackbar.Snackbar
<strong>import </strong>com.google.android.material.snackbar.Snackbar.SnackbarLayout

<strong>class </strong>SnackBarActivityKotlin : AppCompatActivity(), View.OnClickListener {

 <strong>override fun </strong>onCreate(savedInstanceState: Bundle?) {
 <strong>super</strong>.onCreate(savedInstanceState)
 setContentView(R.layout.<em>activity_snackbar</em>)

 <em><strong><span style="color:#04603e" class="has-inline-color">//get views by its id</span></strong>
 </em><strong>val </strong>btnSimpleSnackbar = findViewById<;Button>;(R.id.<em>btn_simple_snackbar</em>)
 <strong>val </strong>btnSnackbarWithAction = findViewById<;Button>;(R.id.<em>btn_snackbar_with_action</em>)
 <strong>val </strong>btnCustomSnackbar = findViewById<;Button>;(R.id.<em>btn_snackbar_with_custom_view</em>)

 <em><strong><span style="color:#04603e" class="has-inline-color">//setting listener to each button</span></strong>
 </em>btnSimpleSnackbar.setOnClickListener(<strong>this</strong>)
 btnSnackbarWithAction.setOnClickListener(<strong>this</strong>)
 btnCustomSnackbar.setOnClickListener(<strong>this</strong>)
 }

 <strong>override fun </strong>onClick(view: View) {

 <em><strong><span style="color:#04603e" class="has-inline-color">//get parent view by its id</span></strong>
 </em><strong>val </strong>constraintLayout = findViewById<;ConstraintLayout>;(R.id.<em>constraintLayout</em>)

 <strong>val </strong>id: Int = view.getId()
 <strong>when </strong>(id) {
 R.id.<em>btn_simple_snackbar </em>->; Snackbar.make(constraintLayout, <strong>"Simple Snackbar "</strong>, Snackbar.<em>LENGTH_LONG</em>).show()
 R.id.<em>btn_snackbar_with_action </em>->; {
 <strong>val </strong>snackbarWithAction = Snackbar.make(constraintLayout, <strong>"Contact removed"</strong>, Snackbar.<em>LENGTH_LONG</em>)
 <strong>val </strong>snackbarView = snackbarWithAction.<em>view <strong><span style="color:#04603e" class="has-inline-color">//get snackbar root view</span></strong>

 <strong><span style="color:#04603e" class="has-inline-color">//getting child views of snackbar</span></strong>
 </em><strong>val </strong>snackbarText = snackbarView.findViewById<;View>;(R.id.<em>snackbar_text</em>) <strong>as </strong>TextView
 <strong>val </strong>snackbarActionText = snackbarView.findViewById<;View>;(R.id.<em>snackbar_action</em>) <strong>as </strong>TextView

 <em><strong><span style="color:#04603e" class="has-inline-color">//change snackbar text color</span></strong>
 </em>snackbarText.setTextColor(Color.<em>CYAN</em>)

 <em><strong><span style="color:#04603e" class="has-inline-color">//change snackbar action button text color</span></strong>
 </em>snackbarActionText.setTextColor(Color.<em>RED</em>)
 snackbarWithAction.setAction(<strong>"UNDO"</strong>) <strong>{
 val </strong>snackbar = Snackbar.make(constraintLayout, <strong>"The contact is restored"</strong>, Snackbar.<em>LENGTH_LONG</em>)
 <strong>val </strong>snackbarView = snackbar.<em>view

 <strong><span style="color:#04603e" class="has-inline-color">//change snackbar background color</span></strong>
 </em>snackbarView.setBackgroundColor(<em>resources</em>.getColor(R.color.<em>green</em>))
 <strong>val </strong>snackbarText = snackbarView.findViewById<;View>;(R.id.<em>snackbar_text</em>) <strong>as </strong>TextView

 <em><strong><span style="color:#04603e" class="has-inline-color">//change snackbar text color</span></strong>
 </em>snackbarText.setTextColor(Color.<em>WHITE</em>)

 <em><strong><span style="color:#04603e" class="has-inline-color">//change snackbar text gravity</span></strong>
 </em><strong>if </strong>(Build.VERSION.<em>SDK_INT </em>>;= Build.VERSION_CODES.<em>JELLY_BEAN_MR1</em>) snackbarText.<em>textAlignment </em>= View.<em>TEXT_ALIGNMENT_CENTER </em><strong>else </strong>{
 snackbarText.<em>gravity </em>= Gravity.<em>CENTER_HORIZONTAL
 </em>}
 <em><strong><span style="color:#04603e" class="has-inline-color">//show snackbar</span></strong>
 </em>snackbar.show()
 <strong>}
 </strong><em><span style="color:#04603e" class="has-inline-color"><strong>//show snackbar</strong></span>
 </em>snackbarWithAction.show()
 }
 R.id.<em>btn_snackbar_with_custom_view </em>->; {
 <strong>val </strong>customSnackBar = Snackbar.make(constraintLayout, <strong>""</strong>, Snackbar.<em>LENGTH_LONG</em>)
 <strong>val </strong>layout = customSnackBar.<em>view </em><strong>as </strong>SnackbarLayout

 <em><strong><span style="color:#04603e" class="has-inline-color">//get created custom layout of snackbar</span></strong>
 </em><strong>val </strong>customSnackBarView = <em>layoutInflater</em>.inflate(R.layout.<em>custom_snackbar</em>, findViewById<;View>;(R.id.<em>custom_snackbar_container</em>) <strong>as? </strong>ViewGroup)

 <em><strong><span style="color:#04603e" class="has-inline-color">//getting views from snackbar custom layout</span></strong>
 </em><strong>val </strong>textView = customSnackBarView.findViewById<;TextView>;(R.id.<em>tv_custom_view</em>)
 <strong>val </strong>imageView = customSnackBarView.findViewById<;ImageView>;(R.id.<em>iv_custom_view</em>)

 <em><strong><span style="color:#04603e" class="has-inline-color">//set text in snackbar custom layout textview</span></strong>
 </em>textView.<em>text </em>= <em>resources</em>.getText(R.string.<em>no_external_storage_available</em>)
 layout.setPadding(0, 0, 0, 0)

 <em><strong><span style="color:#04603e" class="has-inline-color">//add snackbar custom layout</span></strong>
 </em>layout.addView(customSnackBarView, 0)

 <em><strong><span style="color:#04603e" class="has-inline-color">//show snackbar</span></strong>
 </em>customSnackBar.show()
 }
 }
 }
}</pre>


