How to animate the progress notification icon
Asked Answered
C

3

5

I am building the notification progress bar in Service in such a way that is given below :

private NotificationManager mNotifyManager;
private NotificationCompat.Builder mBuilder;



 mBuilder =
                    new NotificationCompat.Builder(DownloadService.this)
                            .setSmallIcon(R.drawable.download)
                            .setContentTitle("MayUp")
                            .setContentText("Downloading new tools")
                            .setTicker("Downloading in progress");
            mBuilder.setAutoCancel(true);
            Intent targetIntent = new Intent(DownloadService.this, ListClass.class);
            TaskStackBuilder stackBuilder = TaskStackBuilder.create(DownloadService.this);
            // Adds the back stack for the Intent (but not the Intent itself)
            stackBuilder.addParentStack(MyActivity.class);
            // Adds the Intent that starts the Activity to the top of the stack
            stackBuilder.addNextIntent(targetIntent);
//            PendingIntent contentIntent = PendingIntent.getActivity(DownloadService.this, 0, targetIntent, PendingIntent.FLAG_UPDATE_CURRENT);
//            mBuilder.setContentIntent(contentIntent);
            PendingIntent resultPendingIntent = stackBuilder.getPendingIntent(0,
                    PendingIntent.FLAG_UPDATE_CURRENT);
            mBuilder.setContentIntent(resultPendingIntent);

            mNotifyManager.notify(1, mBuilder.build());

            mBuilder.setProgress(100, 0, false);
            mNotifyManager.notify(1, mBuilder.build());

its working fine , the notification and the progress bar is showing all fine , I wanted the simple progress bar in the notification and it is the simple and I have no problem with that but what I want is given below :

What I want:

I want that My notification Icon should animate like the google play store app does, it animates its icon to show that download in progress. I want to do the same. Please tell me by using builder and notification manager How can I get this , I have seen many tutorial but they are deprecated.

Cohort answered 2/12, 2015 at 8:27 Comment(2)
As i know, Pushing notifications which have same IDs by sequencely ordered will update same notification. So you can get thet behaiver.Janinejanis
I am updating the progress bar under that notification , I know how to push the updates using same Id , I want to ask about how to animate the icon onlyCohort
I
1

you can make the animation list as described above in the answer of Rushab patel but there could be problem if you are using the newst api and if you are targetting the new sdk .

so this following line would become outdated and deprecated

Notification notification = new Notification(R.drawable.wheelAnim, null, System.currentTimeMillis());

which is too bad in coding notification , I must suggest you as you have described above that you are using notification builder , thus I would recomend you to directly use this animation list drawable in the notification builder.

from your snippet

mBuilder =
                    new NotificationCompat.Builder(DownloadService.this)
                            .setSmallIcon(R.drawable.youAnimationList)
                            .setContentTitle("MayUp")
                            .setContentText("Downloading new tools")
                            .setTicker("Downloading in progress");
            mBuilder.setAutoCancel(true);
Instrumentation answered 3/12, 2015 at 10:39 Comment(0)
A
38

By using this code you can display a notification for downloading some files with a animated icon:

mBuilder.setContentTitle("Downloading... ")
        .setContentText("Please wait...")
        .setOngoing(true)
        .setOnlyAlertOnce(true)
        .setSmallIcon(android.R.drawable.stat_sys_download)  // here is the animated icon
        .setLargeIcon(BitmapFactory.decodeResource(this.getResources(), android.R.drawable.stat_sys_download)).build();
Ailey answered 4/9, 2016 at 9:55 Comment(2)
if any one need icon for upload , it is android.R.drawable.stat_sys_uploadDewaynedewberry
@ManoharReddy OMFG you are the best bro I was looking for this so long. THANK YOU SO MUCHSpellman
W
7

Create .xml file with below code:

<!-- Animation frames are wheel0.png -- wheel5.png files inside the
 res/drawable/ folder -->
 <animation-list android:id="selected" android:oneshot="false">
    <item android:drawable="@drawable/wheel0" android:duration="50" />
    <item android:drawable="@drawable/wheel1" android:duration="50" />
    <item android:drawable="@drawable/wheel2" android:duration="50" />
    <item android:drawable="@drawable/wheel3" android:duration="50" />
    <item android:drawable="@drawable/wheel4" android:duration="50" />
    <item android:drawable="@drawable/wheel5" android:duration="50" />
</animation-list>

Where wheel0 to 5 are the images of the wheel. Make sure all images are in your drawable folder.

User below snippet into your code to animate icon:

Notification notification = new Notification(R.drawable.wheelAnim, null, System.currentTimeMillis());

PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, new Intent(), 0);

notification.flags |= Notification.FLAG_AUTO_CANCEL;

notification.setLatestEventInfo(this, getText(R.string.someTitle),getText(R.string.someText), pendingIntent);

((NotificationManager) getSystemService(NOTIFICATION_SERVICE)).notify(uid, notification);

Ref link

Winne answered 2/12, 2015 at 8:57 Comment(3)
but Notification notification = new Notification(R.drawable.wheelAnim, null, System.currentTimeMillis()); is deprecatedCohort
in simple words this way of doing notification is deprecated , the encourage to use the notification builder and managerCohort
Another way is to maintain one timer which will just update the notification icon every let's say 200 milliseconds and notify the same notification id. mBuilder.setSmallIcon(R.drawable.ic_download); mNotifyManager.notify(0, mBuilder.build());Winne
I
1

you can make the animation list as described above in the answer of Rushab patel but there could be problem if you are using the newst api and if you are targetting the new sdk .

so this following line would become outdated and deprecated

Notification notification = new Notification(R.drawable.wheelAnim, null, System.currentTimeMillis());

which is too bad in coding notification , I must suggest you as you have described above that you are using notification builder , thus I would recomend you to directly use this animation list drawable in the notification builder.

from your snippet

mBuilder =
                    new NotificationCompat.Builder(DownloadService.this)
                            .setSmallIcon(R.drawable.youAnimationList)
                            .setContentTitle("MayUp")
                            .setContentText("Downloading new tools")
                            .setTicker("Downloading in progress");
            mBuilder.setAutoCancel(true);
Instrumentation answered 3/12, 2015 at 10:39 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.