java - ClipDrawable not working when used as layout background for widget -
i'm attempting make simple widget displays battery percentage both textually , graphically in widget. textual part works without problem, i'm having great difficulty getting widget graphically update.
graphically, have battery image clip according battery percentage. i'm attempting use clipdrawable
this.
battery_widget_layout.xml
<linearlayout xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/widgetlayout" android:layout_width="fill_parent" android:layout_height="fill_parent" android:orientation="horizontal" android:padding="@dimen/widget_padding" android:background="@drawable/battery_clip_layer" > <textview android:id="@+id/batterypercentagewidgettextview" android:layout_width="fill_parent" android:layout_height="fill_parent" android:text="@string/battery_percentage_widget_default" android:gravity="center" /> </linearlayout>
battery_clip_layer.xml (ie clipdrawable)
<clip xmlns:android="http://schemas.android.com/apk/res/android" android:cliporientation="vertical" android:gravity="bottom" android:drawable="@drawable/battery_shape" />
batteryservice.java - service receives battery events , updates widget via remoteviews
public class batteryservice extends service { private static final string log = batteryservice.class.getname(); private final atomicinteger batterypercentage = new atomicinteger(100); private final broadcastreceiver batteryupdatereceiver = new broadcastreceiver() { @override public void onreceive(context context, intent intent) { int level = intent.getintextra("level", 0); batterypercentage.set(level); updatewidget(); } }; @override public void oncreate() { super.oncreate(); registerreceiver(batteryupdatereceiver, new intentfilter(intent.action_battery_changed)); log.i(log, "created..."); } @override public void ondestroy() { super.ondestroy(); unregisterreceiver(batteryupdatereceiver); log.i(log, "destroyed..."); } @override public int onstartcommand(intent intent, int flags, int startid) { log.i(log, "started..."); updatewidget(); return super.onstartcommand(intent, flags, startid); } @override public ibinder onbind(intent arg0) { return null; } private void updatewidget() { final int percentage = batterypercentage.get(); log.i(log, "updated... " + percentage); remoteviews remoteviews = new remoteviews(getpackagename(), r.layout.battery_widget_layout); remoteviews.settextviewtext(r.id.batterypercentagewidgettextview, percentage + "%"); //attempt 1 - no cigar remoteviews.setint(r.drawable.battery_clip_layer, "setlevel", 5000); appwidgetmanager appwidgetmanager = appwidgetmanager.getinstance(this); appwidgetmanager.updateappwidget(new componentname(this, batteryappwidgetprovider.class), remoteviews); } private void updatewidgetattempt2() { final int percentage = batterypercentage.get(); log.i(log, "updated... " + percentage); //attempt 2 - still no cigar drawable cliplayer = getapplicationcontext().getresources().getdrawable(r.drawable.battery_clip_layer); if (cliplayer instanceof clipdrawable) { clipdrawable clipdrawable = (clipdrawable) cliplayer; int level = clipdrawable.getlevel(); if (clipdrawable.setlevel(10000)) { clipdrawable.invalidateself(); } log.i(log, "updated clip amount..." + level + " -> " + ((clipdrawable) cliplayer).getlevel()); } remoteviews remoteviews = new remoteviews(getpackagename(), r.layout.battery_widget_layout); remoteviews.settextviewtext(r.id.batterypercentagewidgettextview, percentage + "%"); appwidgetmanager appwidgetmanager = appwidgetmanager.getinstance(this); appwidgetmanager.updateappwidget(new componentname(this, batteryappwidgetprovider.class), remoteviews); } }
in batteryservice
please have @ 2 different attempts update widget (updatewidget()
, updatewidgetattempt2()
). neither attempt successful.
i feel i'm doing fundamentally wrong. i'm grateful help/advice! :)
you correct in need set level on drawable instance, unfortunately can't @ drawable because it's in remote view.
however, imageview have method called setimagelevel
, can call remotely. suggestion put drawable in imageview instead of background of container, , use setint(r.id.image_view, "setimagelevel", level)
.
here's layout should like:
<framelayout xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/widgetlayout" android:layout_width="match_parent" android:layout_height="match_parent" android:padding="@dimen/widget_padding" > <imageview android:id="@+id/battery" android:layout_width="match_parent" android:layout_height="match_parent" android:src="@drawable/battery_clip_layer" /> <textview android:id="@+id/batterypercentagewidgettextview" android:layout_width="match_parent" android:layout_height="match_parent" android:text="@string/battery_percentage_widget_default" android:gravity="center" /> </framelayout>
and updatewidget()
method:
private void updatewidget() { final int percentage = batterypercentage.get(); log.i(log, "updated... " + percentage); remoteviews remoteviews = new remoteviews(getpackagename(), r.layout.battery_widget_layout); remoteviews.settextviewtext(r.id.batterypercentagewidgettextview, percentage + "%"); remoteviews.setint(r.id.battery, "setimagelevel", percentage * 100); appwidgetmanager appwidgetmanager = appwidgetmanager.getinstance(this); appwidgetmanager.updateappwidget(new componentname(this, batteryappwidgetprovider.class), remoteviews); }
Comments
Post a Comment