Search code examples
androidandroid-appwidgetviewflipperremoteview

How to use ViewFlipper to dynamically add images in AppWidget like ad rotation


I'm trying to make AppWidget shows rotation images which can automatically change every 5 seconds,and I think ViewFlipper might be able to reach this goal.

But I get images from server and can't just put them as ImageView in .xml file under nested ViewFlipper like this:

<ViewFlipper android:id="@+id/viewflipper" 
 android:layout_width="match_parent" 
 android:layout_height="match_content"
 android:autostart="true" 
 android:flipinterval="5000"  > 

  <ImageView 
     android:layout_width="match_parent"
     android:layout_height="match_parent"
     android:src="@drawable/drawable1"/>

  <ImageView 
     android:layout_width="match_parent"
     android:layout_height="match_parent"
     android:src="@drawable/drawable2"/>

</ViewFlipper>

I need to add images dynamically, and shows one picture at one time. If there's more than one image, they need to be shown on AppWidget by turns.

Hope somebody could help me with this, thanks!


Solution

  • I assume by "dynamically" you mean there could be an arbitrary number of items. What you probably want is AdapterViewFlipper. You can follow this guide for building an AppWidget whose content is backed by an adapter.