I am programmically creating a view at runtime and I want to this view to be added to my linearlayout dynamically at runtime. Here is the code I got:
public class Widget extends AppWidgetProvider {
@Override
public void onUpdate(Context context, AppWidgetManager appWidgetManager,
int[] appWidgetIds) {
RemoteViews updateViews = new RemoteViews(context.getPackageName(),
R.layout.main);
DemoView dv = new DemoView(context);
// Stuck here...How do I add my new dv View to my android widget
appWidgetManager.updateAppWidget(appWidgetIds, updateViews);
super.onUpdate(context, appWidgetManager, appWidgetIds);
}
private class DemoView extends View {
public DemoView(Context context) {
super(context);
}
@Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
Paint LedColor = new Paint();
Paint BlankColor = new Paint();
Path p,p1;
Matrix m = new Matrix();
LedColor.setStyle(Paint.Style.FILL);
LedColor.setColor(0xffffffff);
BlankColor.setStyle(Paint.Style.FILL);
BlankColor.setColor(0xff111111);
DigitPaths dp = new DigitPaths();
p = dp.GetDigitPath(-1);
p1 = dp.GetDigitPath(5);
p.offset(50, 50);
p1.offset(50, 50);
m.setScale(6.5f, 6.5f);
p.transform(m);
p1.transform(m);
canvas.drawPath(p,BlankColor);
canvas.drawPath(p1, LedColor);
}
}
}
Any help would be appreciated!
It’s not possible to add a custom
Viewto an app widget. See the “Creating the App Widget Layout” section of the App Widgets Dev Guide for whatViewtypes are allowed.Android 3.0 adds support for some views to display collections. See the “Using App Widgets with Collections” section for details.
Otherwise, to dynamically add an allowed
Viewto an App Widget, after inflating theRemoteViewsand getting a reference to it, you can use itsaddView(View)method, or theaddView(View)method on any of theViewobjects already in theRemoteViews.