Snackbar android studio kotlin

Kotlin Android – Snackbar – Set Action – Example

Android – Snackbar setAction

An action could be set to Snackbar using Snackbar.setAction() method. setAction takes View.OnClickListener as second argument and you may write your set of instructions to perform the action.

In this tutorial, we will learn how to display Snackbar widget on even of an action.

The following screenshot is an example for Snackbar, with a message and an actionable item next to it.

Important Note : To use Snackbar in your android application, you need to include the ‘com.android.support.design’ package in your build.gradle (app) dependencies. There are different available versions of the package. You need to include the one with version matching the compileSdkVersion.

Following variants are available for setAction method.

1. setAction(int resId, View.OnClickListener listener)

Set the action to be displayed in this BaseTransientBottomBar.

2. setAction(CharSequence text, View.OnClickListener listener)

Set the action to be displayed in this BaseTransientBottomBar.

Example – Android Snackbar – Set Action

In this example, we shall set text “DISMISS” to appear in Snackbar and an action would be taken when clicked on it.

activity_main.xml

MainActivity.kt

Run this Android application, and an Android screen will appear as shown below with a button. Click on the button “Show Snackbar”. Snackbar appears at the bottom of the screen.

Conclusion

In this Kotlin Android Tutorial, we have learnt to set an Action Listener to the Snackbar using setAction() method. In our next tutorial, we shall learn to change the text color and background of Snackbar and Action String in it.

Источник

Android Snackbar Example

Android Snackbar is an interesting component introduced by Material Design. Snackbars are just like Toast messages except they provide action to interact with. Snackbar will be displayed at the bottom of the screen and can be swiped off in order to dismiss them.

Difference between Toast and Snackbar

  • Toast messages can be customized and printed anywhere on the screen, but a Snackbar can be only shown in the bottom of the screen.
  • A Toast message doesn’t have action button, but Snackbar may have action button optionally.
  • Toast message cannot be off until the time limit finish, but Snackbar can be swiped off before the time limit.

You can set how long the message will be shown using this three different values.

  • Snackbar.LENGTH_LONG
  • Snackbar.LENGTH_SHORT
  • Snackbar.LENGTH_INDEFINITE

Let’s Getting into Snackbar Android Example

In Android Studio, Create New Project and fill all the details required to create a new project.

Open build.gradle and add Material Design dependency.

By using material dependency, you can also create components like Recyclerview, Cardview and snackbar etc. If you want to know more about other material components check below links.

Also, setup the application theme as theme.MaterialComponents.

Now, done with the project setup.

Create Simple Android Snackbar

Below is the syntax of a simple snackbar. The make function accepts three parameters. View, display message and duration of the message to be displayed.

Android Snackbar with Action Callback

You can also mention a callback interaction method using setAction() method. This allows us to take certain action when the user interacts with the snackbar.

Читайте также:  What is an android wifi tablet

Customizing the Android Snackbar

Snackbar comes with default white color text and #323232 background color. You can override these colors.

Theming snackbars

Snackbars support Material Theming and can be customized in terms of color and typography.

Implementing snackbar theming

Using theme attributes in res/values/styles.xml

or in code (affects only this snackbar):

Android snackbar example

Download the example from github.

Thanks for reading this post. Material design having lot of useful components like Material Button , Cardview etc. I will write more about material design in my future post.

Источник

Kotlin Android – Snackbar – Example

Android Snackbar

Android Snackbar is a material design component introduced with API 22.2.0. The functionality would resemble Android Toast, but unlike Toast, Snackbar could be dismissed by user or an action listener could be setup to respond to user actions.

Snackbar when shown, is displayed at the bottom of the screen, with default background color “#323232” and text color “#FFFFFF“. You may change the background and text color. We shall go through that in the tutorial – Snackbar – Change Background and Text Color.

The following screenshot is an example, of Snackbar sliding from from bottom of the screen, displaying a message.

Important Note : To use Snackbar in your android application, you need to include the ‘com.android.support.design’ package in your build.gradle (app) dependencies. There are different available versions of the package. You need to include the one with version matching the compileSdkVersion.

Once you modify the build.gradle (Module:app), Android Studio asks for Gradle Sync. Sync gradle and you are good to use Snackbar in your project.

Example – Kotlin Android Snackbar

Create an Android Application with Kotlin Support and find the code for activity_main.xml layout file and MainActivity.kt provided below.

In this Kotlin Android Snackbar Example, we shall display a button, and when the button is clicked, Snackbar is displayed at the bottom of the screen.

activity_main.xml

MainActivity.kt

Run this Android Application on a physical Android Device or Android Virtual Device, and you get an Activity with the button as shown in the following screenshot. When you click on the “Show Snackbar” button, a Snackbar appears at the bottom of the screen.

An action could be set to Snackbar using Snackbar.setAction() method.

Conclusion

In this Kotlin Android Tutorial, we have learnt to include support library for Snackbar in the dependencies with Example Android Application. In our next tutorial, we shall learn to set action listener to the Snackbar.

