0

how do I use an imageswitcher in a widget?

my code compiles fine, but when I try to create my widget it gives an error:

Error inflating AppWidget AppWidgetProviderInfo(provider=ComponentInfo{test.basic/test.basic.HelloWorldWidget}): android.view.InflateException: Binary XML file line #5: Error inflating class android.widget.ImageSwitcher

my xml code for my imageswitcher is:

<ImageSwitcher android:id="@+id/image_switcher"
    android:layout_width="fill_parent" android:layout_height="fill_parent" />

and my code for creating a viewfactory is:

    @Override
public void onEnabled(final Context context) {
    readImages(context);

    ImageSwitcher imageSwitcher = getImageSwitcher(context);
    imageSwitcher.setFactory(new ViewFactory() {

        public View makeView() {
            ImageView imageView = new ImageView(context);
            imageView.setBackgroundColor(0xFF000000);
            imageView.setScaleType(ImageView.ScaleType.FIT_CENTER);
            imageView.setLayoutParams(new ImageSwitcher.LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT));
            return imageView;
        }
    });
}

any help would be strongly appreciated.

thank you

1 Answers1

1

I don't think it's possible to use an ImageSwitcher in an AppWidget. The AppWidget documentation (http://developer.android.com/guide/topics/appwidgets/index.html) lists seven widget types that may be used when building an AppWidget. And ImageSwicher is not on the list.

Your best bet will probably be to use a FrameLayout or a RelativeLayout with multiple ImageView's stacked on top of each other, and then set their visibility one by one until only the currently desired image is visible.

  • but then the problem becomes how I use animation between the images – Michael Studebaker Feb 27 '11 at 22:21
  • Yeah, I tried once to use LayoutAnimations in my widget and the result was not too good. They got triggered at funny times, for example when the user navigated to a home screen with one of my widgets for the first time after an upgrade. If you really wants animations, I think you have to use an AlarmManager (or some other type of scheduling mechanism) to schedule, say, five updates with 100 ms intervals and then manipulate the Alpha of your images to achieve a fade-in/fade-out effect, for example. – Nicolai Buch-Andersen Feb 28 '11 at 13:51