tags:

views:

2068

answers:

4

I am trying to create an AppWidget, in which the background color of a TextView changes at random at specified periodic interval.

The TextView is defined in layout xml file as

<?xml version="1.0" encoding="utf-8" ?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/widget"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:orientation="vertical" >
    <TextView  
        android:id="@+id/message"
        android:background="#ff99ff"
        android:text="Hello Widget" />
</LinearLayout>

In update method, i have loaded the layout as

RemoteViews remoteView=new RemoteViews(context.getPackageName(),R.layout.widget_message);

To change the background of TextView i used the following statement

remoteView.setInt(R.id.message, "setBackgroundResource", R.color.col_1);

But i am getting a widget saying problem loading widget. If i remove the above line everything works fine.

LogCat says:

updateAppWidget couldn't find any view, using error view

android.widget.RemoteViews$ActionException: view: android.widget.TextView can't use method with RemoteViews: setBackgroundResource(int)

A: 

The reason is that via RemoteViews you can call just limited amount of methods. In case that it is prohibited you get message like this.

Tom

Tomáš Hubálek
+1  A: 

Tomas is correct. My solution is to make two views with the respective backgrounds and make one INVISIBLE and the other one VISIBLE. Of course this only works with a small number of backgrounds, e.g., "green" and "red" that might indicate some state.

mobibob
A: 

What i find weird about this is that it works great on my nexus one ( 2.2 ), but not at all on an HTC Tattoo ( 1.6 ). I'm going to try and run some emulator tests and see if it's not just another case of HTC lazily implementing some underlying UI rendering code, which i already found with the Tattoo ( Layouts render differently than on stock Android 1.6 ).

What device were you testing this on?

DavidG
Doesn't work on my 2.1 emulator. Maybe the restriction was removed in Android 2.2?? See also [this discussion](http://groups.google.com/group/android-developers/browse_thread/thread/aa000a36eadafaa5/a406589560d76784#a406589560d76784) on the android-developers group: sounds like it's a well-known limitation
MarkJ
Now what i do is:views.setImageViewBitmap(R.id.background, BitmapFactory.decodeResource(context.getResources(), info.backgroundImageId) );It's far more annoying and less efficient, but it allows me now to add downloadable themes
DavidG
A: 

Is there a workaround for this problem on all devices?