Источник

Snackbar

java.lang.Object
com.google.android.material.snackbar.BaseTransientBottomBar
com.google.android.material.snackbar.Snackbar

Snackbars provide lightweight feedback about an operation. They show a brief message at the bottom of the screen on mobile and lower left on larger devices. Snackbars appear above all other elements on screen and only one can be displayed at a time.

They automatically disappear after a timeout or after user interaction elsewhere on the screen, particularly after interactions that summon a new surface or activity. Snackbars can be swiped off screen.

To be notified when a snackbar has been shown or dismissed, you can provide a Snackbar.Callback via BaseTransientBottomBar.addCallback(BaseCallback) .

Summary

Nested classes

Callback class for Snackbar instances.

Inherited constants

Animation mode that corresponds to the fade in and out animations.

Animation mode that corresponds to the slide in and out animations.

Show the Snackbar indefinitely.

Show the Snackbar for a long period of time.

Show the Snackbar for a short period of time.

Inherited fields

Public methods

Return the duration.

Return whether this BaseTransientBottomBar is currently being shown.

static Snackbar make(View view, CharSequence text, int duration)

Make a Snackbar to display a message

Snackbar will try and find a parent view to hold Snackbar’s view from the value given to view .

static Snackbar make(Context context, View view, CharSequence text, int duration)
Читайте также:  Стандартный вызов для андроида

Make a Snackbar to display a message

Snackbar will try and find a parent view to hold Snackbar’s view from the value given to view .

static Snackbar make(View view, int resId, int duration)

Make a Snackbar to display a message.

Snackbar setAction(int resId, View.OnClickListener listener)

Set the action to be displayed in this BaseTransientBottomBar .

Snackbar setAction(CharSequence text, View.OnClickListener listener)

Set the action to be displayed in this BaseTransientBottomBar .

Snackbar setActionTextColor(ColorStateList colors)

Sets the text color of the action specified in setAction(CharSequence, View.OnClickListener) .

Snackbar setActionTextColor(int color)

Sets the text color of the action specified in setAction(CharSequence, View.OnClickListener) .

Snackbar setBackgroundTint(int color)

Sets the tint color of the background Drawable.

Snackbar setBackgroundTintList(ColorStateList colorStateList)

Sets the tint color state list of the background Drawable.

Snackbar setBackgroundTintMode(PorterDuff.Mode mode) Snackbar setCallback(Snackbar.Callback callback) Snackbar setMaxInlineActionWidth(int width)

Sets the max width of the action to be in the same line as the message.

Snackbar setText(int resId)

Update the text in this Snackbar .

Snackbar setText(CharSequence message)

Update the text in this Snackbar .

Snackbar setTextColor(int color)

Sets the text color of the message specified in setText(CharSequence) and setText(int) .

Snackbar setTextColor(ColorStateList colors)

Sets the text color of the message specified in setText(CharSequence) and setText(int) .

Protected methods

static boolean hasSnackbarButtonStyleAttr(Context context)

This method is deprecated. This is for backward compatibility with AppCompat themes.

Inherited methods

Adds the specified callback to the list of callbacks that will be notified of transient bottom bar events.

void dispatchDismiss(int event) View getAnchorView()

Returns the anchor view for this BaseTransientBottomBar .

Returns the animation mode.

Return the behavior.

Return the duration.

SwipeDismissBehavior getNewBehavior() int getSnackbarBaseLayoutResId() View getView()

Snackbar s should still work with AppCompat themes, which don’t specify a snackbarStyle .

Returns whether the anchor view layout listener is enabled.

Returns whether this bottom bar should adjust it’s position based on the system gesture area on Android Q and above.

Return whether this BaseTransientBottomBar is currently being shown.

Returns whether this BaseTransientBottomBar is currently being shown, or is queued to be shown next.

Snackbar removeCallback(BaseCallback callback)

Removes the specified callback from the list of callbacks that will be notified of transient bottom bar events.

Snackbar setAnchorView(int anchorViewId)

Sets the view the BaseTransientBottomBar should be anchored above by id.

Snackbar setAnchorView(View anchorView)

Sets the view the BaseTransientBottomBar should be anchored above.

void setAnchorViewLayoutListenerEnabled(boolean anchorViewLayoutListenerEnabled)

Sets whether the anchor view layout listener is enabled.

Snackbar setAnimationMode(int animationMode)

Sets the animation mode.

Snackbar setBehavior(BaseTransientBottomBar.Behavior behavior) Snackbar setDuration(int duration)

Set how long to show the view for.

Snackbar setGestureInsetBottomIgnored(boolean gestureInsetBottomIgnored)

Sets whether this bottom bar should adjust it’s position based on the system gesture area on Android Q and above.

From class java.lang.Object

Object clone()
boolean equals(Object arg0)
void finalize()
final Class getClass()
int hashCode()
final void notify()
final void notifyAll()
String toString()
final void wait(long arg0, int arg1)
final void wait(long arg0)
final void wait()

Public methods

dismiss

getDuration

