Thanks to RemoteViews's setViewVisibility, its trivial to show Spinner progress bar in the Notification. Here is an application suggesting new books to download via a background service.
A good feedback would be to show a progress spinner as the book is being downloaded. Here is the notification layout to achieve the same,
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="match_parent">
<ImageView
android:id="@+id/preview"
android:layout_width="128dp"
android:layout_height="128dp"/>
<FrameLayout
android:paddingLeft="40dp"
android:layout_gravity="center"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<ImageButton
android:id="@+id/download_button"
android:background="@android:color/transparent"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/images2" />
<ProgressBar
android:id="@+id/progress_bar"
android:clickable="false"
android:visibility="gone"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</FrameLayout>
</LinearLayout>
The ProgressBar's default visibility is set to hidden and is updated only when the intent is fired as the user clicks on the download button.
Notification.Builder mBuilder = new Notification.Builder(this)
.setContentTitle("Notification")
.setContentText("Progress Notification")
.setSmallIcon(R.drawable.ic_launcher);
private void updateNotification(boolean showProgress) {
RemoteViews remoteView = new RemoteViews(getPackageName(), R.layout.service_notification);
remoteView.setImageViewResource(R.id.preview, R.drawable.gameofthrones);
if ( showProgress ) {
remoteView.setViewVisibility(R.id.download_button, View.GONE);
remoteView.setViewVisibility(R.id.progress_bar, View.VISIBLE);
} else {
remoteView.setViewVisibility(R.id.download_button, View.VISIBLE);
remoteView.setViewVisibility(R.id.progress_bar, View.GONE);
}
Notification notification = mBuilder.build();
notification.contentView = remoteView;
notification.bigContentView = remoteView;
NotificationManager manager;
manager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
manager.notify(NOTIFICATION_ID, notification);
}
Now as and when the book is being downloaded, the service can update the Notification via updateNotification() and we have a spinner.
No comments:
Post a Comment