22

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

I know how to change the "android:src" with:

remoteView.setImageViewResource(int viewId, int srcId);

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

Thanks

BrainCrash
  • 12,992
  • 3
  • 32
  • 38

3 Answers3

75

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

remoteView.setInt(R.id.viewid, "setBackgroundResource", R.color.your_color)
jamapag
  • 9,290
  • 4
  • 34
  • 28
  • It's work from Android SDK version > 7 in SDK 7 no @RemotableViewMethod annotation [source here](http://grepcode.com/file/repository.grepcode.com/java/ext/com.google.android/android/2.1_r2/android/view/View.java#View.setBackgroundResource%28int%29) and in SDK 8 it added [source here](http://grepcode.com/file/repository.grepcode.com/java/ext/com.google.android/android/2.2_r1.1/android/view/View.java#View.setBackgroundResource%28int%29) and this annotation only a reason why setBackgroundResource() method don't work. – Nikolay Nikiforchuk Jan 16 '13 at 08:42
  • 1
    In a (home-page) widget that uses a ListView, this setInt() technique worked for an item's *inner* layouts but not the item's outermost view; I assume the system is resetting the bg of the outermost view to its own selector. – larham1 Dec 02 '13 at 07:54
0

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.

Charleston
  • 1,539
  • 18
  • 10
0

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.

nicholas.hauschild
  • 42,483
  • 9
  • 127
  • 120
  • 2
    I know the use of setBackgroundResource(int), but unfortunately it can't be used on remoteViews. – BrainCrash Jun 13 '11 at 19:25
  • 1
    The question fails to mention that you are using a RemoveViews object. Please be more specific. – nicholas.hauschild Jun 13 '11 at 19:31
  • 2
    The word "widget" is present in the title and description, also the example starts with "remoteView". I will edit to make it more clear anyway. – BrainCrash Jun 16 '11 at 11:02
  • 1
    http://developer.android.com/reference/android/widget/package-summary.html Essentially every view is in the widget package. Home screen widgets are widgets. But widgets aren't homescreen widgets. Also, your variable naming scheme shouldn't be your only documentation or description. – Falmarri Jul 07 '11 at 18:50
  • 2
    To be fair, the confusion between "widget" and "app widget / home screen widget" comes from the official documentation. – Jose_GD Mar 26 '14 at 14:17