Search code examples
javaandroidandroid-pendingintentremoteview

Changing a background image of a remoteView button?


Cant quite figure this one out. I have a button on my Activity that once pressed opens a notification. This custom notification has a button on it. I'm trying to change the notifications background image once its pressed.

Here's what I've used so far with no luck.

This is a service class that gets called once the "notifcation_Button" gets pressed. ( I have a pending intent that calls this class)

  public class newService extends Service {

  @Override
  public int onStartCommand(Intent intent, int flags, int startId) {

    Log.d("newService", "Newservice");

    RemoteViews remoteView = new RemoteViews(getPackageName(), R.layout.custom_layout); //this is my custom notification

    remoteView.setInt(R.id.StartButton_Notification, "setBackgroundColor", R.drawable.sungrey);
    remoteView.setInt(R.id.StartButton_Notification, "setBackgroundResource", sungrey);
    remoteView.setImageViewResource(R.id.StartButton_Notification, R.drawable.sungrey);

    return START_NOT_STICKY;
}

Solution

  • So the issue for anyone wondering is that NotificationCompat.Builder isnt being updated, so where ever you have it originally, it needs to be updated with remoteView.setInt(R.id.yourDrawable, "setBackgroundResource", newDrawable);. Either toss it in a method and update it that way or bring the whole sucker into the class where the change happens.