Apps Widget background

Hello,

why cannot i change the background image of my widget with this code? All get is a text saying "Problem loading widget" in place of the widget. Everything looks fine with the code.

Code:
RemoteViews updateViews = new RemoteViews(EditPreferences.this.getPackageName(), R.layout.main);
updateViews.setImageViewBitmap(R.id.widgetlayout, ((BitmapDrawable)EditPreferences.this.getResources().getDrawable(R.drawable.brownbg)).getBitmap());
ComponentName thisWidget = new ComponentName(EditPreferences.this, HelloWidget.class);
AppWidgetManager manager = AppWidgetManager.getInstance(EditPreferences.this);
manager.updateAppWidget(thisWidget, updateViews);

LogCat says:
Code:
02-12 12:10:26.294: WARN/AppWidgetHostView(122): updateAppWidget couldn't find any view, using error view
02-12 12:10:26.294: WARN/AppWidgetHostView(122): android.widget.RemoteViews$ActionException: view: android.widget.LinearLayout doesn't have method: setImageBitmap(android.graphics.Bitmap)

It doesn't like RelativeLayout either...
Is that because it needs an ImageView? Fine:

Code:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="146dip"
    android:layout_height="72dip"
    android:layout_gravity="center"
    android:background="@drawable/goldgreenbg"
    android:id="@+id/widgetlayout">
<ImageView android:id="@+id/ImageView01"
 	 android:layout_width="146dip" 
	 android:layout_height="72dip">
</ImageView>
<TextView android:id="@+id/widget_textview"
    android:text="@string/widget_text"
	android:layout_height="wrap_content"
	android:layout_width="wrap_content"
	android:layout_gravity="center_horizontal|center"
	android:gravity="center_horizontal"
	android:layout_marginTop="0dip"
	android:padding="0dip"
	android:textColor="#0B3B0B"
	android:textSize="11sp"/>
<TextView android:id="@+id/widget_textview2"
    android:text="@string/widget_text"
	android:layout_height="wrap_content"
	android:layout_width="wrap_content"
	android:layout_gravity="center_horizontal|center"
	android:gravity="center_horizontal"
	android:layout_marginTop="0dip"
	android:padding="0dip"
	android:textSize="12sp"
	android:textColor="#FFFFFF"/>
<TextView android:id="@+id/widget_textview3"
    android:text="@string/widget_text"
	android:layout_height="wrap_content"
	android:layout_width="wrap_content"
	android:layout_gravity="center_horizontal|center"
	android:layout_marginTop="0dip"
	android:padding="0dip"
	android:textSize="9sp"
	android:textColor="#0B3B0B"/>
</RelativeLayout>

I displays the background now, BUT the imageview is thinner than the layout even if they have the same size! Why is that?
 

erdomester

Newbie
Thread starter
I have figured it out.
I had put this line to the ImageView in the xml file:

Code:
android:scaleType="fitXY"

This way the imageview is as big as the layout.
 
Top