Snackbar Implementation in PSLab Android App
In PSLab android app we have developed the functionality of logging sensor data in CSV format. We can start and stop the data recording using the save button in the upper right corner of the menu bar and toast message was shown to notify the user for logging status whether it is started or stopped but it leads to some problem like:-
- The user doesn’t know where the logged file has been created in the external storage.
- If the user accidentally clicked on the save button the data logging will start the user have to manually go the storage location and delete the recently created unwanted CSV file.
What’s the solution?
The solution to both these problem is solved by implementing Snackbar instead of Toast message.
According to Material Design documentation:-
The Snackbar widget provides brief feedback about an operation through a message at the bottom of the screen. Snackbar disappears automatically, either after a timeout or after a user interaction elsewhere on the screen, and can also be swiped off the screen.
Snackbar can also offer the ability to perform an action, such as undoing an action that was just taken or retrying an action that had failed. |
To implement the Snackbar in our Android app I started by creating a custom snack bar class which contains all the code to create and show the Snackbar on the screen.
public class CustomSnackBar {
public static void showSnackBar(@NonNull CoordinatorLayout holderLayout,
@NonNull String displayText,
String actionText,
View.OnClickListener clickListener){
Snackbar snackbar =
Snackbar.make(holderLayout,displayText,Snackbar.LENGTH_LONG)
.setAction(actionText, clickListener);
//do your customization here
}
The custom class contains a static method ‘showSnackBar()’ having parameters:
Parameter | Return Type | Description |
holderLayout | CoordinatorLayout | Container layout in which the snack bar will be shown at the bottom (should not be null) |
displayText | String | Text to be displayed in the content of Snackbar (should not be null) |
actionText | String | Clickable text which has some action associated with it |
clickListener | View.OnClickListener | On click listener specifying an action to be performed when actionText is clicked |
Inside the method, I called the static make() method provided by the Snackbar class and passed holderlayout, displayText and duration of Snackbar in this case Snackbar.LENGTH_LONG as parameters.
Then I called setAction() and passed in the actionText and the clickListener as parameters in it to set the action text. If we pass in null no action text will be generated.
Then, if we want to changes the action text color we can do that by calling setActionTextColor() and passing in the desired color.
snackbar.setActionTextColor(ContextCompat.getColor(holderLayout.getContext(), R.color.colorPrimary));
And if we want to change the content text color then we need to first get the view then we need to get the instance of TextView containing the content text using findViewById() and passing android.support.design.R.id.snackbar_text which is default ID for context TextView, and then call setTextColor() to set the desired color.
View sbView = snackbar.getView();
TextView textView =
sbView.findViewById(android.support.design.R.id.snackbar_text);
textView.setTextColor(Color.WHITE);
}
So, now our Snackbar engine is complete now we need to call CustomSnackBar class static method showSnackbar() in our sensor data logger.
For doing this I replaced all the instances of the Toast message with the ‘CustomSnackBar’ by passing in the desired messages that were being passed in Toast message.
But I still need to find the location of our stored CSV file and a method to delete the current generated CSV file.
For that, I did below modification to the CSVLogger class in PSLab android app.
public class CSVLogger {
private static final String CSV_DIRECTORY = "PSLab";
public CSVLogger(String category) {
this.category = category;
setupPath();
}
/*Below methods are included at the bottom of the class */
public String getCurrentFilePath() {
return Environment.getExternalStorageDirectory().getAbsolutePath() +
File.separator + CSV_DIRECTORY + File.separator + category;
}
public void deleteFile() {
csvFile.delete();
}
}
Now for passing the location of the stored file and implementing delete option, I called the below method when the CSV logging is stopped by the user:
CustomSnackBar.showSnackBar((CoordinatorLayout) parent.findViewById(R.id.cl),
“CSV File stored at” + " " +lux_logger.getCurrentFilePath(),
“DELETE”,
new View.OnClickListener() {
Override
public void onClick(View view) {
lux_logger.deleteFile();
});
By doing this I get a Snackbar as shown in Figure 2, clicking on the “DELETE” text deletes the current CSV file.
So, implementing Snackbar helped to make the app interactive and keeps user notified and control the data logging.
Resources
- https://www.journaldev.com/10324/android-snackbar-example-tutorial – Android SnackBar example implemetation tutorial
- https://material.io/develop/android/components/snackbar/ – Android Material Desing implementation of Snackbar.
You must be logged in to post a comment.