Java – How do you make custom widgets?

How do you make custom widgets?… here is a solution to the problem.

How do you make custom widgets?

When developing widgets for Android use, it seems that you cannot add your own custom View classes to the AppWidgetProvider.

For example, I created a custom class that extends the View, which works fine when used in an activity, but when I add it to the widget, I get a “ClassNotFoundException” because Android seems to only allow a specific set of system widgets to be added.

I’ve seen some apps look like they made their own custom widgets in them. For example, there is a radial menu that pops up when clicked showing the application shortcut. How are these achieved? Can I use my own custom widget? They seem to have a block canvas that can be drawn inside the widget.

Good quick example https://market.android.com/details?id=zombiesinthelab.widgets.droidpetwidget&feature=top-paid
So this widget is done by drawing ImageView and updating them regularly instead of using the Canvas drawing frame?

Solution

Android widgets can only contain Layout-Widgets supported by RemoteViews.

A RemoteViews object (and,
consequently, an App Widget) can
support the following layout classes:

FrameLayout
LinearLayout
RelativeLayout

And the following widget classes:

AnalogClock
Button
Chronometer
ImageButton
ImageView
ProgressBar
TextView

> Descendants of these classes are not
supported.

Pay attention to the last sentence. You cannot change this behavior, it is hardcoded in Android.

Related Problems and Solutions