In this last part of the tutorial we will implement buttons to the appWidget, which will directly interact with the appWidget functionality.
We will have 2 buttons, a plus button to add one more day to the target date, and a minus button to decrease time left by one day.
First add the buttons to the countdownwidget.xml layout:
As described in the previous part, we will use a Service to update the appWidget.
So we will have the Service below, which gets the command (right now we have only the update command), ant the appwidgetId, reads the date from sharedPreferences and updates the widget.
I just come to a new discovery regarding widgets. I was developing an appwidget, which - just like the widgets we are trying to make in this series of tutorials - and tried it out multiple phones. Unfortunatelly on one of our test phones it didn't function properly. For random intervals it stopped to refresh, and it din't responded to button presses on the widget, only after 1-2 minutes. After hard work I discivered the following:
Buttons on appwiget can have their onclick flunctionality thorough RemoteViews.setOnClickPendingIntent(). This method gets a PendingIntent ap parametes to bound to a button. PendingIntent's can have 3 types created with
getActivity(Context, int, Intent, int)
Would you like to disable a button on an appwidget?
As far as I know it can not be done, since the appwiget UI manipulation is limited by the methods of the RemoteViews class.
But if you insist to do that there is a way to make it look like the button were disabled!
RemoteViews can't manipulate a buttons enabled/disabled state, but it can modify its visibility. So the trick is to have two buttons, the real one, and an other which is designed to look like the real one in disabled state, and change witch one is visible.
Lets see a simple example:
We want to have two buttons on the widget, a stop and a start button in order to stop and start some kind of functionality. Once we have started it, we can not start it agin, until we stopped it and vica versa, so we want to disable the button which can not be used right now.
The XML definition of the buttons can be like this: