How to set custom time to Android AnalogClock placed in app widget?
As an alternative I was thinking to override default AnalogClock to set the time through codes. Or in other words, I would create my custom Clock which extends from default View or AnalogClock. Then put my custom Clock on widget layout UI.
Is this possible? I’m afraid we are limited on RemoteViews to have our own custom Component.
UPDATE:
This is error log I have following the solution given by vArDo at first.

Intro
It’s not possible to include custom views in app widgets. According to documentation only small predefined subset of views can be used in layout. So, as you’ve mentioned, it’s not possible to create your own
AnalogClockview (as I’ve shown in my other answer) and include it in app widget.However there are other means that can be used to create custom
AnalogClockin app widget. In such implementation setting time directly inAnalogClockshould not be a problem.Short answer
One of the ways to accomplish this is draw custom
AnalogClockintoImageView(which is one of the allowed views in app widgets). RepeatingPendingIntentis usingServiceto draw intoImageViewevery 60 seconds (viaRemoteViews).Note on battery usage
Keep in mind that invoking
Serviceaction to update app widget viaRemoteViewsevery 60 seconds is not so lightweight on battery. On Jelly Bean example implementation used 2% of battery during night (screen off, average network strength). However, I’ve been updating the screen every 15 seconds, which is not necessary when it comes to analog clocks. So a rough estimate would be that an app widget with one update per minute will consume about 0.5% of battery juice during night – this might be significant for your users.Solution details
Changes drawing service
First of all you’ll have to create
Servicethat will draw intoImageViewpresent in your view. Drawing code is pretty much taken fromAnalogClockimplementation and rearrange according to needs. First of all, there are no checks on whether time has changed – this is becauseServicewill be invoked only when we decide too (e.g. every 60 seconds). Seconds change is that we createDrawables from resources in the method.Another change is that code draws analog clock into local
BitmapusingCanvas:Update is drawn into
ImageViewviaRemoteViews. Changes are aggregated and then pushed into app widget on home screen – see docs for details). In our case there is only one change, and it is done through special method:Bare in mind that for code to work you have to declare what is available space for drawing your analog clock. Based on what is declare in your
widget_provider.xml, you can determine the dimensions ofImageView. Note that you’ll have to convertdpunit topxunit, before drawing:I’ve pasted full code of
Servicesubclass here.Also at the top you’ll find the code that is responsible for setting time that will be displayed – modify as needed:
Also don’t forget to declare this
Serviceand app widget in yourAndroidManifest.xmlfile, e.g.:Most important info about using services to update app widget is described shortly in this blog post.
Invoking drawing
Scheduling updates is done in
onUpdate()method of yourAppWidgetProvidersubclass – the on that is declare in yourAndroidManifest.xmlfile. We’ll also need to removePendingIntentwhen app widget is removed from home screen. This is done in overriddenonDisabled()method. Remember to declare both actions that will call one of each methods:APPWIDGET_UPDATEandAPPWIDGET_DISABLED. See excerpt fromAndroidManifest.xmlabove.Last line of
onUpdate()method schedules a repeating event to happen every 60 seconds, with first update to happen right away.Drawing
AnalogClockonce onlyUsing above code the simplest way to invoke service only once is to schedule single event in time instead of repeating one. Simple replace
alarmManager.setRepeating()call with following line:Results
Below is a screen shot with custom
AnalogClockapp widget that had time set only once (note difference between analog clock time and time shown in status bar):