Change remoteView ImageView background

I have some remoteView with ImageViews, and I need to change the “android:background” programmatically.

I know how to change the “android:src” with:

  • Mock GPS Provider only works while in Debug mode
  • Google Play Staged Rollout with modified screenshots
  • Android: How to create a directory on the SD Card and copy files from /res/raw to it?
  • Page change callback for PagerAdapter
  • What does “enable DOM storage API” mean?
  • Color of TextView shows as white on a real device with ConstraintLayout
  • remoteView.setImageViewResource(int viewId, int srcId);

    And it works fine, but how do I change the “android:background”?


    Related posts:

    ViewPager detect when user is trying to swipe out of bounds
    Is this custom CursorAdapter for a ListView properly coded for Android?
    Spinner functionality not working on Android 6.0.1
    Showing dex method count by package
    How to make Button highlight?
    Aquery ajax request seems not starting in android
  • Combining Spannable with String.format()
  • Difference between Preference's onCreateView and onBindView methods
  • How can I pass data from activity to the dialogFragment that activity invoked?
  • Tabs+Swipe in Android for all API levels
  • Setting custom key when pushing new data to firebase database
  • Android: Ringer mode changed to silent doesn't stop the phone vibrating
  • 3 Solutions collect form web for “Change remoteView ImageView background”

    You can use public void setInt (int viewId, String methodName, int value) method.

    remoteView.setInt(, "setBackgroundResource", R.color.your_color)

    Use setBackgroundResource(int).

    Also, here is something you may take note of. When you look at Android documentation, if there is an xml element for a View that you can change, it normally points to the method to make the change at runtime.

    The jerry-rig way

    I think you could do it using the jerry-rig way doing a second layout with the new background, then you create your remoteView using this new layout, like this:

    • RemoteViews remoteViews = new RemoteViews(context.getPackageName(), R.layout.widget1);
    • RemoteViews remoteViews = new RemoteViews(context.getPackageName(), R.layout.widget2);

    after all, a layout isn’t so expensive.

    Android Babe is a Google Android Fan, All about Android Phones, Android Wear, Android Dev and Android Games Apps and so on.