extending Android’s TimePicker with seconds

My current quest: the TimePicker provided with Android doesn’t have a field for seconds. Boo. So I want to add my own. TimePicker internally uses a widget called NumberPicker, so I should be able to just another of those at the end for seconds. But it’s an internal widget, and we are strongly cautioned by the Android devs not to use it directly.

So although it would be bad practice to use internals directly, I can clone the source and implement it myself, right? In theory. In practice, I’m feeling a major WTF coming on…

Incidentally, those source bundles I provided in my “see the source” article? Don’t include much from com.android.internal. I don’t know why; just because it’s internal doesn’t mean we won’t debug into it. I’ll see about including those (will have to modify the script for gathering source).

NumberPicker is using resources from com.android.internal.R; fine, those can be cloned too (grumble grumble). And it’s using another internal widget, NumberPickerButton, which can also be cloned (grumble).

Also, it’s accessing mContext from its parent class, View. Why is eclipse claiming problems with this? The member in question is a protected field, so it ought to be visible in subclasses. WTF… perhaps this has to do with being marked “hide” so the SDK doesn’t know about it? But it should work anyway… hmm. Well, getContext() should accomplish the same thing, so I’ll just do that.

Track down all references to internal and point to my own clones. And… it works! Well, that’s something.

Of course, what I probably really want to do is subclass TimePicker into my own widget and add the NumberPicker there. If that’s possible. Because then I could leave the AM/PM button in place.

I think this is hard enough that it warrants a feature request. Including seconds or not oughta be a simple option on the TimePicker…

Advertisements

4 Responses

  1. Hi Luke,
    Great blog. I used your post about getting the source and got that installed. Thanks!

    I’m trying to extend the timepicker to have seconds as well, but not having the same luck you are having. (a bit overwhelmed, new to Java, Eclipse and Android….). Would you be willing to share some more details or perhaps even some code?

    Thanks

    Tom

  2. I am trying to do the same thing and I found the source for NumberPicker and NumberPickerButton, but not android.internal.R. I am looking here:
    https://android.git.kernel.org/?p=platform/frameworks/base.git;a=tree;f=core/java/com/android/internal;h=82be9b9021f3f393bd0aa691ef177b515df6632a;hb=HEAD

    Is there somewhere else I should be looking?

    Thanks.

  3. Sorry I have been a bad neglectful blogger!

    Here’s the code I ganked in my program – have a look, it hooks into various resources too.

    http://github.com/sosiouxme/WhenDidI/tree/master/src/net/sosiouxme/WhenDidI/custom

    Sorry I only have a second to drop in! Let me know if that doesn’t point you right.

  4. Mr. Neglectful Blogger,

    Link is throwing 400 request. Can you please update us with new link.

    Though very nice and informative post helped me a lot to understand the internal working of widgets. As, I followed every URL you mentioned here and even got to know how by the means of reflections the guy was modifying the widget.

    Thanks Again and keep up the good work.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: