Android AppWidget TextView: How to set background color at run time

Posted by Amit on Stack Overflow See other posts from Stack Overflow or by Amit
Published on 2010-03-16T10:28:13Z Indexed on 2010/04/01 5:13 UTC
Read the original article Hit count: 805

Filed under:
|

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)

© Stack Overflow or respective owner

Related posts about android

Related posts about android-sdk