The PendingIntent Cache Reuse And Multiple Android Widgets Issue
Posted by admin on
June 29, 2012
This was a pain to find out. While my Android widget was working okay considering that I only set it to have only 1 size, it took me a very long time to find out that the same PendingIntent object is used by the Android system when certain attributes are created the same way e.g. class, action, request code.
The quickest and easiest solution is to use the appWidgetId as the value of the request code in the PendingIntent parameter when you either call the getActivity(), getBroadcast() or getService() methods.
That should make it unique in such a way that the PendingIntent object will be used only for that certain widget no matter which size or which instance it is.
Other options would be to give different values for its setData() and setAction() methods like adding a System.currentTimeMillis() but sometimes we may want our action value to be fixed so the best option in my opinion is to focus on the request code value having a unique value.
That should give make your multiple widget instances work.
Donations appreciated. Every little $ helps. Or click Google +1. Or play the short video on the upper right of this page.tags: multiple, pendingintent, requestcode, setaction, setdata, widget
No Comments
Maqruee Scrolling TextView In Android Widget
Posted by admin on
April 10, 2012
An Android Widget does not provide any way to get a reference to any control like a TextView. So what if you wanted a horizontal scrolling marquee TextView?
While doing it in an app is pretty easy since you can get hold of a TextView object and even doing it programmatically by giving focus to the TextView object, a widget is different. However, there is a way within the XML to make this happen.
See the XML example.
<TextView android:id="@+id/mytextview" android:text="this is a horizontal scrolling text sample ..." android:singleLine="true" android:ellipsize="marquee" android:layout_width="wrap_content" android:layout_height="wrap_content" android:marqueeRepeatLimit="marquee_forever" android:scrollHorizontally="true" android:focusable="true" android:focusableInTouchMode="true" android:duplicateParentState="true" > <requestFocus android:focusable="true" android:focusableInTouchMode="true" android:duplicateParentState="true" /> </TextView>
This way, whenever your widget gets called, the TextView that will display the marquee text will have focus right away and will be able to show the horizontal animating effect that you so want.
Donations appreciated. Every little $ helps. Or click Google +1. Or play the short video on the upper right of this page.Multiple Instances Of Widget Only Updating Last Widget
Posted by admin on
September 26, 2011
This took me awhile to figure it out because whenever I navigate using the buttons in one of the widget, it affects all other instances of that widget. But thankfully, some forum member in Stack Overflow named snailer provided a solution to making each Intent object unique per instance of an Android widget.
Just add the following code to your Intent object.
Uri data = Uri.withAppendedPath(Uri.parse(URI_SCHEME + "://widget/id/"), String.valueOf(appWidgetId)); intent.setData(data);
The variable URI_SCHEME is a String, and can be whatever you would like. ie - “ABCD” This causes each widget to have a unique PendingIntent.
Donations appreciated. Every little $ helps. Or click Google +1. Or play the short video on the upper right of this page.









