Jonas
Jonas

Reputation: 53

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.

Upvotes: 1

Views: 827

Answers (2)

Jonas
Jonas

Reputation: 53

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.

Upvotes: 1

Leon Lu
Leon Lu

Reputation: 9234

Can you share a gif about your needs?

If you want to change the icon like a GIF result, I cannot found a way to achieve it.

However, I you want to add a progress bar like Google Play Store while downloads, it could be achieved by RemoteView like this running gif.

enter image description here

I used RemoteView for Notification

  RemoteViews views = new RemoteViews(this.PackageName, Resource.Layout.layout_nitification);

            views.SetProgressBar(Resource.Id.progressBar1,100,1,false);

              // Create the PendingIntent with the back stack:            
              var resultPendingIntent = stackBuilder.GetPendingIntent(0, (int) PendingIntentFlags.UpdateCurrent);
         
            // Build the notification:
            var builder = new NotificationCompat.Builder(this, CHANNEL_ID)
                .SetContent(views)
                          .SetAutoCancel(true) // Dismiss the notification from the notification area when the user clicks on it
                          .SetContentIntent(resultPendingIntent) // Start up this activity when the user clicks the intent.
                          .SetContentTitle("Button Clicked") // Set the title
                          .SetNumber(count) // Display the count in the Content Info
                        
                          .SetSmallIcon(Resource.Drawable.Icon)
                          .SetContentText($"The button has been clicked {count} times."); // the message to display.

Here is code about layout_nitification.xml

<?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="60dp">
    <ProgressBar
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
        android:id="@+id/progressBar1"/>
  
    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="This is a notification"/>

</LinearLayout>

Upvotes: 1

Related Questions