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

  • Home
  • SEARCH
  • 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 7715497
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T02:24:02+00:00 2026-06-01T02:24:02+00:00

I am trying to create a simple android widget that shows The time but

  • 0

I am trying to create a simple android widget that shows The time but uses a custom font. Anyone knows how to do this?
I’ve tried this:
Problem showing time using custom font

But it will not work. First getAssets() will not work because it needs the context and if I remove the line that sets up the custom font after a couple of seconds I will get the failed binder transaction error.

Anyone can help or give me a link to a tutorial?
Also is it a good practice to update the time with a timer?

My code
LWidget.java

public class LWidget extends AppWidgetProvider {

//=============================================================================================
//On Update
//=============================================================================================


    @Override
    public void onUpdate(Context context, AppWidgetManager appWidgetManager,
    int[] appWidgetIds) {
        Timer timer = new Timer();
        timer.scheduleAtFixedRate(new MyTime(context, appWidgetManager), 1, 1000);



        //views.setImageViewBitmap(R.id.TimeView, buildUpdate(time));





    }
//=============================================================================================
//Timer class
//=============================================================================================

    private class MyTime extends TimerTask {
        RemoteViews remoteViews;
        AppWidgetManager appWidgetManager;
        ComponentName thisWidget;
        DateFormat timeformat = new SimpleDateFormat("kk:mm");
        public MyTime(Context context, AppWidgetManager appWidgetManager) {
        this.appWidgetManager = appWidgetManager;
        remoteViews = new RemoteViews(context.getPackageName(), R.layout.main);
        thisWidget = new ComponentName(context, LWidget.class);
    }

    @Override
    public void run() {
        Calendar ci = Calendar.getInstance();
        String time = timeformat.format(ci.getTime());
        remoteViews.setTextViewText(R.id.widget_textview,"Time = " + time);
        remoteViews.setImageViewBitmap(R.id.widget_imageview, buildUpdate(time));
        appWidgetManager.updateAppWidget(thisWidget, remoteViews);
        }
    }

//=============================================================================================
//bitmap font
//=============================================================================================
    public Bitmap buildUpdate(String time) 
{
        Bitmap myBitmap = Bitmap.createBitmap(160, 84, Bitmap.Config.ARGB_4444);
        Canvas myCanvas = new Canvas(myBitmap);
        Paint paint = new Paint();

        //if I remove the comment here, getAssets() will not work

        //Typeface clock = Typeface.createFromAsset(this.getAssets(),"fonts/Crysta.ttf");
        paint.setAntiAlias(true);
        paint.setSubpixelText(true);
       // paint.setTypeface(clock);
        paint.setStyle(Paint.Style.FILL);
        paint.setColor(Color.WHITE);
        paint.setTextSize(65);
        paint.setTextAlign(Align.CENTER);
        myCanvas.drawText(time, 80, 60, paint);
        return myBitmap;
 }




//=============================================================================================
//On receive
//=============================================================================================
@Override
    public void onReceive(Context context, Intent intent) {
            // v1.5 fix that doesn't call onDelete Action
            final String action = intent.getAction();
            if (AppWidgetManager.ACTION_APPWIDGET_DELETED.equals(action)) {
            final int appWidgetId = intent.getExtras().getInt(
            AppWidgetManager.EXTRA_APPWIDGET_ID,
            AppWidgetManager.INVALID_APPWIDGET_ID);
            if (appWidgetId != AppWidgetManager.INVALID_APPWIDGET_ID) {
                this.onDeleted(context, new int[] { appWidgetId });
            }
            } else {
                super.onReceive(context, intent);
            }
    }

}

main.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:orientation="vertical"
android:layout_gravity="center"
android:layout_height="wrap_content">

<TextView android:id="@+id/widget_textview"
android:text="@string/widget_text"
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:layout_gravity="center_horizontal|center"
android:layout_marginTop="5dip"
android:padding="10dip"
android:textColor="@android:color/black"/>

<ImageView 
   android:id="@+id/widget_imageview"
   android:layout_width="wrap_content"
   android:layout_height="wrap_content"
   />

</LinearLayout>

ltime_widget_provider.xml

<?xml version="1.0" encoding="utf-8"?>
<appwidget-provider xmlns:android="http://schemas.android.com/apk/res/android"
android:minWidth="146dip"
android:minHeight="72dip"
android:updatePeriodMillis="10000"
android:initialLayout="@layout/main"
/>
  • 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-01T02:24:04+00:00Added an answer on June 1, 2026 at 2:24 am

    You must use an AlarmManager instead of a Timer.
    You can not use a Timer because:

    AppWidgetProvider is an extension of BroadcastReceiver, your
    process is not guaranteed to keep running after the callback methods
    return.

    Use the code here for reference.

    getAssets() will not work because it needs the context

    onUpdate() provides a Context, no?

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

Sidebar

Related Questions

I am trying to create a simple Android application that has a ActivityList of
I'm trying to create a simple Android UI that consists of a EditText and
I am trying to create a simple android app in Java but I have
I'm trying to create simple custom component that draws Bitmap. It works fine when
I am trying to create a simple test app that basically extends the Android
I am trying to create a simple button example, but when I add this
I'm trying to create a simple app for android with progress bar . Everything
I'm trying to create an app for Android that simply sends a command to
Im trying to create a simple pan and zoom app using silverlight 4, but
Im trying to create a simple input box with form validation, but im not

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.