blog posts

What is Snack Bar: How to use Snack Bar on Android?

How to use Snack Bar in Android: In the past, it was common among programmers to use Toast to display a message to the user in the application. With the introduction of the Material Design library, a new tool named Snack Bar was introduced. Snack Bar is very easy to use and, at the same time, offers a very good user experience. Also, this utility can be a very good alternative to Toast in your Android applications.

Maybe you have heard the name Snack Bar but haven’t used it yet. In this article, we will introduce this tool to you and teach you how to use Snack Bar on Android practically. If you intend to learn this simple yet practical tool, Learn’s programming training website until the end of this article. stay with Son

What is the operating system?

Operating system is a computer software that works to integrate hardware and software resources. Therefore, the operating system helps different types of hardware to work together while providing a platform for transferring information and software components. Therefore, the Android operating system in smartphones acts as a manager, controlling the performance of the phone’s hardware and the applications you have installed on it.

What is Android?

 

Android is a mobile operating system that was developed by Google about 13 years ago and has been used in various smartphones and tablets since then. Although Google owns this operating system, it has been made available to the public as an Open Source technology, and even its commercial use is free. This distinguishes the Android operating system from operating systems such as iOS, MAC OS, and Windows, which are all closed source platforms.

Android is the most popular operating system in the world. Estimates show that this operating system is active on 2.5 billion devices around the world, and this platform, having more than three billion users, has added about 39% of the world’s population to its user community. The popularity of this operating system is even higher than Apple’s iOS and Microsoft’s Windows. There are more than three million applications for the Android operating system, most of which can be found in the official Google Play Store. But some of them are also available on various websites and can be easily downloaded and installed. This variety makes Android phones very powerful, but it also makes them susceptible to viruses and other malware.

Is the Android operating system designed only for mobile devices?

 

Many people think that Android is an operating system for mobile devices. But that’s not all! Although the majority of Android devices are smartphones, tablets, smart watches and even televisions also use this operating system. But how does Android appear in smart watches? If your smartwatch uses the Wear OS operating system, it means that its operating system is based on Android. In other words, when a company offers Android with a slight modification (minor changes to the original Android code) on its devices, it means that it has designed an Android-based operating system.

In addition to these, there is a television platform called Android TV that works using the Android operating system. Meanwhile, Android Automotive is an Android-based software used in vehicles. Be careful not to confuse Android Automotive with Android Auto. Android Auto is a platform that integrates smartphones with the dashboard system of various cars. In addition to Android-based operating systems, there are other operating systems that are not based on Android, but support running Android applications. New versions of Chrome operating system provide users with this possibility. So almost all Chromebooks on the market support Android apps as well. From the end of 2021, version 11 of the Windows operating system will support Android applications

What is a snack bar?

Before trying to learn how to use Snack Bar on Android, it is better to introduce this useful tool to you first.

Snack Bar is a suitable replacement for Toast in Android. As you know, in the past, Toast was used to inform the user about various events that occurred in the application. However, today there is a more powerful tool than Toast that you can use instead of Toast. Snack Bar has more features than Toast in Android, which is why Snack Bar is superior to Toast. In the rest of this article, we will talk a little about how to make and personalize this extremely useful tool.

 

Tips that you should know before learning how to use the snack bar on Android

Before making a Snack Bar, you should know some tips about this useful tool. One of the most important points to note is that you can use this tool in AppCompatActivity and CoordinatorLayout, and we will explain how to make this tool in each of these cases. You should also note that before you want to create and use this tool in the onCreate() function or any other function, you must place it inside your Layout, which can be done by drag and drop.

Another thing you should know when placing this tool inside the Layout you want is that you need to define an ID for it so that you can access and use it in your classes.

 

How to make a snack bar on Android

As we mentioned in the previous part, we can use AppCompatActivity and CoordinatorLayout to display and use Snack Bar in Android. In the following, we are going to explain how to implement the snack bar in these two.

How to make Snack Bar in AppCompatActivity

To create this tool in AppCompatActivity, you must do the following:

Snackbar.make(findViewById(R. id .your_id),  "your Message" , your duration).show() 

In the following, we will give more explanations about Snack Bar arguments.

How to make Snack Bar in CoordinatorLayout

The construction of the snack bar in CoordinatorLayout is as follows:

val snackBar = Snackbar.make(coordinatorLayout,  "your Message" , your duration)
        snackBar.show() 

Note that in both methods you must call the show() method. Only then can you display the snack bar in your application.

 

Tips on making Snack Bar codes

