Patrick Huy
Patrick Huy

Reputation: 995

Can't call setFormat24Hour of a TextClock in a Widget

I'm trying to dynamically update the format24Hour of my Widget's TextClock.

To do so I'm using RemoteViews like so:

RemoteViews views = new RemoteViews(context.getPackageName(), R.layout.clockwidget);
views.setString(R.id.textclock, "setFormat24Hour", formatString24.toString());
appWidgetManager.updateAppWidget(appWidgetId, views);

This however causes my Widget to show "Problem loading Widget" and logcat shows

 03-31 17:37:19.872: W/AppWidgetHostView(1408): updateAppWidget couldn't find any view, using error view
 03-31 17:37:19.872: W/AppWidgetHostView(1408): android.widget.RemoteViews$ActionException: view: android.widget.TextClock doesn't have method: setFormat24Hour(class java.lang.String)

However I'm pretty sure that android.widget.TextClock has a method setFormat24Hour(CharSequence format) which is marked as @RemotableViewMethod.

Is there anything I'm missing? Why isn't this working?

Upvotes: 0

Views: 1252

Answers (2)

Barthy
Barthy

Reputation: 3231

I know this question has been asked months ago, but your own is answer actually wrong I think.

As stated in the API guides, the only classes allowed on an appwidget are:

AnalogClock
Button
Chronometer
ImageButton
ImageView
ProgressBar
TextView
ViewFlipper
ListView
GridView
StackView
AdapterViewFlipper

Therefore, you need an alarmManager that updates the widget each second, minute or hour when the screen is on, depending on what you want.

When updating the widget you must then change the text of a textview to the actual time. You can format this time as follows:

RemoteViews views = new RemoteViews(context.getPackageName(), viewID);
Calendar c = Calendar.getInstance();
SimpleDateFormat timeFormat24 = new SimpleDateFormat("kk:mm");
String time24 = timeFormat24.format(c.getTime());
views.setTextViewText(R.id.textClock, time24);

See this link for more information on the formatting.

Upvotes: 0

Patrick Huy
Patrick Huy

Reputation: 995

Thanks to njzk2 I found the solution. It was a rather stupid problem. I need to call RemoteViews#setCharSequence instead of setString and then everything works fine!

Upvotes: 5

Related Questions