Site icon C1CTech

Android Snackbar(Java/Kotlin)

&NewLine;<p>This article is about Android Snackbar and how to use it in android application with simple examples in Java and Kotlin&period;<&sol;p>&NewLine;&NewLine;&NewLine;&NewLine;<figure class&equals;"wp-block-embed-youtube wp-block-embed is-type-video is-provider-youtube wp-embed-aspect-16-9 wp-has-aspect-ratio"><div class&equals;"wp-block-embed&lowbar;&lowbar;wrapper">&NewLine;<amp-youtube layout&equals;"responsive" width&equals;"1200" height&equals;"675" data-videoid&equals;"dtV0xJQz&lowbar;eA" title&equals;"Android Snackbar Example&lpar;Java&sol;Kotlin&rpar;"><a placeholder href&equals;"https&colon;&sol;&sol;youtu&period;be&sol;dtV0xJQz&lowbar;eA"><img src&equals;"https&colon;&sol;&sol;i&period;ytimg&period;com&sol;vi&sol;dtV0xJQz&lowbar;eA&sol;hqdefault&period;jpg" layout&equals;"fill" object-fit&equals;"cover" alt&equals;"Android Snackbar Example&lpar;Java&sol;Kotlin&rpar;"><&sol;a><&sol;amp-youtube>&NewLine;<&sol;div><&sol;figure>&NewLine;&NewLine;&NewLine;&NewLine;<div class&equals;"wp-block-buttons aligncenter is-layout-flex wp-block-buttons-is-layout-flex">&NewLine;<div class&equals;"wp-block-button is-style-outline is-style-outline--1"><a class&equals;"wp-block-button&lowbar;&lowbar;link has-white-color has-text-color has-background" href&equals;"https&colon;&sol;&sol;github&period;com&sol;arunk7839&sol;AndroidSnackbarDemo" style&equals;"background-color&colon;&num;560399" target&equals;"&lowbar;blank" rel&equals;"noreferrer noopener"><strong>DOWNLOAD CODE<&sol;strong><&sol;a><&sol;div>&NewLine;<&sol;div>&NewLine;&NewLine;&NewLine;&NewLine;<h3 class&equals;"wp-block-heading"><strong><span style&equals;"color&colon;&num;530599" class&equals;"has-inline-color">Snackbar<&sol;span><&sol;strong><&sol;h3>&NewLine;&NewLine;&NewLine;&NewLine;<p><span style&equals;"color&colon;&num;530599" class&equals;"has-inline-color"><strong>Snackbar<&sol;strong><&sol;span> is an Android Material Design component &period; 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&period; <&sol;p>&NewLine;&NewLine;&NewLine;&NewLine;<p>A Snackbar can contain an optional action button to perform action&comma; such as undoing an action or retrying an action&period;<&sol;p>&NewLine;&NewLine;&NewLine;&NewLine;<p>Only one snackbar will be shown at a time&period; <&sol;p>&NewLine;&NewLine;&NewLine;&NewLine;<p>Snackbar disappear either after a timeout or after a user interaction elsewhere on the screen &lpar;particularly after interactions that invites a new surface or activity&rpar;&comma; but can also be swiped off the screen&period;<&sol;p>&NewLine;&NewLine;&NewLine;&NewLine;<p><span class&equals;"has-inline-color has-vivid-red-color"><strong>Note&colon;<&sol;strong>&nbsp&semi;<&sol;span>Snackbars work best if they are displayed inside of a <strong>CoordinatorLayout<&sol;strong>&period;&nbsp&semi;CoordinatorLayout&nbsp&semi;allows the snackbar to enable behavior like swipe-to-dismiss&comma; as well as automatically moving widgets like <strong>FloatingActionButton<&sol;strong>&period;<&sol;p>&NewLine;&NewLine;&NewLine;&NewLine;<h3 class&equals;"wp-block-heading"><strong><span style&equals;"color&colon;&num;520599" class&equals;"has-inline-color">Simple Snackbar<&sol;span><&sol;strong><&sol;h3>&NewLine;&NewLine;&NewLine;&NewLine;<p>The below code snippet is an example of Simple Snackbar<&sol;p>&NewLine;&NewLine;&NewLine;&NewLine;<pre class&equals;"wp-block-preformatted">Snackbar snackbar &equals; Snackbar&period;<em>make<&sol;em>&lpar;<strong>constraintLayout<&sol;strong>&comma; <strong>"Simple Snackbar "<&sol;strong>&comma; Snackbar&period;<strong><em>LENGTH&lowbar;LONG<&sol;em><&sol;strong>&rpar;&semi;&NewLine;snackbar&period;show&lpar;&rpar;&semi;<&sol;pre>&NewLine;&NewLine;&NewLine;&NewLine;<p>The&nbsp&semi;<strong><span style&equals;"color&colon;&num;04603e" class&equals;"has-inline-color">make<&sol;span><&sol;strong>&nbsp&semi;function accepts three parameters&colon; <&sol;p>&NewLine;&NewLine;&NewLine;&NewLine;<p><strong><span style&equals;"color&colon;&num;04603e" class&equals;"has-inline-color">View<&sol;span><&sol;strong>&nbsp&semi;&colon; Used<em> <&sol;em>to<em> <&sol;em>find a parent view to hold Snackbar&&num;8217&semi;s view&period;<&sol;p>&NewLine;&NewLine;&NewLine;&NewLine;<p><strong><span style&equals;"color&colon;&num;04603e" class&equals;"has-inline-color">CharSequence<&sol;span><&sol;strong>&colon; The text to show on snackbar&period;<&sol;p>&NewLine;&NewLine;&NewLine;&NewLine;<p><strong><span style&equals;"color&colon;&num;04603e" class&equals;"has-inline-color">Duration<&sol;span><&sol;strong> &colon; How long to display the message&period;&nbsp&semi;It can be LENGTH&lowbar;SHORT&comma; LENGTH&lowbar;LONG&comma; LENGTH&lowbar;INDEFINITE&nbsp&semi;or a custom duration in milliseconds&period;<&sol;p>&NewLine;&NewLine;&NewLine;&NewLine;<ul class&equals;"wp-block-list"><li><em><strong><span style&equals;"color&colon;&num;530599" class&equals;"has-inline-color">LENGTH&lowbar;SHORT<&sol;span><&sol;strong>&colon; <&sol;em>Show the Snackbar for a short period of time&period;<&sol;li><&sol;ul>&NewLine;&NewLine;&NewLine;&NewLine;<ul class&equals;"wp-block-list"><li><em><strong><span style&equals;"color&colon;&num;530599" class&equals;"has-inline-color">LENGTH&lowbar;LONG<&sol;span><&sol;strong>&colon; <&sol;em>Show the Snackbar for a long period of time&period;<&sol;li><&sol;ul>&NewLine;&NewLine;&NewLine;&NewLine;<ul class&equals;"wp-block-list"><li><em><strong><span style&equals;"color&colon;&num;530599" class&equals;"has-inline-color">LENGTH&lowbar;INDEFINITE<&sol;span><&sol;strong>&colon; <&sol;em>Show the snackbar until it&&num;8217&semi;s either dismissed or another snackbar is shown&period;<&sol;li><&sol;ul>&NewLine;&NewLine;&NewLine;&NewLine;<p><strong><span style&equals;"color&colon;&num;04603e" class&equals;"has-inline-color">show&lpar;&rpar;<&sol;span><&sol;strong>&colon; Used to display the Snackbar on the screen&period;<&sol;p>&NewLine;&NewLine;&NewLine;&NewLine;<div class&equals;"wp-block-image"><figure class&equals;"aligncenter size-large is-resized"><img src&equals;"https&colon;&sol;&sol;c1ctech&period;com&sol;wp-content&sol;uploads&sol;2020&sol;09&sol;Screen-Shot-2020-09-10-at-12&period;13&period;30-PM&period;png" alt&equals;"" class&equals;"wp-image-2135" width&equals;"513" height&equals;"161"&sol;><figcaption><strong><span class&equals;"has-inline-color has-vivid-red-color">Android Default Snackbar<&sol;span><&sol;strong><&sol;figcaption><&sol;figure><&sol;div>&NewLine;&NewLine;&NewLine;&NewLine;<h3 class&equals;"wp-block-heading"><span style&equals;"color&colon;&num;520599" class&equals;"has-inline-color"><strong>Snackbar with Action<&sol;strong><&sol;span><&sol;h3>&NewLine;&NewLine;&NewLine;&NewLine;<p>You can add an action to a&nbsp&semi;Snackbar &comma; allowing the user to respond to your message&period; <&sol;p>&NewLine;&NewLine;&NewLine;&NewLine;<p>To add an action&comma; use the&nbsp&semi;<strong><span style&equals;"color&colon;&num;04603e" class&equals;"has-inline-color">setAction<&sol;span><&sol;strong>&nbsp&semi;method on the object returned from&nbsp&semi;<strong>make<&sol;strong>&period; Snackbars are automatically dismissed when the action is clicked&period;<&sol;p>&NewLine;&NewLine;&NewLine;&NewLine;<p><span style&equals;"color&colon;&num;530599" class&equals;"has-inline-color"><strong>setAction&lpar;&rpar;<&sol;strong><&sol;span>&colon; Set the action to be displayed in&nbsp&semi;the Snackbar&period; It takes two parameters&colon;<&sol;p>&NewLine;&NewLine;&NewLine;&NewLine;<ul class&equals;"wp-block-list"><li><strong><span style&equals;"color&colon;&num;04603e" class&equals;"has-inline-color">text<&sol;span><&sol;strong>&colon; Text to display for the action<&sol;li><&sol;ul>&NewLine;&NewLine;&NewLine;&NewLine;<ul class&equals;"wp-block-list"><li><strong><span style&equals;"color&colon;&num;04603e" class&equals;"has-inline-color">listener<&sol;span><&sol;strong>&colon; callback to be invoked when the action is clicked&period;<&sol;li><&sol;ul>&NewLine;&NewLine;&NewLine;&NewLine;<p>The below code snippet is an example of how to show a snackbar with a message and an action&colon;<&sol;p>&NewLine;&NewLine;&NewLine;&NewLine;<pre class&equals;"wp-block-preformatted">Snackbar snackbarWithAction &equals; Snackbar&period;<em>make<&sol;em>&lpar;<strong>constraintLayout<&sol;strong>&comma; <strong>"Contact removed"<&sol;strong>&comma; Snackbar&period;<strong><em>LENGTH&lowbar;LONG<&sol;em><&sol;strong>&rpar;&semi;&NewLine;&NewLine;<strong><span style&equals;"color&colon;&num;04603e" class&equals;"has-inline-color">&sol;&sol;get snackbar view<&sol;span><&sol;strong>&NewLine;View snackbarView &equals; snackbarWithAction&period;getView&lpar;&rpar;&semi;&NewLine;&NewLine;<strong><span style&equals;"color&colon;&num;04603e" class&equals;"has-inline-color">&sol;&sol;get snackbar child views<&sol;span><&sol;strong>&NewLine;TextView snackbarText &equals; &lpar;TextView&rpar; snackbarView&period;findViewById&lpar;R&period;id&period;<strong><em>snackbar&lowbar;text<&sol;em><&sol;strong>&rpar;&semi;&NewLine;TextView snackbarActionText &equals; &lpar;TextView&rpar; snackbarView&period;findViewById&lpar;R&period;id&period;<strong><em>snackbar&lowbar;action<&sol;em><&sol;strong>&rpar;&semi;&NewLine;&NewLine;<em><strong><span style&equals;"color&colon;&num;04603e" class&equals;"has-inline-color">&sol;&sol;change snackbar text color<&sol;span><&sol;strong>&NewLine;<&sol;em>snackbarText&period;setTextColor&lpar;&lpar;Color&period;<strong><em>CYAN<&sol;em><&sol;strong>&rpar;&rpar;&semi;&NewLine;&NewLine;<em><strong><span style&equals;"color&colon;&num;04603e" class&equals;"has-inline-color">&sol;&sol;change snackbar action button text color<&sol;span><&sol;strong>&NewLine;<&sol;em>snackbarActionText&period;setTextColor&lpar;&lpar;Color&period;<strong><em>RED<&sol;em><&sol;strong>&rpar;&rpar;&semi;&NewLine;&NewLine;<strong><span style&equals;"color&colon;&num;04603e" class&equals;"has-inline-color">&sol;&sol;use setAction&lpar;&rpar; to add action UNDO<&sol;span><&sol;strong>&NewLine;snackbarWithAction&period;<strong>setAction<&sol;strong>&lpar;<strong>"UNDO"<&sol;strong>&comma; <strong>new <&sol;strong>View&period;OnClickListener&lpar;&rpar; &lbrace;&NewLine; &commat;Override&NewLine; <strong>public void <&sol;strong>onClick&lpar;View view&rpar; &lbrace;&NewLine;&NewLine; Snackbar snackbar &equals; Snackbar&period;<em>make<&sol;em>&lpar;<strong>constraintLayout<&sol;strong>&comma; <strong>"The contact is restored"<&sol;strong>&comma; Snackbar&period;<strong><em>LENGTH&lowbar;LONG<&sol;em><&sol;strong>&rpar;&semi;&NewLine;&NewLine; View snackbarView &equals; snackbar&period;getView&lpar;&rpar;&semi;&NewLine;&NewLine; <em><strong><span style&equals;"color&colon;&num;04603e" class&equals;"has-inline-color">&sol;&sol;change snackbar background color<&sol;span><&sol;strong> <&sol;em>&NewLine;<em> <&sol;em> snackbarView&period;setBackgroundColor&lpar;getResources&lpar;&rpar;&NewLine; &period;getColor&lpar;R&period;color&period;<strong><em>green<&sol;em><&sol;strong>&rpar;&rpar;&semi;&NewLine;&NewLine; TextView snackbarText &equals; &lpar;TextView&rpar; snackbarView&period;findViewById&lpar;R&period;id&period;<strong><em>snackbar&lowbar;text<&sol;em><&sol;strong>&rpar;&semi;&NewLine;&NewLine; <em><strong><span style&equals;"color&colon;&num;04603e" class&equals;"has-inline-color">&sol;&sol;change snackbar text color<&sol;span><&sol;strong>&NewLine; <&sol;em>snackbarText&period;setTextColor&lpar;&lpar;Color&period;<strong><em>WHITE<&sol;em><&sol;strong>&rpar;&rpar;&semi;&NewLine;&NewLine; <em><strong><span style&equals;"color&colon;&num;04603e" class&equals;"has-inline-color">&sol;&sol;change snackbar text gravity<&sol;span><&sol;strong>&NewLine; <&sol;em><strong>if <&sol;strong>&lpar;Build&period;VERSION&period;<strong><em>SDK&lowbar;INT <&sol;em><&sol;strong>&gt&semi;&equals; Build&period;VERSION&lowbar;CODES&period;<strong><em>JELLY&lowbar;BEAN&lowbar;MR1<&sol;em><&sol;strong>&rpar;&NewLine; snackbarText&period;setTextAlignment&lpar;View&period;<strong><em>TEXT&lowbar;ALIGNMENT&lowbar;CENTER<&sol;em><&sol;strong>&rpar;&semi;&NewLine; <strong>else <&sol;strong>&lbrace;&NewLine; snackbarText&period;setGravity&lpar;Gravity&period;<strong><em>CENTER&lowbar;HORIZONTAL<&sol;em><&sol;strong>&rpar;&semi;&NewLine; &rcub;&NewLine; snackbar&period;show&lpar;&rpar;&semi;&NewLine; &rcub;&NewLine;&rcub;&rpar;&semi;&NewLine;<strong><span style&equals;"color&colon;&num;04603e" class&equals;"has-inline-color">&sol;&sol;show snackbar<&sol;span><&sol;strong>&NewLine;snackbarWithAction&period;show&lpar;&rpar;&semi;<&sol;pre>&NewLine;&NewLine;&NewLine;&NewLine;<div class&equals;"wp-block-image"><figure class&equals;"aligncenter size-large is-resized"><img src&equals;"https&colon;&sol;&sol;c1ctech&period;com&sol;wp-content&sol;uploads&sol;2020&sol;09&sol;Screen-Shot-2020-09-10-at-12&period;14&period;02-PM&period;png" alt&equals;"" class&equals;"wp-image-2136" width&equals;"516" height&equals;"202"&sol;><figcaption><strong><span class&equals;"has-inline-color has-vivid-red-color">Snackbar with Action<&sol;span><&sol;strong><&sol;figcaption><&sol;figure><&sol;div>&NewLine;&NewLine;&NewLine;&NewLine;<div class&equals;"wp-block-image"><figure class&equals;"aligncenter size-large is-resized"><img src&equals;"https&colon;&sol;&sol;c1ctech&period;com&sol;wp-content&sol;uploads&sol;2020&sol;09&sol;Screen-Shot-2020-09-10-at-12&period;14&period;26-PM&period;png" alt&equals;"" class&equals;"wp-image-2137" width&equals;"518" height&equals;"174"&sol;><figcaption><strong><span class&equals;"has-inline-color has-vivid-red-color">Show snackbar with text &lpar;center&rpar; on click of UNDO button<&sol;span><&sol;strong><&sol;figcaption><&sol;figure><&sol;div>&NewLine;&NewLine;&NewLine;&NewLine;<h3 class&equals;"wp-block-heading"><span style&equals;"color&colon;&num;520599" class&equals;"has-inline-color"><strong>Snackbar with Custom View<&sol;strong><&sol;span><&sol;h3>&NewLine;&NewLine;&NewLine;&NewLine;<p>Android Snackbar allows you to create a customized layout for your snackbar&period; <&sol;p>&NewLine;&NewLine;&NewLine;&NewLine;<p>To create a custom layout&comma; define a View layout&comma; in XML&period; The following snippet contains a customized layout for snackbar &lpar;saved as&nbsp&semi;layout&sol;custom&lowbar;snackbar&period;xml&rpar;&colon;<&sol;p>&NewLine;&NewLine;&NewLine;&NewLine;<pre class&equals;"wp-block-preformatted">&lt&semi;<strong>androidx&period;constraintlayout&period;widget&period;ConstraintLayout xmlns&colon;android&equals;"http&colon;&sol;&sol;schemas&period;android&period;com&sol;apk&sol;res&sol;android"&NewLine; xmlns&colon;app&equals;"http&colon;&sol;&sol;schemas&period;android&period;com&sol;apk&sol;res-auto"&NewLine; android&colon;layout&lowbar;width&equals;"match&lowbar;parent"&NewLine; android&colon;layout&lowbar;height&equals;"wrap&lowbar;content"&NewLine; android&colon;id&equals;"&commat;&plus;id&sol;custom&lowbar;snackbar&lowbar;container"&NewLine; android&colon;background&equals;"&commat;android&colon;color&sol;holo&lowbar;orange&lowbar;light"<&sol;strong>&gt&semi;&NewLine;&NewLine; &lt&semi;<strong>ImageView&NewLine; android&colon;id&equals;"&commat;&plus;id&sol;iv&lowbar;custom&lowbar;view"&NewLine; android&colon;layout&lowbar;width&equals;"wrap&lowbar;content"&NewLine; android&colon;layout&lowbar;height&equals;"wrap&lowbar;content"&NewLine; android&colon;layout&lowbar;marginRight&equals;"&commat;dimen&sol;margin&lowbar;10dp"&NewLine; app&colon;layout&lowbar;constraintBottom&lowbar;toBottomOf&equals;"parent"&NewLine; app&colon;layout&lowbar;constraintEnd&lowbar;toStartOf&equals;"&commat;&plus;id&sol;tv&lowbar;custom&lowbar;view"&NewLine; app&colon;layout&lowbar;constraintHorizontal&lowbar;bias&equals;"0&period;5"&NewLine; app&colon;layout&lowbar;constraintHorizontal&lowbar;chainStyle&equals;"packed"&NewLine; app&colon;layout&lowbar;constraintStart&lowbar;toStartOf&equals;"parent"&NewLine; app&colon;layout&lowbar;constraintTop&lowbar;toTopOf&equals;"parent"&NewLine; app&colon;srcCompat&equals;"&commat;android&colon;drawable&sol;ic&lowbar;dialog&lowbar;alert" <&sol;strong>&sol;&gt&semi;&NewLine;&NewLine; &lt&semi;<strong>TextView&NewLine; android&colon;id&equals;"&commat;&plus;id&sol;tv&lowbar;custom&lowbar;view"&NewLine; android&colon;layout&lowbar;width&equals;"wrap&lowbar;content"&NewLine; android&colon;layout&lowbar;height&equals;"wrap&lowbar;content"&NewLine; android&colon;text&equals;"&commat;string&sol;no&lowbar;external&lowbar;storage&lowbar;available"&NewLine; android&colon;textColor&equals;"&commat;android&colon;color&sol;black"&NewLine; android&colon;textSize&equals;"&commat;dimen&sol;textsize&lowbar;15sp"&NewLine; app&colon;layout&lowbar;constraintBottom&lowbar;toBottomOf&equals;"parent"&NewLine; app&colon;layout&lowbar;constraintEnd&lowbar;toEndOf&equals;"parent"&NewLine; app&colon;layout&lowbar;constraintHorizontal&lowbar;bias&equals;"0&period;5"&NewLine; app&colon;layout&lowbar;constraintStart&lowbar;toEndOf&equals;"&commat;&plus;id&sol;iv&lowbar;custom&lowbar;view"&NewLine; app&colon;layout&lowbar;constraintTop&lowbar;toTopOf&equals;"parent" <&sol;strong>&sol;&gt&semi;&NewLine;&NewLine;&lt&semi;&sol;<strong>androidx&period;constraintlayout&period;widget&period;ConstraintLayout<&sol;strong>&gt&semi;<&sol;pre>&NewLine;&NewLine;&NewLine;&NewLine;<p>You must use the ID of the ConstraintLayout element &lpar; &&num;8220&semi;<strong>custom&lowbar;snackbar&lowbar;container<&sol;strong>&&num;8220&semi;&rpar; and the ID of the XML layout file &&num;8220&semi;<strong>custom&lowbar;snackbar&period;xml<&sol;strong>&&num;8221&semi; to inflate the layout&comma; as shown here&colon;<&sol;p>&NewLine;&NewLine;&NewLine;&NewLine;<pre class&equals;"wp-block-preformatted"><em><strong><span style&equals;"color&colon;&num;04603e" class&equals;"has-inline-color">&sol;&sol;get created custom layout of snackbar<&sol;span><&sol;strong>&NewLine;<&sol;em>View <strong>customSnackBarView<&sol;strong> &equals; getLayoutInflater&lpar;&rpar;&period;inflate&lpar;R&period;layout&period;<strong><em>custom&lowbar;snackbar<&sol;em><&sol;strong>&comma; &lpar;ViewGroup&rpar; findViewById&lpar;R&period;id&period;<strong><em>custom&lowbar;snackbar&lowbar;container<&sol;em><&sol;strong>&rpar;&rpar;&semi;<&sol;pre>&NewLine;&NewLine;&NewLine;&NewLine;<p>Now pass this&nbsp&semi;<strong>View<&sol;strong> object &lpar;created using custom layout&rpar; to the&nbsp&semi;<strong>addView&lpar;&rpar;<&sol;strong> method&period;<&sol;p>&NewLine;&NewLine;&NewLine;&NewLine;<pre class&equals;"wp-block-preformatted">Snackbar customSnackBar &equals; Snackbar&period;<em>make<&sol;em>&lpar;<strong>constraintLayout<&sol;strong>&comma; <strong>""<&sol;strong>&comma; Snackbar&period;<strong><em>LENGTH&lowbar;LONG<&sol;em><&sol;strong>&rpar;&semi;&NewLine;&NewLine;<strong>Snackbar&period;SnackbarLayout layout &equals; &lpar;Snackbar&period;SnackbarLayout&rpar; customSnackBar&period;getView&lpar;&rpar;&semi;<&sol;strong>&NewLine;&NewLine;<em><span style&equals;"color&colon;&num;04603e" class&equals;"has-inline-color"><strong>&sol;&sol;get snackbar custom layout<&sol;strong><&sol;span>&NewLine;<&sol;em>View customSnackBarView &equals; getLayoutInflater&lpar;&rpar;&period;inflate&lpar;R&period;layout&period;<strong><em>custom&lowbar;snackbar<&sol;em><&sol;strong>&comma; &lpar;ViewGroup&rpar; findViewById&lpar;R&period;id&period;<strong><em>custom&lowbar;snackbar&lowbar;container<&sol;em><&sol;strong>&rpar;&rpar;&semi;&NewLine;&NewLine;<em><strong><span style&equals;"color&colon;&num;04603e" class&equals;"has-inline-color">&sol;&sol;add snackbar custom layout<&sol;span><&sol;strong>&NewLine;<&sol;em><strong>layout&period;addView&lpar;customSnackBarView&comma; 0&rpar;&semi;<&sol;strong>&NewLine;&NewLine;customSnackBar&period;show&lpar;&rpar;&semi;<&sol;pre>&NewLine;&NewLine;&NewLine;&NewLine;<div class&equals;"wp-block-image"><figure class&equals;"aligncenter size-large is-resized"><img src&equals;"https&colon;&sol;&sol;c1ctech&period;com&sol;wp-content&sol;uploads&sol;2020&sol;09&sol;Screen-Shot-2020-09-10-at-12&period;14&period;49-PM&period;png" alt&equals;"" class&equals;"wp-image-2138" width&equals;"524" height&equals;"182"&sol;><figcaption><strong><span class&equals;"has-inline-color has-vivid-red-color">Snackbar with custom layout<&sol;span><&sol;strong><&sol;figcaption><&sol;figure><&sol;div>&NewLine;&NewLine;&NewLine;&NewLine;<p><&sol;p>&NewLine;&NewLine;&NewLine;&NewLine;<h3 class&equals;"wp-block-heading"><strong><span style&equals;"color&colon;&num;520599" class&equals;"has-inline-color">Creating New Project<&sol;span><&sol;strong><&sol;h3>&NewLine;&NewLine;&NewLine;&NewLine;<p><strong>1<&sol;strong>&period; In&nbsp&semi;<strong><span style&equals;"color&colon;&num;04603e" class&equals;"has-inline-color">Android Studio<&sol;span><&sol;strong>&comma; go to&nbsp&semi;<strong><span style&equals;"color&colon;&num;04603e" class&equals;"has-inline-color">File &Implies; New Project<&sol;span><&sol;strong>&comma; fill all the details required to create a new project and then click on <strong><span style&equals;"color&colon;&num;04603e" class&equals;"has-inline-color">finish<&sol;span><&sol;strong>&period;<&sol;p>&NewLine;&NewLine;&NewLine;&NewLine;<p><strong>2<&sol;strong>&period; Open&nbsp&semi;<strong><span style&equals;"color&colon;&num;04603e" class&equals;"has-inline-color">build&period;gradle<&sol;span><&sol;strong> &lpar;app level&rpar;&comma; add the material dependency as shown below and then <strong>sync<&sol;strong> your project&colon;<&sol;p>&NewLine;&NewLine;&NewLine;&NewLine;<p><strong><span style&equals;"color&colon;&num;520599" class&equals;"has-inline-color">build&period;gradle<&sol;span><&sol;strong><&sol;p>&NewLine;&NewLine;&NewLine;&NewLine;<pre class&equals;"wp-block-preformatted"><code>dependencies <strong>&lbrace;&NewLine; <span style&equals;"color&colon;&num;04603e" class&equals;"has-inline-color"> implementation 'com&period;google&period;android&period;material&colon;material&colon;1&period;2&period;0'<&sol;span>&NewLine;&rcub;<&sol;strong><&sol;code><&sol;pre>&NewLine;&NewLine;&NewLine;&NewLine;<p><strong>3<&sol;strong>&period; Rename the layout file <strong>activity&lowbar;main&period;xml<&sol;strong> as <strong><span style&equals;"color&colon;&num;04603e" class&equals;"has-inline-color">activity&lowbar;snackbar&period;xml<&sol;span><&sol;strong> and add the below code&period; This layout file consist of three buttons&comma; <strong>SIMPLE SNACKBAR<&sol;strong> &lpar;To show Android standard snackbar&rpar;&comma; <strong>SNACKBAR WITH ACTION<&sol;strong> &lpar;To show snackbar with action&rpar;&comma; <strong>SNACKBAR WITH CUSTOM VIEW<&sol;strong> &lpar;To show snackbar with custom view&rpar;&period;<&sol;p>&NewLine;&NewLine;&NewLine;&NewLine;<p><strong><span style&equals;"color&colon;&num;520599" class&equals;"has-inline-color">activity&lowbar;snackbar&period;xml<&sol;span><&sol;strong><&sol;p>&NewLine;&NewLine;&NewLine;&NewLine;<pre class&equals;"wp-block-preformatted"><em>&lt&semi;&quest;<&sol;em><strong>xml version&equals;"1&period;0" encoding&equals;"utf-8"<&sol;strong><em>&quest;&gt&semi;&NewLine;<&sol;em>&lt&semi;<strong>androidx&period;constraintlayout&period;widget&period;ConstraintLayout xmlns&colon;android&equals;"http&colon;&sol;&sol;schemas&period;android&period;com&sol;apk&sol;res&sol;android"&NewLine; xmlns&colon;app&equals;"http&colon;&sol;&sol;schemas&period;android&period;com&sol;apk&sol;res-auto"&NewLine; xmlns&colon;tools&equals;"http&colon;&sol;&sol;schemas&period;android&period;com&sol;tools"&NewLine; android&colon;id&equals;"&commat;&plus;id&sol;constraintLayout"&NewLine; android&colon;layout&lowbar;width&equals;"match&lowbar;parent"&NewLine; android&colon;layout&lowbar;height&equals;"match&lowbar;parent"&NewLine; android&colon;layout&lowbar;margin&equals;"&commat;dimen&sol;margin&lowbar;20dp"&NewLine; tools&colon;context&equals;"&period;SnackbarActivityJava"<&sol;strong>&gt&semi;&NewLine;&NewLine; &lt&semi;<strong>Button&NewLine; android&colon;id&equals;"&commat;&plus;id&sol;btn&lowbar;simple&lowbar;snackbar"&NewLine; android&colon;layout&lowbar;width&equals;"match&lowbar;parent"&NewLine; android&colon;layout&lowbar;height&equals;"wrap&lowbar;content"&NewLine; android&colon;text&equals;"&commat;string&sol;simple&lowbar;snackbar"&NewLine; android&colon;padding&equals;"&commat;dimen&sol;padding&lowbar;12dp"&NewLine; app&colon;layout&lowbar;constraintBottom&lowbar;toTopOf&equals;"&commat;&plus;id&sol;btn&lowbar;snackbar&lowbar;with&lowbar;action"&NewLine; app&colon;layout&lowbar;constraintEnd&lowbar;toEndOf&equals;"parent"&NewLine; app&colon;layout&lowbar;constraintStart&lowbar;toStartOf&equals;"parent"&NewLine; app&colon;layout&lowbar;constraintTop&lowbar;toTopOf&equals;"parent"&NewLine; app&colon;layout&lowbar;constraintVertical&lowbar;chainStyle&equals;"packed" <&sol;strong>&sol;&gt&semi;&NewLine;&NewLine;&NewLine; &lt&semi;<strong>Button&NewLine; android&colon;id&equals;"&commat;&plus;id&sol;btn&lowbar;snackbar&lowbar;with&lowbar;action"&NewLine; android&colon;layout&lowbar;width&equals;"match&lowbar;parent"&NewLine; android&colon;layout&lowbar;height&equals;"wrap&lowbar;content"&NewLine; android&colon;layout&lowbar;marginTop&equals;"&commat;dimen&sol;margin&lowbar;10dp"&NewLine; android&colon;layout&lowbar;marginBottom&equals;"&commat;dimen&sol;margin&lowbar;10dp"&NewLine; android&colon;text&equals;"&commat;string&sol;snackbar&lowbar;with&lowbar;action"&NewLine; android&colon;padding&equals;"&commat;dimen&sol;padding&lowbar;12dp"&NewLine; app&colon;layout&lowbar;constraintBottom&lowbar;toTopOf&equals;"&commat;&plus;id&sol;btn&lowbar;snackbar&lowbar;with&lowbar;custom&lowbar;view"&NewLine; app&colon;layout&lowbar;constraintEnd&lowbar;toEndOf&equals;"&commat;&plus;id&sol;btn&lowbar;simple&lowbar;snackbar"&NewLine; app&colon;layout&lowbar;constraintHorizontal&lowbar;bias&equals;"0&period;5"&NewLine; app&colon;layout&lowbar;constraintStart&lowbar;toStartOf&equals;"&commat;&plus;id&sol;btn&lowbar;simple&lowbar;snackbar"&NewLine; app&colon;layout&lowbar;constraintTop&lowbar;toBottomOf&equals;"&commat;&plus;id&sol;btn&lowbar;simple&lowbar;snackbar" <&sol;strong>&sol;&gt&semi;&NewLine;&NewLine; &lt&semi;<strong>Button&NewLine; android&colon;id&equals;"&commat;&plus;id&sol;btn&lowbar;snackbar&lowbar;with&lowbar;custom&lowbar;view"&NewLine; android&colon;layout&lowbar;width&equals;"match&lowbar;parent"&NewLine; android&colon;layout&lowbar;height&equals;"wrap&lowbar;content"&NewLine; android&colon;text&equals;"&commat;string&sol;snackbar&lowbar;with&lowbar;custom&lowbar;view"&NewLine; android&colon;padding&equals;"&commat;dimen&sol;padding&lowbar;12dp"&NewLine; app&colon;layout&lowbar;constraintBottom&lowbar;toBottomOf&equals;"parent"&NewLine; app&colon;layout&lowbar;constraintEnd&lowbar;toEndOf&equals;"&commat;&plus;id&sol;btn&lowbar;snackbar&lowbar;with&lowbar;action"&NewLine; app&colon;layout&lowbar;constraintHorizontal&lowbar;bias&equals;"0&period;5"&NewLine; app&colon;layout&lowbar;constraintStart&lowbar;toStartOf&equals;"&commat;&plus;id&sol;btn&lowbar;snackbar&lowbar;with&lowbar;action"&NewLine; app&colon;layout&lowbar;constraintTop&lowbar;toBottomOf&equals;"&commat;&plus;id&sol;btn&lowbar;snackbar&lowbar;with&lowbar;action" <&sol;strong>&sol;&gt&semi;&NewLine;&NewLine;&lt&semi;&sol;<strong>androidx&period;constraintlayout&period;widget&period;ConstraintLayout<&sol;strong>&gt&semi;<&sol;pre>&NewLine;&NewLine;&NewLine;&NewLine;<p><strong>4&period; <&sol;strong>Create a new layout file <strong>custom&lowbar;snackbar&period;xml<&sol;strong> &lpar;layout-&gt&semi;New-&gt&semi;Layout Resource File&rpar; and add the below code&period; This layout file represents the custom layout of snackbar&period;<&sol;p>&NewLine;&NewLine;&NewLine;&NewLine;<p><strong><span style&equals;"color&colon;&num;520599" class&equals;"has-inline-color">custom&lowbar;snackbar&period;xml<&sol;span><&sol;strong><&sol;p>&NewLine;<&excl;-- WP QUADS Content Ad Plugin v&period; 2&period;0&period;98&period;1 -->&NewLine;<div class&equals;"quads-location quads-ad2" id&equals;"quads-ad2" style&equals;"float&colon;none&semi;margin&colon;0px&semi;">&NewLine;&NewLine;<&sol;div>&NewLine;&NewLine;&NewLine;&NewLine;&NewLine;<pre class&equals;"wp-block-preformatted"><em>&lt&semi;&quest;<&sol;em><strong>xml version<&sol;strong><strong>&equals;"1&period;0" <&sol;strong><strong>encoding<&sol;strong><strong>&equals;"utf-8"<&sol;strong><em>&quest;&gt&semi;<br><&sol;em>&lt&semi;<strong>androidx&period;constraintlayout&period;widget&period;ConstraintLayout <&sol;strong><strong>xmlns&colon;<&sol;strong><strong>android<&sol;strong><strong>&equals;"http&colon;&sol;&sol;schemas&period;android&period;com&sol;apk&sol;res&sol;android"<br><&sol;strong><strong> <&sol;strong><strong>xmlns&colon;<&sol;strong><strong>app<&sol;strong><strong>&equals;"http&colon;&sol;&sol;schemas&period;android&period;com&sol;apk&sol;res-auto"<br><&sol;strong><strong> <&sol;strong><strong>android<&sol;strong><strong>&colon;layout&lowbar;width<&sol;strong><strong>&equals;"match&lowbar;parent"<br><&sol;strong><strong> <&sol;strong><strong>android<&sol;strong><strong>&colon;layout&lowbar;height<&sol;strong><strong>&equals;"wrap&lowbar;content"<br><&sol;strong><strong> <&sol;strong><strong>android<&sol;strong><strong>&colon;id<&sol;strong><strong>&equals;"&commat;&plus;id&sol;custom&lowbar;snackbar&lowbar;container"<br><&sol;strong><strong> <&sol;strong><strong>android<&sol;strong><strong>&colon;background<&sol;strong><strong>&equals;"&commat;android&colon;color&sol;holo&lowbar;orange&lowbar;light"<&sol;strong>&gt&semi;<br><br> &lt&semi;<strong>ImageView<br><&sol;strong><strong> <&sol;strong><strong>android<&sol;strong><strong>&colon;id<&sol;strong><strong>&equals;"&commat;&plus;id&sol;iv&lowbar;custom&lowbar;view"<br><&sol;strong><strong> <&sol;strong><strong>android<&sol;strong><strong>&colon;layout&lowbar;width<&sol;strong><strong>&equals;"wrap&lowbar;content"<br><&sol;strong><strong> <&sol;strong><strong>android<&sol;strong><strong>&colon;layout&lowbar;height<&sol;strong><strong>&equals;"wrap&lowbar;content"<br><&sol;strong><strong> <&sol;strong><strong>android<&sol;strong><strong>&colon;layout&lowbar;marginRight<&sol;strong><strong>&equals;"&commat;dimen&sol;margin&lowbar;10dp"<br><&sol;strong><strong> <&sol;strong><strong>app<&sol;strong><strong>&colon;layout&lowbar;constraintBottom&lowbar;toBottomOf<&sol;strong><strong>&equals;"parent"<br><&sol;strong><strong> <&sol;strong><strong>app<&sol;strong><strong>&colon;layout&lowbar;constraintEnd&lowbar;toStartOf<&sol;strong><strong>&equals;"&commat;&plus;id&sol;tv&lowbar;custom&lowbar;view"<br><&sol;strong><strong> <&sol;strong><strong>app<&sol;strong><strong>&colon;layout&lowbar;constraintHorizontal&lowbar;bias<&sol;strong><strong>&equals;"0&period;5"<br><&sol;strong><strong> <&sol;strong><strong>app<&sol;strong><strong>&colon;layout&lowbar;constraintHorizontal&lowbar;chainStyle<&sol;strong><strong>&equals;"packed"<br><&sol;strong><strong> <&sol;strong><strong>app<&sol;strong><strong>&colon;layout&lowbar;constraintStart&lowbar;toStartOf<&sol;strong><strong>&equals;"parent"<br><&sol;strong><strong> <&sol;strong><strong>app<&sol;strong><strong>&colon;layout&lowbar;constraintTop&lowbar;toTopOf<&sol;strong><strong>&equals;"parent"<br><&sol;strong><strong> <&sol;strong><strong>app<&sol;strong><strong>&colon;srcCompat<&sol;strong><strong>&equals;"&commat;android&colon;drawable&sol;ic&lowbar;dialog&lowbar;alert" <&sol;strong>&sol;&gt&semi;<br><br> &lt&semi;<strong>TextView<br><&sol;strong><strong> <&sol;strong><strong>android<&sol;strong><strong>&colon;id<&sol;strong><strong>&equals;"&commat;&plus;id&sol;tv&lowbar;custom&lowbar;view"<br><&sol;strong><strong> <&sol;strong><strong>android<&sol;strong><strong>&colon;layout&lowbar;width<&sol;strong><strong>&equals;"wrap&lowbar;content"<br><&sol;strong><strong> <&sol;strong><strong>android<&sol;strong><strong>&colon;layout&lowbar;height<&sol;strong><strong>&equals;"wrap&lowbar;content"<br><&sol;strong><strong> <&sol;strong><strong>android<&sol;strong><strong>&colon;text<&sol;strong><strong>&equals;"&commat;string&sol;no&lowbar;external&lowbar;storage&lowbar;available"<br><&sol;strong><strong> <&sol;strong><strong>android<&sol;strong><strong>&colon;textColor<&sol;strong><strong>&equals;"&commat;android&colon;color&sol;black"<br><&sol;strong><strong> <&sol;strong><strong>android<&sol;strong><strong>&colon;textSize<&sol;strong><strong>&equals;"&commat;dimen&sol;textsize&lowbar;15sp"<br><&sol;strong><strong> <&sol;strong><strong>app<&sol;strong><strong>&colon;layout&lowbar;constraintBottom&lowbar;toBottomOf<&sol;strong><strong>&equals;"parent"<br><&sol;strong><strong> <&sol;strong><strong>app<&sol;strong><strong>&colon;layout&lowbar;constraintEnd&lowbar;toEndOf<&sol;strong><strong>&equals;"parent"<br><&sol;strong><strong> <&sol;strong><strong>app<&sol;strong><strong>&colon;layout&lowbar;constraintHorizontal&lowbar;bias<&sol;strong><strong>&equals;"0&period;5"<br><&sol;strong><strong> <&sol;strong><strong>app<&sol;strong><strong>&colon;layout&lowbar;constraintStart&lowbar;toEndOf<&sol;strong><strong>&equals;"&commat;&plus;id&sol;iv&lowbar;custom&lowbar;view"<br><&sol;strong><strong> <&sol;strong><strong>app<&sol;strong><strong>&colon;layout&lowbar;constraintTop&lowbar;toTopOf<&sol;strong><strong>&equals;"parent" <&sol;strong>&sol;&gt&semi;<br><br>&lt&semi;&sol;<strong>androidx&period;constraintlayout&period;widget&period;ConstraintLayout<&sol;strong>&gt&semi;<&sol;pre>&NewLine;&NewLine;&NewLine;&NewLine;<h4 class&equals;"wp-block-heading"><span style&equals;"color&colon;&num;520599" class&equals;"has-inline-color"><strong>Complete code in Java<&sol;strong><&sol;span><&sol;h4>&NewLine;&NewLine;&NewLine;&NewLine;<p>Create a new activity <strong><span style&equals;"color&colon;&num;04603e" class&equals;"has-inline-color">SnackbarActivityJava&period;java<&sol;span><&sol;strong> This activity contains three buttons with click listener to show the different ways of implementing the Snackbar in <strong>Java<&sol;strong>&period;<&sol;p>&NewLine;&NewLine;&NewLine;&NewLine;<p><strong><span style&equals;"color&colon;&num;520599" class&equals;"has-inline-color">SnackbarActivityJava&period;java<&sol;span><&sol;strong><&sol;p>&NewLine;&NewLine;&NewLine;&NewLine;<pre class&equals;"wp-block-preformatted"><strong>package <&sol;strong>com&period;c1ctech&period;androidsnackbardemo&semi;&NewLine;&NewLine;<strong>import <&sol;strong>android&period;graphics&period;Color&semi;&NewLine;<strong>import <&sol;strong>android&period;os&period;Build&semi;&NewLine;<strong>import <&sol;strong>android&period;os&period;Bundle&semi;&NewLine;<strong>import <&sol;strong>android&period;view&period;Gravity&semi;&NewLine;<strong>import <&sol;strong>android&period;view&period;View&semi;&NewLine;<strong>import <&sol;strong>android&period;view&period;ViewGroup&semi;&NewLine;<strong>import <&sol;strong>android&period;widget&period;Button&semi;&NewLine;<strong>import <&sol;strong>android&period;widget&period;ImageView&semi;&NewLine;<strong>import <&sol;strong>android&period;widget&period;TextView&semi;&NewLine;&NewLine;<strong>import <&sol;strong>com&period;google&period;android&period;material&period;snackbar&period;Snackbar&semi;&NewLine;&NewLine;<strong>import <&sol;strong>androidx&period;appcompat&period;app&period;AppCompatActivity&semi;&NewLine;<strong>import <&sol;strong>androidx&period;constraintlayout&period;widget&period;ConstraintLayout&semi;&NewLine;&NewLine;<strong>public class <&sol;strong>SnackbarActivityJava <strong>extends <&sol;strong>AppCompatActivity <strong>implements <&sol;strong>View&period;OnClickListener &lbrace;&NewLine;&NewLine; <strong>private <&sol;strong>Button <strong>btnSimpleSnackbar<&sol;strong>&comma; <strong>btnSnackbarWithAction<&sol;strong>&comma; <strong>btnCustomSnackbar<&sol;strong>&semi;&NewLine; <strong>private <&sol;strong>ConstraintLayout <strong>constraintLayout<&sol;strong>&semi;&NewLine;&NewLine; &commat;Override&NewLine; <strong>protected void <&sol;strong>onCreate&lpar;Bundle savedInstanceState&rpar; &lbrace;&NewLine; <strong>super<&sol;strong>&period;onCreate&lpar;savedInstanceState&rpar;&semi;&NewLine; setContentView&lpar;R&period;layout&period;<strong><em>activity&lowbar;snackbar<&sol;em><&sol;strong>&rpar;&semi;&NewLine;&NewLine; <em><strong><span style&equals;"color&colon;&num;04603e" class&equals;"has-inline-color">&sol;&sol;get parent view by its id<&sol;span><&sol;strong>&NewLine; <&sol;em><strong>constraintLayout <&sol;strong>&equals; findViewById&lpar;R&period;id&period;<strong><em>constraintLayout<&sol;em><&sol;strong>&rpar;&semi;&NewLine;&NewLine; <em><strong><span style&equals;"color&colon;&num;04603e" class&equals;"has-inline-color">&sol;&sol;get child views of constraintLayout by its id<&sol;span><&sol;strong>&NewLine; <&sol;em><strong>btnSimpleSnackbar <&sol;strong>&equals; findViewById&lpar;R&period;id&period;<strong><em>btn&lowbar;simple&lowbar;snackbar<&sol;em><&sol;strong>&rpar;&semi;&NewLine; <strong>btnSnackbarWithAction <&sol;strong>&equals; findViewById&lpar;R&period;id&period;<strong><em>btn&lowbar;snackbar&lowbar;with&lowbar;action<&sol;em><&sol;strong>&rpar;&semi;&NewLine; <strong>btnCustomSnackbar <&sol;strong>&equals; findViewById&lpar;R&period;id&period;<strong><em>btn&lowbar;snackbar&lowbar;with&lowbar;custom&lowbar;view<&sol;em><&sol;strong>&rpar;&semi;&NewLine;&NewLine; <em><span style&equals;"color&colon;&num;04603e" class&equals;"has-inline-color"><strong>&sol;&sol;setting listener to each button<&sol;strong><&sol;span>&NewLine; <&sol;em><strong>btnSimpleSnackbar<&sol;strong>&period;setOnClickListener&lpar;<strong>this<&sol;strong>&rpar;&semi;&NewLine; <strong>btnSnackbarWithAction<&sol;strong>&period;setOnClickListener&lpar;<strong>this<&sol;strong>&rpar;&semi;&NewLine; <strong>btnCustomSnackbar<&sol;strong>&period;setOnClickListener&lpar;<strong>this<&sol;strong>&rpar;&semi;&NewLine; &rcub;&NewLine;&NewLine; &commat;Override&NewLine; <strong>public void <&sol;strong>onClick&lpar;View view&rpar; &lbrace;&NewLine;&NewLine; <strong>int <&sol;strong>id &equals; view&period;getId&lpar;&rpar;&semi;&NewLine; <strong>switch <&sol;strong>&lpar;id&rpar; &lbrace;&NewLine; <strong>case <&sol;strong>R&period;id&period;<strong><em>btn&lowbar;simple&lowbar;snackbar<&sol;em><&sol;strong>&colon;&NewLine; Snackbar&period;<em>make<&sol;em>&lpar;<strong>constraintLayout<&sol;strong>&comma; <strong>"Simple Snackbar "<&sol;strong>&comma; Snackbar&period;<strong><em>LENGTH&lowbar;LONG<&sol;em><&sol;strong>&rpar;&period;show&lpar;&rpar;&semi;&NewLine; <strong>break<&sol;strong>&semi;&NewLine;&NewLine; <strong>case <&sol;strong>R&period;id&period;<strong><em>btn&lowbar;snackbar&lowbar;with&lowbar;action<&sol;em><&sol;strong>&colon;&NewLine; Snackbar snackbarWithAction &equals; Snackbar&period;<em>make<&sol;em>&lpar;<strong>constraintLayout<&sol;strong>&comma; <strong>"Contact removed"<&sol;strong>&comma; Snackbar&period;<strong><em>LENGTH&lowbar;LONG<&sol;em><&sol;strong>&rpar;&semi;&NewLine;&NewLine; <em><strong><span style&equals;"color&colon;&num;04603e" class&equals;"has-inline-color">&sol;&sol;get snackbar root view<&sol;span><&sol;strong>&NewLine; <&sol;em>View snackbarView &equals; snackbarWithAction&period;getView&lpar;&rpar;&semi;&NewLine;&NewLine; <em><span style&equals;"color&colon;&num;04603e" class&equals;"has-inline-color"><strong>&sol;&sol;getting child views of snackbar<&sol;strong><&sol;span>&NewLine; <&sol;em>TextView snackbarText &equals; &lpar;TextView&rpar; snackbarView&period;findViewById&lpar;R&period;id&period;<strong><em>snackbar&lowbar;text<&sol;em><&sol;strong>&rpar;&semi;&NewLine; TextView snackbarActionText &equals; &lpar;TextView&rpar; snackbarView&period;findViewById&lpar;R&period;id&period;<strong><em>snackbar&lowbar;action<&sol;em><&sol;strong>&rpar;&semi;&NewLine;&NewLine; <em><strong><span style&equals;"color&colon;&num;04603e" class&equals;"has-inline-color">&sol;&sol;change snackbar text color<&sol;span><&sol;strong>&NewLine; <&sol;em>snackbarText&period;setTextColor&lpar;&lpar;Color&period;<strong><em>CYAN<&sol;em><&sol;strong>&rpar;&rpar;&semi;&NewLine;&NewLine; <em><strong><span style&equals;"color&colon;&num;04603e" class&equals;"has-inline-color">&sol;&sol;change snackbar action button text color<&sol;span><&sol;strong>&NewLine; <&sol;em>snackbarActionText&period;setTextColor&lpar;&lpar;Color&period;<strong><em>RED<&sol;em><&sol;strong>&rpar;&rpar;&semi;&NewLine;&NewLine; snackbarWithAction&period;setAction&lpar;<strong>"UNDO"<&sol;strong>&comma; <strong>new <&sol;strong>View&period;OnClickListener&lpar;&rpar; &lbrace;&NewLine; &commat;Override&NewLine; <strong>public void <&sol;strong>onClick&lpar;View view&rpar; &lbrace;&NewLine;&NewLine; Snackbar snackbar &equals; Snackbar&period;<em>make<&sol;em>&lpar;<strong>constraintLayout<&sol;strong>&comma; <strong>"The contact is restored"<&sol;strong>&comma; Snackbar&period;<strong><em>LENGTH&lowbar;LONG<&sol;em><&sol;strong>&rpar;&semi;&NewLine;&NewLine; View snackbarView &equals; snackbar&period;getView&lpar;&rpar;&semi;&NewLine;&NewLine; <em><strong><span style&equals;"color&colon;&num;04603e" class&equals;"has-inline-color">&sol;&sol;change snackbar background color<&sol;span><&sol;strong>&NewLine; <&sol;em>snackbarView&period;setBackgroundColor&lpar;getResources&lpar;&rpar;&period;getColor&lpar;R&period;color&period;<strong><em>green<&sol;em><&sol;strong>&rpar;&rpar;&semi;&NewLine;&NewLine; TextView snackbarText &equals; &lpar;TextView&rpar; snackbarView&period;findViewById&lpar;R&period;id&period;<strong><em>snackbar&lowbar;text<&sol;em><&sol;strong>&rpar;&semi;&NewLine;&NewLine; <em><strong><span style&equals;"color&colon;&num;04603e" class&equals;"has-inline-color">&sol;&sol;change snackbar text color<&sol;span><&sol;strong>&NewLine; <&sol;em>snackbarText&period;setTextColor&lpar;&lpar;Color&period;<strong><em>WHITE<&sol;em><&sol;strong>&rpar;&rpar;&semi;&NewLine;&NewLine; <em><strong><span style&equals;"color&colon;&num;04603e" class&equals;"has-inline-color">&sol;&sol;change snackbar text gravity<&sol;span><&sol;strong>&NewLine; <&sol;em><strong>if <&sol;strong>&lpar;Build&period;VERSION&period;<strong><em>SDK&lowbar;INT <&sol;em><&sol;strong>&gt&semi;&equals; Build&period;VERSION&lowbar;CODES&period;<strong><em>JELLY&lowbar;BEAN&lowbar;MR1<&sol;em><&sol;strong>&rpar;&NewLine; snackbarText&period;setTextAlignment&lpar;View&period;<strong><em>TEXT&lowbar;ALIGNMENT&lowbar;CENTER<&sol;em><&sol;strong>&rpar;&semi;&NewLine; <strong>else <&sol;strong>&lbrace;&NewLine; snackbarText&period;setGravity&lpar;Gravity&period;<strong><em>CENTER&lowbar;HORIZONTAL<&sol;em><&sol;strong>&rpar;&semi;&NewLine; &rcub;&NewLine; <em><strong><span style&equals;"color&colon;&num;04603e" class&equals;"has-inline-color">&sol;&sol;show snackbar<&sol;span><&sol;strong>&NewLine; <&sol;em>snackbar&period;show&lpar;&rpar;&semi;&NewLine; &rcub;&NewLine; &rcub;&rpar;&semi;&NewLine; <em><strong><span style&equals;"color&colon;&num;04603e" class&equals;"has-inline-color">&sol;&sol;show snackbar<&sol;span><&sol;strong>&NewLine; <&sol;em>snackbarWithAction&period;show&lpar;&rpar;&semi;&NewLine; <strong>break<&sol;strong>&semi;&NewLine;&NewLine; <strong>case <&sol;strong>R&period;id&period;<strong><em>btn&lowbar;snackbar&lowbar;with&lowbar;custom&lowbar;view<&sol;em><&sol;strong>&colon;&NewLine;&NewLine; Snackbar customSnackBar &equals; Snackbar&period;<em>make<&sol;em>&lpar;<strong>constraintLayout<&sol;strong>&comma; <strong>""<&sol;strong>&comma; Snackbar&period;<strong><em>LENGTH&lowbar;LONG<&sol;em><&sol;strong>&rpar;&semi;&NewLine;&NewLine; Snackbar&period;SnackbarLayout layout &equals; &lpar;Snackbar&period;SnackbarLayout&rpar; customSnackBar&period;getView&lpar;&rpar;&semi;&NewLine;&NewLine; <em><strong><span style&equals;"color&colon;&num;04603e" class&equals;"has-inline-color">&sol;&sol;get created custom layout of snackbar<&sol;span><&sol;strong>&NewLine; <&sol;em>View customSnackBarView &equals; getLayoutInflater&lpar;&rpar;&period;inflate&lpar;R&period;layout&period;<strong><em>custom&lowbar;snackbar<&sol;em><&sol;strong>&comma; &lpar;ViewGroup&rpar; findViewById&lpar;R&period;id&period;<strong><em>custom&lowbar;snackbar&lowbar;container<&sol;em><&sol;strong>&rpar;&rpar;&semi;&NewLine;&NewLine; <em><span style&equals;"color&colon;&num;04603e" class&equals;"has-inline-color"><strong>&sol;&sol;getting views from snackbar custom layout<&sol;strong><&sol;span>&NewLine; <&sol;em>TextView textView &equals; &lpar;TextView&rpar; customSnackBarView&period;findViewById&lpar;R&period;id&period;<strong><em>tv&lowbar;custom&lowbar;view<&sol;em><&sol;strong>&rpar;&semi;&NewLine; ImageView imageView &equals; &lpar;ImageView&rpar; customSnackBarView&period;findViewById&lpar;R&period;id&period;<strong><em>iv&lowbar;custom&lowbar;view<&sol;em><&sol;strong>&rpar;&semi;&NewLine;&NewLine; <em><strong><span style&equals;"color&colon;&num;04603e" class&equals;"has-inline-color">&sol;&sol;set text in snackbar custom layout textview<&sol;span><&sol;strong>&NewLine; <&sol;em>textView&period;setText&lpar;getResources&lpar;&rpar;&period;getText&lpar;R&period;string&period;<strong><em>no&lowbar;external&lowbar;storage&lowbar;available<&sol;em><&sol;strong>&rpar;&rpar;&semi;&NewLine;&NewLine; layout&period;setPadding&lpar;0&comma; 0&comma; 0&comma; 0&rpar;&semi;&NewLine;&NewLine; <em><span style&equals;"color&colon;&num;04603e" class&equals;"has-inline-color"><strong>&sol;&sol;add snackbar custom layout<&sol;strong><&sol;span>&NewLine; <&sol;em>layout&period;addView&lpar;customSnackBarView&comma; 0&rpar;&semi;&NewLine;&NewLine; <em><span style&equals;"color&colon;&num;04603e" class&equals;"has-inline-color"><strong>&sol;&sol;show snackbar<&sol;strong><&sol;span>&NewLine; <&sol;em>customSnackBar&period;show&lpar;&rpar;&semi;&NewLine; <strong>break<&sol;strong>&semi;&NewLine;&NewLine; &rcub;&NewLine; &rcub;&NewLine;&rcub;&NewLine;&NewLine;<&sol;pre>&NewLine;&NewLine;&NewLine;&NewLine;<p>When you run the application it will look like this&colon;<&sol;p>&NewLine;&NewLine;&NewLine;&NewLine;<figure class&equals;"wp-block-image size-large is-resized"><img src&equals;"https&colon;&sol;&sol;c1ctech&period;com&sol;wp-content&sol;uploads&sol;2020&sol;09&sol;Screenshot&lowbar;1599719772-576x1024&period;png" alt&equals;"" class&equals;"wp-image-2133" width&equals;"514" height&equals;"914"&sol;><&sol;figure>&NewLine;&NewLine;&NewLine;&NewLine;<h4 class&equals;"wp-block-heading"><span style&equals;"color&colon;&num;520599" class&equals;"has-inline-color"><strong>Complete code in Kotlin<&sol;strong><&sol;span><&sol;h4>&NewLine;&NewLine;&NewLine;&NewLine;<p>Create a new activity <strong>SnackbarActivityKotlin&period;kt<&sol;strong> &lpar;New-&gt&semi;Activity-&gt&semi;Empty Activity-&gt&semi;select language kotlin&rpar; and then click on <strong>finish<&sol;strong>&period;This activity contains three buttons with click listener to show the different ways of implementing the snackbar in <strong>Kotlin<&sol;strong>&period;<&sol;p>&NewLine;&NewLine;&NewLine;&NewLine;<p><strong><span style&equals;"color&colon;&num;520599" class&equals;"has-inline-color">SnackbarActivityKotlin&period;kt<&sol;span><&sol;strong><&sol;p>&NewLine;&NewLine;&NewLine;&NewLine;<pre class&equals;"wp-block-preformatted"><strong>package <&sol;strong>com&period;c1ctech&period;androidsnackbardemo&NewLine;&NewLine;<strong>import <&sol;strong>android&period;graphics&period;Color&NewLine;<strong>import <&sol;strong>android&period;os&period;Build&NewLine;<strong>import <&sol;strong>android&period;os&period;Bundle&NewLine;<strong>import <&sol;strong>android&period;view&period;Gravity&NewLine;<strong>import <&sol;strong>android&period;view&period;View&NewLine;<strong>import <&sol;strong>android&period;view&period;ViewGroup&NewLine;<strong>import <&sol;strong>android&period;widget&period;Button&NewLine;<strong>import <&sol;strong>android&period;widget&period;ImageView&NewLine;<strong>import <&sol;strong>android&period;widget&period;TextView&NewLine;<strong>import <&sol;strong>androidx&period;appcompat&period;app&period;AppCompatActivity&NewLine;<strong>import <&sol;strong>androidx&period;constraintlayout&period;widget&period;ConstraintLayout&NewLine;<strong>import <&sol;strong>com&period;google&period;android&period;material&period;snackbar&period;Snackbar&NewLine;<strong>import <&sol;strong>com&period;google&period;android&period;material&period;snackbar&period;Snackbar&period;SnackbarLayout&NewLine;&NewLine;<strong>class <&sol;strong>SnackBarActivityKotlin &colon; AppCompatActivity&lpar;&rpar;&comma; View&period;OnClickListener &lbrace;&NewLine;&NewLine; <strong>override fun <&sol;strong>onCreate&lpar;savedInstanceState&colon; Bundle&quest;&rpar; &lbrace;&NewLine; <strong>super<&sol;strong>&period;onCreate&lpar;savedInstanceState&rpar;&NewLine; setContentView&lpar;R&period;layout&period;<em>activity&lowbar;snackbar<&sol;em>&rpar;&NewLine;&NewLine; <em><strong><span style&equals;"color&colon;&num;04603e" class&equals;"has-inline-color">&sol;&sol;get views by its id<&sol;span><&sol;strong>&NewLine; <&sol;em><strong>val <&sol;strong>btnSimpleSnackbar &equals; findViewById&lt&semi;Button&gt&semi;&lpar;R&period;id&period;<em>btn&lowbar;simple&lowbar;snackbar<&sol;em>&rpar;&NewLine; <strong>val <&sol;strong>btnSnackbarWithAction &equals; findViewById&lt&semi;Button&gt&semi;&lpar;R&period;id&period;<em>btn&lowbar;snackbar&lowbar;with&lowbar;action<&sol;em>&rpar;&NewLine; <strong>val <&sol;strong>btnCustomSnackbar &equals; findViewById&lt&semi;Button&gt&semi;&lpar;R&period;id&period;<em>btn&lowbar;snackbar&lowbar;with&lowbar;custom&lowbar;view<&sol;em>&rpar;&NewLine;&NewLine; <em><strong><span style&equals;"color&colon;&num;04603e" class&equals;"has-inline-color">&sol;&sol;setting listener to each button<&sol;span><&sol;strong>&NewLine; <&sol;em>btnSimpleSnackbar&period;setOnClickListener&lpar;<strong>this<&sol;strong>&rpar;&NewLine; btnSnackbarWithAction&period;setOnClickListener&lpar;<strong>this<&sol;strong>&rpar;&NewLine; btnCustomSnackbar&period;setOnClickListener&lpar;<strong>this<&sol;strong>&rpar;&NewLine; &rcub;&NewLine;&NewLine; <strong>override fun <&sol;strong>onClick&lpar;view&colon; View&rpar; &lbrace;&NewLine;&NewLine; <em><strong><span style&equals;"color&colon;&num;04603e" class&equals;"has-inline-color">&sol;&sol;get parent view by its id<&sol;span><&sol;strong>&NewLine; <&sol;em><strong>val <&sol;strong>constraintLayout &equals; findViewById&lt&semi;ConstraintLayout&gt&semi;&lpar;R&period;id&period;<em>constraintLayout<&sol;em>&rpar;&NewLine;&NewLine; <strong>val <&sol;strong>id&colon; Int &equals; view&period;getId&lpar;&rpar;&NewLine; <strong>when <&sol;strong>&lpar;id&rpar; &lbrace;&NewLine; R&period;id&period;<em>btn&lowbar;simple&lowbar;snackbar <&sol;em>-&gt&semi; Snackbar&period;make&lpar;constraintLayout&comma; <strong>"Simple Snackbar "<&sol;strong>&comma; Snackbar&period;<em>LENGTH&lowbar;LONG<&sol;em>&rpar;&period;show&lpar;&rpar;&NewLine; R&period;id&period;<em>btn&lowbar;snackbar&lowbar;with&lowbar;action <&sol;em>-&gt&semi; &lbrace;&NewLine; <strong>val <&sol;strong>snackbarWithAction &equals; Snackbar&period;make&lpar;constraintLayout&comma; <strong>"Contact removed"<&sol;strong>&comma; Snackbar&period;<em>LENGTH&lowbar;LONG<&sol;em>&rpar;&NewLine; <strong>val <&sol;strong>snackbarView &equals; snackbarWithAction&period;<em>view <strong><span style&equals;"color&colon;&num;04603e" class&equals;"has-inline-color">&sol;&sol;get snackbar root view<&sol;span><&sol;strong>&NewLine;&NewLine; <strong><span style&equals;"color&colon;&num;04603e" class&equals;"has-inline-color">&sol;&sol;getting child views of snackbar<&sol;span><&sol;strong>&NewLine; <&sol;em><strong>val <&sol;strong>snackbarText &equals; snackbarView&period;findViewById&lt&semi;View&gt&semi;&lpar;R&period;id&period;<em>snackbar&lowbar;text<&sol;em>&rpar; <strong>as <&sol;strong>TextView&NewLine; <strong>val <&sol;strong>snackbarActionText &equals; snackbarView&period;findViewById&lt&semi;View&gt&semi;&lpar;R&period;id&period;<em>snackbar&lowbar;action<&sol;em>&rpar; <strong>as <&sol;strong>TextView&NewLine;&NewLine; <em><strong><span style&equals;"color&colon;&num;04603e" class&equals;"has-inline-color">&sol;&sol;change snackbar text color<&sol;span><&sol;strong>&NewLine; <&sol;em>snackbarText&period;setTextColor&lpar;Color&period;<em>CYAN<&sol;em>&rpar;&NewLine;&NewLine; <em><strong><span style&equals;"color&colon;&num;04603e" class&equals;"has-inline-color">&sol;&sol;change snackbar action button text color<&sol;span><&sol;strong>&NewLine; <&sol;em>snackbarActionText&period;setTextColor&lpar;Color&period;<em>RED<&sol;em>&rpar;&NewLine; snackbarWithAction&period;setAction&lpar;<strong>"UNDO"<&sol;strong>&rpar; <strong>&lbrace;&NewLine; val <&sol;strong>snackbar &equals; Snackbar&period;make&lpar;constraintLayout&comma; <strong>"The contact is restored"<&sol;strong>&comma; Snackbar&period;<em>LENGTH&lowbar;LONG<&sol;em>&rpar;&NewLine; <strong>val <&sol;strong>snackbarView &equals; snackbar&period;<em>view&NewLine;&NewLine; <strong><span style&equals;"color&colon;&num;04603e" class&equals;"has-inline-color">&sol;&sol;change snackbar background color<&sol;span><&sol;strong>&NewLine; <&sol;em>snackbarView&period;setBackgroundColor&lpar;<em>resources<&sol;em>&period;getColor&lpar;R&period;color&period;<em>green<&sol;em>&rpar;&rpar;&NewLine; <strong>val <&sol;strong>snackbarText &equals; snackbarView&period;findViewById&lt&semi;View&gt&semi;&lpar;R&period;id&period;<em>snackbar&lowbar;text<&sol;em>&rpar; <strong>as <&sol;strong>TextView&NewLine;&NewLine; <em><strong><span style&equals;"color&colon;&num;04603e" class&equals;"has-inline-color">&sol;&sol;change snackbar text color<&sol;span><&sol;strong>&NewLine; <&sol;em>snackbarText&period;setTextColor&lpar;Color&period;<em>WHITE<&sol;em>&rpar;&NewLine;&NewLine; <em><strong><span style&equals;"color&colon;&num;04603e" class&equals;"has-inline-color">&sol;&sol;change snackbar text gravity<&sol;span><&sol;strong>&NewLine; <&sol;em><strong>if <&sol;strong>&lpar;Build&period;VERSION&period;<em>SDK&lowbar;INT <&sol;em>&gt&semi;&equals; Build&period;VERSION&lowbar;CODES&period;<em>JELLY&lowbar;BEAN&lowbar;MR1<&sol;em>&rpar; snackbarText&period;<em>textAlignment <&sol;em>&equals; View&period;<em>TEXT&lowbar;ALIGNMENT&lowbar;CENTER <&sol;em><strong>else <&sol;strong>&lbrace;&NewLine; snackbarText&period;<em>gravity <&sol;em>&equals; Gravity&period;<em>CENTER&lowbar;HORIZONTAL&NewLine; <&sol;em>&rcub;&NewLine; <em><strong><span style&equals;"color&colon;&num;04603e" class&equals;"has-inline-color">&sol;&sol;show snackbar<&sol;span><&sol;strong>&NewLine; <&sol;em>snackbar&period;show&lpar;&rpar;&NewLine; <strong>&rcub;&NewLine; <&sol;strong><em><span style&equals;"color&colon;&num;04603e" class&equals;"has-inline-color"><strong>&sol;&sol;show snackbar<&sol;strong><&sol;span>&NewLine; <&sol;em>snackbarWithAction&period;show&lpar;&rpar;&NewLine; &rcub;&NewLine; R&period;id&period;<em>btn&lowbar;snackbar&lowbar;with&lowbar;custom&lowbar;view <&sol;em>-&gt&semi; &lbrace;&NewLine; <strong>val <&sol;strong>customSnackBar &equals; Snackbar&period;make&lpar;constraintLayout&comma; <strong>""<&sol;strong>&comma; Snackbar&period;<em>LENGTH&lowbar;LONG<&sol;em>&rpar;&NewLine; <strong>val <&sol;strong>layout &equals; customSnackBar&period;<em>view <&sol;em><strong>as <&sol;strong>SnackbarLayout&NewLine;&NewLine; <em><strong><span style&equals;"color&colon;&num;04603e" class&equals;"has-inline-color">&sol;&sol;get created custom layout of snackbar<&sol;span><&sol;strong>&NewLine; <&sol;em><strong>val <&sol;strong>customSnackBarView &equals; <em>layoutInflater<&sol;em>&period;inflate&lpar;R&period;layout&period;<em>custom&lowbar;snackbar<&sol;em>&comma; findViewById&lt&semi;View&gt&semi;&lpar;R&period;id&period;<em>custom&lowbar;snackbar&lowbar;container<&sol;em>&rpar; <strong>as&quest; <&sol;strong>ViewGroup&rpar;&NewLine;&NewLine; <em><strong><span style&equals;"color&colon;&num;04603e" class&equals;"has-inline-color">&sol;&sol;getting views from snackbar custom layout<&sol;span><&sol;strong>&NewLine; <&sol;em><strong>val <&sol;strong>textView &equals; customSnackBarView&period;findViewById&lt&semi;TextView&gt&semi;&lpar;R&period;id&period;<em>tv&lowbar;custom&lowbar;view<&sol;em>&rpar;&NewLine; <strong>val <&sol;strong>imageView &equals; customSnackBarView&period;findViewById&lt&semi;ImageView&gt&semi;&lpar;R&period;id&period;<em>iv&lowbar;custom&lowbar;view<&sol;em>&rpar;&NewLine;&NewLine; <em><strong><span style&equals;"color&colon;&num;04603e" class&equals;"has-inline-color">&sol;&sol;set text in snackbar custom layout textview<&sol;span><&sol;strong>&NewLine; <&sol;em>textView&period;<em>text <&sol;em>&equals; <em>resources<&sol;em>&period;getText&lpar;R&period;string&period;<em>no&lowbar;external&lowbar;storage&lowbar;available<&sol;em>&rpar;&NewLine; layout&period;setPadding&lpar;0&comma; 0&comma; 0&comma; 0&rpar;&NewLine;&NewLine; <em><strong><span style&equals;"color&colon;&num;04603e" class&equals;"has-inline-color">&sol;&sol;add snackbar custom layout<&sol;span><&sol;strong>&NewLine; <&sol;em>layout&period;addView&lpar;customSnackBarView&comma; 0&rpar;&NewLine;&NewLine; <em><strong><span style&equals;"color&colon;&num;04603e" class&equals;"has-inline-color">&sol;&sol;show snackbar<&sol;span><&sol;strong>&NewLine; <&sol;em>customSnackBar&period;show&lpar;&rpar;&NewLine; &rcub;&NewLine; &rcub;&NewLine; &rcub;&NewLine;&rcub;<&sol;pre>&NewLine;&NewLine;

Exit mobile version