As you know, in Android programming, to use any object and tool, you must call the class of that tool and create a new object from it. To create the Snack Bar in the AppCompatActivity, you can see that we use the static method of the Snack Bar class called make. This method takes 3 arguments:

  • The first argument is the snack bar that you have created in your layout and you introduce it to this method by ID.
  • The second argument is a simple text that you want to display in the snackbar.
  • The third argument is the length of time you want this snack bar to be displayed to users.

As you can see, using Snack Bar is not difficult and you can use it easily. Using Snack Bar in CoordinatorLayout is a little different and in it you have to create an object of the Snack Bar class and set it to the static make method, which also takes 3 arguments, the last 2 arguments being the same as the previous method. The only difference is in the first argument to which you have to pass CoordinatorLayout in this method. According to the mentioned items, you have learned how to use Snack Bar on Android.

 

Determining the duration of the snack bar display when using the Snack Bar on Android

In the previous section, we mentioned that when using the Snack Bar, the third argument sent to the make method determines the duration of the Snack Bar display. The duration of the snack bar display is determined using the constants of the Snack Bar class, which generally has the following three constants in Android:

  • The first mode of the duration of the snack bar display for the fixed user is LENGTH_SHORT, which displays the short time of the snack bar.
  • The second mode of the duration of the snack bar display for the user is the fixed LENGTH_LONG, which displays a longer duration than the previous fixed snack bar.
  • The third mode of the duration of the display of the snack bar for the fixed user is LENGTH_INDEFINITE, which is usually used for times when you want the user to view the snack bar during an operation.

Note that in the LENGTH_INDEFINITE constant, until the View is destroyed or you dismiss the snack bar, your snack bar will not disappear and will be displayed to the user.

 

Place the button in the snack bar

Sometimes when using Snack Bar, you need to place a button so that the user can perform an action by clicking on it. In fact, the button is one of the best advantages of using Snack Bar. In the following, we are going to explain the method of placing the button in the snack bar in Android in two ways:

Putting the button in the snackbar in the AppCompatActivity

To place the button in AppCompatActivity, we do as follows:

Snackbar . make ( findViewById (R. id . your_id ),  "your Message" , your duration)
            . setAction ( "your text" ) {
                 TODO ( "your action" )
            }
            . show () 

Note: In Kotlin, we use Lambda Expression to implement SetOnClickListener.

Place the button in the snackbar in the CoordinatorLayout

To place the button in the CoordinatorLayout, we can do the following:

val snackBar =  Snackbar . make (coordinatorLayout,  "your Message" , your duration)
            . setAction ( "your text" ) {
                 TODO ( "your action" )
            }
        snackBar. show () 

 

Change the color of the snack bar

One of the advantages of the snack bar in Android over Toast is the customization it provides to the programmer. You can change the color of the texts, buttons and background of your snack bar. In the following, we will examine these cases.

Change the background of the snack bar on Android

To change the background in the snack bar, we must code as follows:

val snackBar =  Snackbar . make (coordinatorLayout,  "your Message" , your time)
            . setAction ( "your text" ) {
                 TODO ( "your action" )
            }
        snackBar. view . setBackgroundColor ( Color . parseColor ( "your color" ))
        snackBar. show () 

Change the color of the text in the snack bar on Android

To change the color of the text in the snack bar, we must code as follows:

val snackBar =  Snackbar . make (coordinatorLayout,  "your Message" , your time)
            . setAction ( "your text" ) {
                 TODO ( "your action" )
            }
        snackBar. view . setBackgroundColor ( Color . parseColor ( "your color" ))
        snackBar. setTextColor ( Color . parseColor ( "your color" ))
        snackBar. show () 

Changing the text color of the buttons in the snack bar on Android

To change the color of the text of the button in the snack bar, we must code as follows:

val snackBar =  Snackbar . make (coordinatorLayout,  "your Message" , your time)
            . setAction ( "your text" ) {
                 TODO ( "your action" )
            }
        snackBar. view . setBackgroundColor ( Color . parseColor ( "your color" ))
        snackBar. setTextColor ( Color . parseColor ( "your color" ))
        snackBar. setActionTextColor ( Color . parseColor ( "your color" ))
        snackBar. show () 

Using Snack Bar or Toast?

Snack Bar in Android has more features and benefits than Toast in Android. But each of them is still used in different parts of today’s applications. By using Snack Bar, we can give our user a better user experience. Also, the possibility of user interaction using buttons in the snack bar has made this tool better than Toast.