Search code examples
c#androidxamarinanimationxamarin.android

Use drawable animation as large icon in notification android?


I'm using Xamarin Android with C#.

In the notification builder I can set a large icon like this:

Small Icon & Large Icon (Not mine picture)

            NotificationCompat.Builder notbuilder = new NotificationCompat.Builder(this, "0");
            notbuilder.SetSmallIcon(Resource.Drawable.miconNotify);
            notbuilder.SetColor(GetColor(Resource.Color.notifyColor));
            notbuilder.SetContentText("DownloadText");
            notbuilder.SetContentTitle("DownloadTitle");
            notbuilder.SetLargeIcon(WHAT_COMES_HERE); //<----------- (needs a Bitmap)

Is it possible to animate the large icon somehow (like the Google Play Store while downloads does)? I've tried to use a animation list xml but I found no way to start it.


Solution

  • Found a realy simple solution:

                NotificationCompat.Builder builder = new NotificationCompat.Builder(this, "0");
                builder.SetSmallIcon(Resource.Drawable.miconNotify);
                builder.SetColor(GetColor(Resource.Color.notifyColor));
                builder.SetContentText("Download1");
                builder.SetContentTitle("DownloadTitle");
                builder.SetLargeIcon(BitmapFactory.DecodeResource(Resources, Resource.Drawable.NotifyAnim0));
    
                NotificationManager manager = (NotificationManager)GetSystemService(Context.NotificationService) as NotificationManager;
                manager.Notify(0, builder.Build()); //Notify for the first time
    
                bool DownloadFinished = false;
    
                Task TaskA = new Task(() => 
                {
                    while (!DownloadFinished)
                    {
                        Thread.Sleep(200);
                        builder.SetLargeIcon(BitmapFactory.DecodeResource(Resources, Resource.Drawable.NotifyAnim0)); //Pic 1
                        manager.Notify(0, builder.Build());
                        Thread.Sleep(200);
                        builder.SetLargeIcon(BitmapFactory.DecodeResource(Resources, Resource.Drawable.NotifyAnim1)); //Pic 2
                        manager.Notify(0, builder.Build());
                        Thread.Sleep(200);
                        builder.SetLargeIcon(BitmapFactory.DecodeResource(Resources, Resource.Drawable.NotifyAnim2)); //Pic 3
                        manager.Notify(0, builder.Build());
                        Thread.Sleep(200);
                        builder.SetLargeIcon(BitmapFactory.DecodeResource(Resources, Resource.Drawable.NotifyAnim3)); // Pic 4
                        manager.Notify(0, builder.Build());
                        Thread.Sleep(200);
                        builder.SetLargeIcon(BitmapFactory.DecodeResource(Resources, Resource.Drawable.NotifyAnim4)); //Pic 5
                        manager.Notify(0, builder.Build());
                    }
                });
                TaskA.Start();
    

    Be sure you are using NotificationCompat.Builder! The task runs parallel and the app works just normal. To stop the animation just set DownloadFinished to true.