Return the duration.

isShown

Return whether this BaseTransientBottomBar is currently being shown.

Returns
int

Make a Snackbar to display a message

Snackbar will try and find a parent view to hold Snackbar’s view from the value given to view . Snackbar will walk up the view tree trying to find a suitable parent, which is defined as a CoordinatorLayout or the window decor’s content view, whichever comes first.

Having a CoordinatorLayout in your view hierarchy allows Snackbar to enable certain features, such as swipe-to-dismiss and automatically moving of widgets.

Returns
boolean
Parameters
view View : The view to find a parent from. This view is also used to find the anchor view when calling BaseTransientBottomBar.setAnchorView(int) .
text CharSequence : The text to show. Can be formatted text.
duration int : How long to display the message. Can be BaseTransientBottomBar.LENGTH_SHORT , BaseTransientBottomBar.LENGTH_LONG , BaseTransientBottomBar.LENGTH_INDEFINITE , or a custom duration in milliseconds.

Make a Snackbar to display a message

Snackbar will try and find a parent view to hold Snackbar’s view from the value given to view . Snackbar will walk up the view tree trying to find a suitable parent, which is defined as a CoordinatorLayout or the window decor’s content view, whichever comes first.

Having a CoordinatorLayout in your view hierarchy allows Snackbar to enable certain features, such as swipe-to-dismiss and automatically moving of widgets.

Parameters
context Context : The context to use to create the Snackbar view.
view View : The view to find a parent from. This view is also used to find the anchor view when calling BaseTransientBottomBar.setAnchorView(int) .
text CharSequence : The text to show. Can be formatted text.
duration int : How long to display the message. Can be BaseTransientBottomBar.LENGTH_SHORT , BaseTransientBottomBar.LENGTH_LONG , BaseTransientBottomBar.LENGTH_INDEFINITE , or a custom duration in milliseconds.

Make a Snackbar to display a message.

Snackbar will try and find a parent view to hold Snackbar’s view from the value given to view . Snackbar will walk up the view tree trying to find a suitable parent, which is defined as a CoordinatorLayout or the window decor’s content view, whichever comes first.

Having a CoordinatorLayout in your view hierarchy allows Snackbar to enable certain features, such as swipe-to-dismiss and automatically moving of widgets.

Parameters
view View : The view to find a parent from.
resId int : The resource id of the string resource to use. Can be formatted text.
duration int : How long to display the message. Can be BaseTransientBottomBar.LENGTH_SHORT , BaseTransientBottomBar.LENGTH_LONG , BaseTransientBottomBar.LENGTH_INDEFINITE , or a custom duration in milliseconds.

setAction

Set the action to be displayed in this BaseTransientBottomBar .

Parameters
resId int : String resource to display for the action
listener View.OnClickListener : callback to be invoked when the action is clicked

setAction

Set the action to be displayed in this BaseTransientBottomBar .

Parameters
text CharSequence : Text to display for the action
listener View.OnClickListener : callback to be invoked when the action is clicked

setActionTextColor

Sets the text color of the action specified in setAction(CharSequence, View.OnClickListener) .

Parameters
colors ColorStateList

setActionTextColor

Sets the text color of the action specified in setAction(CharSequence, View.OnClickListener) .

Parameters
color int

setBackgroundTint

Sets the tint color of the background Drawable.

Parameters
color int

setBackgroundTintList

Sets the tint color state list of the background Drawable.

Parameters
colorStateList ColorStateList

setBackgroundTintMode

Parameters
mode PorterDuff.Mode

setCallback

Set a callback to be called when this the visibility of this Snackbar changes. Note that this method is deprecated and you should use BaseTransientBottomBar.addCallback(BaseCallback) to add a callback and BaseTransientBottomBar.removeCallback(BaseCallback) to remove a registered callback.

Parameters
callback Snackbar.Callback : Callback to notify when transient bottom bar events occur.

setMaxInlineActionWidth

Sets the max width of the action to be in the same line as the message. If the width is exceeded the action would go to the next line.

Parameters
width int

setText

Update the text in this Snackbar .

Parameters
resId int : The new text for this BaseTransientBottomBar .

setText

Update the text in this Snackbar .

Parameters
message CharSequence : The new text for this BaseTransientBottomBar .

setTextColor

Sets the text color of the message specified in setText(CharSequence) and setText(int) .

Parameters
color int

setTextColor

Sets the text color of the message specified in setText(CharSequence) and setText(int) .

Parameters
colors ColorStateList

Protected methods

hasSnackbarButtonStyleAttr

This method is deprecated.
This is for backward compatibility with AppCompat themes.

Snackbar s should still work with AppCompat themes, which don’t specify a snackbarButtonStyle . This method helps to check if a valid snackbarButtonStyle is set within the current context, so that we know whether we can use the attribute.

Parameters
context Context

Content and code samples on this page are subject to the licenses described in the Content License. Java is a registered trademark of Oracle and/or its affiliates.

Источник

Читайте также:  Сборник карт для андроид
Оцените статью