1

I am trying to create a homescreen widget using the 3.1 SDK. I followed the StackWidget tutorial. and changed the StackView to ListView. I want to add an image and text to each row in my list, and these are loaded at runtime.

I use this code in my WidgetProvider.java class

public RemoteViews getViewAt(int position) {

    Bitmap bitmap = ((BitmapDrawable)mWidgetItems.get(position).getImage());

    RemoteViews image = new RemoteViews(mContext.getPackageName(), R.layout.widget_item);      
    image.setImageViewBitmap(R.id.widget_image, bitmap );

    RemoteViews text = new RemoteViews(mContext.getPackageName(), R.layout.widget_item); 
    text.setTextViewText(R.id.widget_text, mWidgetItems.get(position).getText());

    RemoteViews layout = new RemoteViews(mContext.getPackageName(), R.layout.widget_item);

    Bundle extras = new Bundle();
    extras.putInt(StackWidgetProvider.EXTRA_ITEM, position);
    Intent fillInIntent = new Intent();
    fillInIntent.putExtras(extras);

    layout.setOnClickFillInIntent(R.id.widget_item, fillInIntent);

    return layout;
}

All of this works correctly when I use StackView, but changing to ListView messes up everything. The result is this

enter image description here

Only one view can be returned. So, if I add the following code, this problem occurs, if I don't add it, then the content of the list is empty.

layout.addView(R.id.widget_item, image);
layout.addView(R.id.widget_item, text);

How else do I add multple views to a widget? Why does this work for StackView but not ListView? and how do I rectify this?

Any help is greatly appreciated.

EDIT

widget_item.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/widget_item"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:gravity="center_vertical">
<ImageView
    android:id="@+id/widget_image"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:padding="10dp"/>
<TextView
    android:id="@+id/widget_text"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:padding="20dp"
    android:layout_weight="1"
    android:textColor="#000000"/>       
</LinearLayout>
1
  • what I was trying to do above was show a list of all the apps with package name starting with com.google.android and its associated icons. Commented Jul 7, 2011 at 12:08

1 Answer 1

1

You rectify this by inflating the right layout from the outset. R.layout.widget_image should already have whatever stuff you have in R.layout.widget_image and R.layout.widget_item.

This will be:

  • faster to execute (fewer instructions to achieve same visual end)
  • less Java code
  • less layout code (one layout file versus three)
  • easier to maintain layout code (you can use the visual editor in Eclipse to maintain your list rows better)

And, as an extra bonus, it should work for any supported AdapterView-based app widget.

Sign up to request clarification or add additional context in comments.

5 Comments

Umm.. I dont think I understood it fully.. sorry, but this widget concept is a little hard for me to grasp. I have updated the question and added the layout code. Could you please tell me what changes I should make to the xml and the code?
@rohith: You have widget_item.xml, widget_text.xml, and widget_image.xml as three separate files. Combine those into one file (presumably still named widget_item.xml), holding your LinearLayout, your TextView, and your ImageView. Delete the other two layout files. Delete where you are referring to those other two files in your Java code.
But then how do I set the text and image in the respective views?
@rohith: With your existing code. Your setTextViewText() calls and such do not change.
I have updated the code above. If I use addView() and add image and text, the issue persists.. if I dont, then the contents of the ListView are empty.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.