In the Open Event Android app we only had a list of sessions in the schedule page without the ability to bookmark the session unless we went into the SessionDetailPage or visited the session list via the tracks page or the locations page. This was obviously very inconvenient. There were several iterations of UI design for the same. Taking cues from the Google I/O 17 App I thought that the addition of the Bookmark Icon in the Schedule ViewHolder would be helpful to the user. In this blog post I will be talking about how this feature was implemented.
Layout Implementation for the Bookmark Icon
<ImageButton
android:id="@+id/slot_bookmark"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="end"
android:layout_alignParentEnd="true"
android:layout_alignParentRight="true"
android:layout_alignParentBottom="true"
android:tint="@color/black"
android:background="?attr/selectableItemBackgroundBorderless"
android:contentDescription="@string/session_bookmark_status"
android:padding="@dimen/padding_small"
app:srcCompat="@drawable/ic_bookmark_border_white_24dp" />
The bookmark Icon was modelled as an ImageButton inside the item_schedule.xml file which serves as the layout file for the DayScheduleViewHolder.
Bookmark Icon Functionality in DayScheduleAdapter
The Bookmark Icon had mainly 3 roles :
- Add the session to the list of bookmarks (obviously)
- Generate the notification giving out the bookmarked session details.
- Generate a Snackbar if the icon was un-clicked which allowed the user to restore the bookmarked status of the session.
- Update the bookmarks widget.
Now we will be seeing how was all this done. Some of this was already done previously in the SessionListAdapter. We just had to modify some of the code to get our desired result.
Session not bookmarked Session bookmarked
First we just set a different icon to highlight the Bookmarked and the un-bookmarked status. This code snippet highlights how this is done.
if(session.isBookmarked()) {
slot_bookmark.setImageResource(R.drawable.ic_bookmark_white_24dp);
} else {
slot_bookmark.setImageResource(R.drawable.ic_bookmark_border_white_24dp);
}
slot_bookmark.setColorFilter(storedColor,PorterDuff.Mode.SRC_ATOP);
We check if the session is bookmarked by calling a function isBookmarked() and choose one of the 2 bookmark icons depending upon the bookmark status.
If a session was found out to be bookmarked and the Bookmark Icon was we use the WidgetUpdater.updateWidget() function to remove that particular session from the Bookmark Widget of the app. During this a Snackbar is also generated “Bookmark Removed” with an UNDO option which is functional.
realmRepo.setBookmark(sessionId, false).subscribe();
slot_bookmark.setImageResource(R.drawable.ic_bookmark_border_white_24dp);
if ("MainActivity".equals(context.getClass().getSimpleName())) {
Snackbar.make(slot_content, R.string.removed_bookmark, Snackbar.LENGTH_LONG)
.setAction(R.string.undo, view -> {
realmRepo.setBookmark(sessionId, true).subscribe();
slot_bookmark.setImageResource(R.drawable.ic_bookmark_white_24dp);
WidgetUpdater.updateWidget(context);
}).show();
else {
Snackbar.make(slot_content, R.string.removed_bookmark, Snackbar.LENGTH_SHORT).show();
}
If a session wasn’t bookmarked earlier but the Bookmark Icon was clicked we would firstly need to update the bookmark status within our local Realm Database.
realmRepo.setBookmark(sessionId, true).subscribe();
We would also create a notification to notify the user.
NotificationUtil.createNotification(session, context).subscribe(
() -> Snackbar.make(slot_content,
R.string.added_bookmark,
Snackbar.LENGTH_SHORT)
.show(),
throwable -> Snackbar.make(slot_content,
R.string.error_create_notification,
Snackbar.LENGTH_LONG).show());
The static class Notification Util is responsible for the generation of notifications. The internal working of that class is not necessary right now. What this snippet of code does is that It creates a Snackbar upon successful notification with the text “Bookmark Added” and if any error occurs a Snackbar with the text “Error Creating Notification” is generated.
slot_bookmark.setImageResource(R.drawable.ic_bookmark_white_24dp);
slot_bookmark.setColorFilter(storedColor,PorterDuff.Mode.SRC_ATOP);
This snippet of code is responsible for the colors that are assigned to the Bookmark Icons for different tracks and this color is obtained in the following manner.
int storedColor = currentSession.getTrack().getColor()
So now we have successfully added the Bookmark Icon to the ScheduleViewHolder inside the schedule of the app.
Resources
You must be logged in to post a comment.