I'm working on my first app widget. Everthing is fine except one thing. I can't find out, how can I load image into ImageView at RemoteViewsFactroy's getViewAt() method with Glide. I allways get error, because I've to run into(AppWidgetTarget) at MainThread, but I can't call new Handler(Looper.getMainLooper()) from RemoteViewsFactory.
Glide has support for this by providing the AppWidgetTarget class.
While updating your widget in onUpdate()
, you create a new AppWidgetTarget, pass it the RemoteViews
object, the id of your image view, and the appwidgetId and then invoke glide in nearly the same way as you would for an imageview in your app.
appWidgetTarget = new AppWidgetTarget( context, rv, R.id.custom_view_image, appWidgetIds );
Glide
.with( context.getApplicationContext() ) // safer!
.load( GlideExampleActivity.eatFoodyImages[3] )
.asBitmap()
.into( appWidgetTarget );
Example code above taken from this wonderful set of glide tutorials. The tutorials cover many practical aspects of using glide.
Similarly, a NotificationTarget target class is also provided by glide - it solves the same problem, but for notifications instead of appWidgets.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With