1. Download our Official Android App: Forums for Android!

Apps Widget background

Discussion in 'Android Development' started by erdomester, Feb 12, 2011.

  1. erdomester

    erdomester Newbie
    Thread Starter
    Rank:
    None
    Points:
    16
    Posts:
    17
    Joined:
    Jan 8, 2011

    Jan 8, 2011
    17
    2
    16
    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 (Text):
    1.  
    2. RemoteViews updateViews = new RemoteViews(EditPreferences.this.getPackageName(), R.layout.main);
    3. updateViews.setImageViewBitmap(R.id.widgetlayout, ((BitmapDrawable)EditPreferences.this.getResources().getDrawable(R.drawable.brownbg)).getBitmap());
    4. ComponentName thisWidget = new ComponentName(EditPreferences.this, HelloWidget.class);
    5. AppWidgetManager manager = AppWidgetManager.getInstance(EditPreferences.this);
    6. manager.updateAppWidget(thisWidget, updateViews);
    7.  
    LogCat says:
    Code (Text):
    1.  
    2. 02-12 12:10:26.294: WARN/AppWidgetHostView(122): updateAppWidget couldn't find any view, using error view
    3. 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)
    4.  
    It doesn't like RelativeLayout either...
    Is that because it needs an ImageView? Fine:

    Code (Text):
    1.  
    2. <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    3.     android:orientation="vertical"
    4.     android:layout_width="146dip"
    5.     android:layout_height="72dip"
    6.     android:layout_gravity="center"
    7.     android:background="@drawable/goldgreenbg"
    8.     android:id="@+id/widgetlayout">
    9. <ImageView android:id="@+id/ImageView01"
    10.      android:layout_width="146dip"
    11.      android:layout_height="72dip">
    12. </ImageView>
    13. <TextView android:id="@+id/widget_textview"
    14.     android:text="@string/widget_text"
    15.     android:layout_height="wrap_content"
    16.     android:layout_width="wrap_content"
    17.     android:layout_gravity="center_horizontal|center"
    18.     android:gravity="center_horizontal"
    19.     android:layout_marginTop="0dip"
    20.     android:padding="0dip"
    21.     android:textColor="#0B3B0B"
    22.     android:textSize="11sp"/>
    23. <TextView android:id="@+id/widget_textview2"
    24.     android:text="@string/widget_text"
    25.     android:layout_height="wrap_content"
    26.     android:layout_width="wrap_content"
    27.     android:layout_gravity="center_horizontal|center"
    28.     android:gravity="center_horizontal"
    29.     android:layout_marginTop="0dip"
    30.     android:padding="0dip"
    31.     android:textSize="12sp"
    32.     android:textColor="#FFFFFF"/>
    33. <TextView android:id="@+id/widget_textview3"
    34.     android:text="@string/widget_text"
    35.     android:layout_height="wrap_content"
    36.     android:layout_width="wrap_content"
    37.     android:layout_gravity="center_horizontal|center"
    38.     android:layout_marginTop="0dip"
    39.     android:padding="0dip"
    40.     android:textSize="9sp"
    41.     android:textColor="#0B3B0B"/>
    42. </RelativeLayout>
    43.  
    I displays the background now, BUT the imageview is thinner than the layout even if they have the same size! Why is that?
     

    Advertisement

  2. erdomester

    erdomester Newbie
    Thread Starter
    Rank:
    None
    Points:
    16
    Posts:
    17
    Joined:
    Jan 8, 2011

    Jan 8, 2011
    17
    2
    16
    I have figured it out.
    I had put this line to the ImageView in the xml file:

    Code (Text):
    1.  
    2. android:scaleType="fitXY"
    3.  
    This way the imageview is as big as the layout.
     

Share This Page

Loading...