How to customize android analog clock widget through configuration screen

Posted by michJ on Stack Overflow See other posts from Stack Overflow or by michJ
Published on 2012-08-10T16:44:54Z Indexed on 2012/09/05 15:38 UTC
Read the original article Hit count: 296

Filed under:
|
|
|
|

I'm trying to develop my own analog clock widget on the home screen. It's based on the stock ICS analog clock widget, but when added to the homescreen, my widget opens a configuration screen first.

widget configuration screenshot

On this screen you can choose the color you want the clock to be (through a colorpicker dialog). You also see a preview of the clock in the chosen color on this screen. (See picture). My problem is changing the color of the widget on the home screen to the chosen color (when you hit the apply button). I have three .png files that I use for the dial, hour hand and minute hand. I color them using this code in my widgetconfig class:

    Drawable dial = getResources().getDrawable(R.drawable.clockbackground);
    dial.setColorFilter(color, PorterDuff.Mode.MULTIPLY);
    //hour
    Drawable hour = getResources().getDrawable(R.drawable.hourhand);
    hour.setColorFilter(color, PorterDuff.Mode.MULTIPLY);
    //minute
    Drawable min = getResources().getDrawable(R.drawable.minutehand);
    min.setColorFilter(color, PorterDuff.Mode.MULTIPLY);
    // clock
    AnalogClock clockpreview = (AnalogClock) findViewById(R.id.ACconfig);
    clockpreview.setBackgroundDrawable(dial);

This works fine because I can find the AnalogClock since it's in the layout xml file of the configuration screen, which I set in the OnCreate() through setContentView(). The problem is that for the clock widget on my screen I have to use RemoteViews. So I tried setting the new background of the clock widget through RemoteViews. But RemoteViews seems too limited to do this. I need something like setTextViewText() but then for the background of my analog clock widget, like for example:

    RemoteViews views = new RemoteViews(context.getPackageName(), R.layout.clockwidget);
    views.setAnalogClockBackgroundDrawable(dial);

The clock widget background seems sort of unchangeable. How can I recolor my clockwidget on homescreen in widgetconfig class? There must be a way because it seems so easy to do at first...

My project consists of the widget config class and clockwidget class, widgetconfig.xml, clockwidget.xml, and widget_info.xml (for appwidgetprovider).

© Stack Overflow or respective owner

Related posts about java

Related posts about android