• After 15+ years, we've made a big change: Android Forums is now Early Bird Club. Learn more here.

Apps Newbie help with Clocks in Eclipse, please read!

Hi there, I am in desperate need of some seasoned help. I started working on this about a month ago and have hit a brick wall. I have been following the tutorial at Tutorial – Creating a Custom Analog Clock Widget and have turned it into a pretty nice analog clock, but now I want to add a box inside the design that has a digital readout of the time and date, but everytime I try to in the layout and the test the app, it gives me an error. I know it is probably something simple, but I am not to good at this kind of stuff. Please, if anybody can help, I would really appreciate it. I have been reading through these threads and I have seen that many newbie questions go unread and especially unanswered and I am sort of hoping that somebody can take a few minutes to help me out. Who knows, maybe it can help somebody else out in the future. ;)

I am enclosing a zip file of my workspace folder from eclipse. If you want to take a look at my files and then let me know what I need to do to add this I would be so appreciative. Thanks again.
 

Attachments

Please does anybody have any suggestions?? ANYTHING at all would be so greatly appreciated for a really embarrassed and frustrated newbie, lol. Thanks again.
 
Could you post the code snippet of what your having issues with? Also... post what errors you are getting the console. If you provide this we can help you a little better. Thanks!
 
Ok, I am so very thankful to you for offering to help. Here is what I am doing. I am taking the files that you have already seen and loading them into Eclipse. Then I opened the res/layout/widget.xml file. I then loaded up the "Graphical Layout" view and then dragged the "DigitalClock" option on top of my background.

Enclosed is a screenshot of the graphical layout view

The snippit looks like this:
Code:
<?xmlversion="1.0"encoding="utf-8"?>
<RelativeLayoutxmlns:android="http://schemas.android.com/apk/res/android"android:id="@+id/Widget"android:layout_width="fill_parent"android:layout_height="fill_parent"android:gravity="center"> 
<AnalogClockandroid: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"/>

<DigitalClockandroid:layout_width="wrap_content"android:layout_height="wrap_content"android:id="@+id/digitalClock1"android:text="DigitalClock"android:layout_alignBottom="@+id/AnalogClock"android:layout_centerHorizontal="true"android:layout_marginBottom="101dp"></DigitalClock>
</RelativeLayout>

Then when I try to test my app it shows a grayed box wherever I put the widget on the homescreen and it says "Problem loading widget." If I go back into Eclipse and remove the DigitalClock part and then retest the widget it loads fine.

:confused::confused:

Once again, thank you for taking the time to help me. I can't say thank you enough.

The only other issue I am having is, it is supposedly able to launch the alarmclock features when you click on the widget on the homescreen. It works on version 1.6 but if I test the widget on 2.3 or something like that it doesn't do anything when you press the clock??

Thanks again for everything.
 

Attachments

  • screen1.jpg
    screen1.jpg
    67.1 KB · Views: 124
I will tell you what I don't have much but I am so desperate if somebody can help me and use my files and then get me the eclipse project files with this working I will pay you. I need the working very quickly and it seems like there is a shortness of people offering advice. Please this is so very important. All I need is to make it so that I have "digital" readout of the time on top of my analog clock and maybe the same thing with the date and then also when you touch the clock the alarm program opens up where it works with all versions of the android software. Right now it ONLY works if I test it on version 1.6 but nothing later.

Please I am SO VERY desperate for help. Please do the right thing and offer a helping hand. Its like that Pay It Forward. ;)
 
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
 

Attachments

Back
Top Bottom