October 13th, 2011, 01:04 AM
|
#6 (permalink)
|
|
Developer
Join Date: Apr 2011
Location: York, PA
Posts: 130
Device(s): Samsung Galaxy Nexus, HTC Thunderbolt, HTC Incredible, HTC Eris
Thanks: 97
Thanked 178 Times in 71 Posts
|
Ok..so where do I start... ha.
First, you need to read this: Android Doc Link
For anyone too lazy to click and read, it basically stats which layouts and views you can use with App Widgets.
The reason your project does not work as intended is simply it is not supported by the SDK.
Solution?
You will first need to remove the DigitalClock from the layout. This WILL NOT EVER work. Replace this with an ImageView like the following:
Code:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/widget"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_gravity="center">
<AnalogClock android:id="@+id/AnalogClock"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:dial="@drawable/widgetdial"
android:hand_hour="@drawable/widgethour"
android:hand_minute="@drawable/widgetminute"/>
<ImageView
android:id="@+id/digitalclock"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
/>
</RelativeLayout>
Get system time
Code:
long epoch = System.currentTimeMillis()/1000;
time = new SimpleDateFormat("MM/dd/yyyy HH:mm:ss").format(new Date(epoch*1000));
Use the buildUpdate function to create a image based on the time
Code:
RemoteViews views = new RemoteViews(getPackageName(), R.layout.widget);
views.setImageViewBitmap(R.id.digitalclock, buildUpdate(time));
Function used above (I even attached the font for you where you can drop it right into the asset folder)
Code:
public Bitmap buildUpdate(String time)
{
Bitmap myBitmap = Bitmap.createBitmap(160, 84, Bitmap.Config.ARGB_4444);
Canvas myCanvas = new Canvas(myBitmap);
Paint paint = new Paint();
Typeface clock = Typeface.createFromAsset(this.getAssets(),"Clockopia.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;
}
Now this is NOT the complete answer. What is left is you need to create a service that will update the time. This is a little more complex then just using a AnalogClock so I will let you look up how you can create a service. Most likely you will need to use the @Override(s) onEnabled and onDisabled of the AppWidgetProvider. So when the widget starts... the service starts. Then use the @Override for onUpdate of the AppWidgetProvider which should then tricker an intent to tell the service to get an updated time and recreate the image.
I hope this helps you out and I am sorry I can not just provide a quick solution for you. Maybe some others can shed some insite or if anyone has created a service like this already could share with the community.
Good Luck!
Steve
|
|
|
Last edited by stevealbright; October 13th, 2011 at 01:06 AM.
Reason: added font attachment
|
|