Monday, 15 July 2013

android - Proper way to update ImageView in a RemoteViews? -



android - Proper way to update ImageView in a RemoteViews? -

i'm trying fetch image remote url , set imageview. imageview within app widget, current understanding means encapsulated within remoteviews , isn't handled directly.

but can never app widget display image. i'm fetching image bitmap remote url using asynctask extension, , seems image beingness fetched ok (a non-null bitmap returned anyway), though yet can't display confirm.

i reckon must using wrong method update app widget's imageview.

here relevant code:

layout/new_app_widget.xml:

<relativelayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:padding="@dimen/widget_margin" > <imageview android:layout_width="match_parent" android:layout_height="match_parent" android:id="@+id/imageview" android:layout_alignparenttop="true" android:layout_alignparentleft="true" android:contentdescription="@string/content" /> </relativelayout>

relevant bit newappwidget.java:

public class newappwidget extends appwidgetprovider { ... static void updateappwidget(context context, appwidgetmanager appwidgetmanager, int appwidgetid) { // build remoteviews object remoteviews views = new remoteviews(context.getpackagename(), r.layout.new_app_widget); string strurl = "http://example.com/yqlosfr4.png"; new imagefetchasynctask(strurl, r.id.imageview).execute(); // ^^^^^ // in above, pass url , id of imageview, fetch // bitmap, , in onpostexecute() method utilize // setimageviewbitmap(viewid, bitmap) load bitmap imageview // // don't have code hand, can provide it. now, // may able tell me somewhere else i'm doing wrong // instruct widget manager update widget appwidgetmanager.updateappwidget(appwidgetid, views); } }

androidmanifest.xml:

<?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.example.mike.widgetapptest" > <uses-permission android:name="android.permission.internet" /> <application android:allowbackup="true" android:icon="@drawable/ic_launcher" android:label="@string/app_name" android:theme="@style/apptheme" > android:debuggable="true" <activity android:name=".myactivity" android:label="@string/app_name" > <intent-filter> <action android:name="android.intent.action.main" /> <category android:name="android.intent.category.launcher" /> </intent-filter> </activity> <receiver android:name=".newappwidget" > <intent-filter> <action android:name="android.appwidget.action.appwidget_update" /> </intent-filter> <meta-data android:name="android.appwidget.provider" android:resource="@xml/new_app_widget_info" /> </receiver> </application> </manifest>

classic beginner's error... attempting next async task, expecting async task have completed. moved depends on result of async task onpostexecute() method of async task, , voila... image displayed in app widget!

android android-appwidget remoteview

No comments:

Post a Comment