Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8374977
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T15:03:40+00:00 2026-06-09T15:03:40+00:00

How to set custom time to Android AnalogClock placed in app widget? As an

  • 0

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.

enter image description here

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-09T15:03:42+00:00Added an answer on June 9, 2026 at 3:03 pm

    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 AnalogClock view (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 AnalogClock in app widget. In such implementation setting time directly in AnalogClock should not be a problem.

    Short answer

    One of the ways to accomplish this is draw custom AnalogClock into ImageView (which is one of the allowed views in app widgets). Repeating PendingIntent is using Service to draw into ImageView every 60 seconds (via RemoteViews).

    Note on battery usage

    Keep in mind that invoking Service action to update app widget via RemoteViews every 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 Service that will draw into ImageView present in your view. Drawing code is pretty much taken from AnalogClock implementation and rearrange according to needs. First of all, there are no checks on whether time has changed – this is because Service will be invoked only when we decide too (e.g. every 60 seconds). Seconds change is that we create Drawables from resources in the method.

    Another change is that code draws analog clock into local Bitmap using Canvas:

        // Creating Bitmap and Canvas to which analog clock will be drawn.
        Bitmap appWidgetBitmap = Bitmap.createBitmap(availableWidth, availableHeight, Bitmap.Config.ARGB_8888);
        Canvas canvas = new Canvas(appWidgetBitmap);
    

    Update is drawn into ImageView via RemoteViews. 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:

        remoteViews.setImageViewBitmap(R.id.imageView1, appWidgetBitmap);
    

    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 of ImageView. Note that you’ll have to convert dp unit to px unit, before drawing:

        // You'll have to determine tour own dimensions of space to which analog clock is drawn.
        final int APP_WIDGET_WIDTH_DP = 210; // Taken from widget_provider.xml: android:minWidth="210dp"
        final int APP_WIDGET_HEIGHT_DP = 210; // Taken from widget_provider.xml: android:minHeight="210dp"
    
        final int availableWidth = (int) (TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, APP_WIDGET_WIDTH_DP, r.getDisplayMetrics()) + 0.5f);
        final int availableHeight = (int) (TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, APP_WIDGET_HEIGHT_DP, r.getDisplayMetrics()) + 0.5f);
    

    I’ve pasted full code of Service subclass here.

    Also at the top you’ll find the code that is responsible for setting time that will be displayed – modify as needed:

        mCalendar = new Time();
    
        // Line below sets time that will be displayed - modify if needed.
        mCalendar.setToNow();
    
        int hour = mCalendar.hour;
        int minute = mCalendar.minute;
        int second = mCalendar.second;
    
        mMinutes = minute + second / 60.0f;
        mHour = hour + mMinutes / 60.0f;
    

    Also don’t forget to declare this Service and app widget in your AndroidManifest.xml file, e.g.:

        <receiver 
            android:name="TimeSettableAnalogClockAppWidget"
            android:icon="@drawable/ic_launcher"
            android:label="@string/app_name">
    
            <intent-filter>
                <action android:name="android.appwidget.action.APPWIDGET_UPDATE"/>
                <action android:name="android.appwidget.action.APPWIDGET_DISABLED"/>
            </intent-filter>
    
            <meta-data 
                android:name="android.appwidget.provider"
                android:resource="@xml/widget_provider"/>            
    
        </receiver>
        <service android:name="TimeSettableAnalogClockService"/>
    

    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 your AppWidgetProvider subclass – the on that is declare in your AndroidManifest.xml file. We’ll also need to remove PendingIntent when app widget is removed from home screen. This is done in overridden onDisabled() method. Remember to declare both actions that will call one of each methods: APPWIDGET_UPDATE and APPWIDGET_DISABLED. See excerpt from AndroidManifest.xml above.

    package com.example.anlogclocksettimeexample;
    
    import java.util.Calendar;
    
    import android.app.AlarmManager;
    import android.app.PendingIntent;
    import android.appwidget.AppWidgetManager;
    import android.appwidget.AppWidgetProvider;
    import android.content.Context;
    import android.content.Intent;
    
    public class TimeSettableAnalogClockAppWidget extends AppWidgetProvider {
    
        private PendingIntent service = null;
    
        @Override
        public void onUpdate(Context context, AppWidgetManager appWidgetManager, int[] appWidgetIds) {
    //      super.onUpdate(context, appWidgetManager, appWidgetIds);
            final AlarmManager alarmManager = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
    
            final Calendar time = Calendar.getInstance();
            time.set(Calendar.SECOND, 0);
            time.set(Calendar.MINUTE, 0);
            time.set(Calendar.HOUR, 0);
    
            final Intent intent = new Intent(context, TimeSettableAnalogClockService.class);
    
            if (service == null) {          
                service = PendingIntent.getService(context, 0, intent, PendingIntent.FLAG_CANCEL_CURRENT);
            }
    
            alarmManager.setRepeating(AlarmManager.RTC, time.getTime().getTime(), 1000 * 60 /* ms */, service);
        }
    
        @Override
        public void onDisabled(Context context) {
            final AlarmManager alarmManager = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
            alarmManager.cancel(service);
        }
    }
    

    Last line of onUpdate() method schedules a repeating event to happen every 60 seconds, with first update to happen right away.

    Drawing AnalogClock once only

    Using 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:

    alarmManager.set(AlarmManager.RTC, time.getTime().getTime(), service);
    

    Results

    Below is a screen shot with custom AnalogClock app widget that had time set only once (note difference between analog clock time and time shown in status bar):

    enter image description here

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Possible Duplicate: How to set Local Notification repeat interval to custom time interval? I
I need to set custom alarm tone in my App. Could anyone please just
A couple of firsts here - first Android app and first time using MonoDroid
I'm new to android programming. I would like to make an app widget that
I am able to set custom font to my text view like this Typeface
How to set custom header logo on your Track Wiki project page? How to
How do I set custom HTTP headers on an ESB proxy service Out Sequence?
I have a function that I set custom key and value to it and
I just can't figure it out how to set custom validator messages in Zend_Form
How can I set a custom filter in my code to ignore accents and

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.