Create widget programmatically

hundeva picture hundeva · Oct 1, 2013 · Viewed 8k times · Source

As far as I understand, android widgets need RemoteViews, instead of View, like activities.

My question: is there any way to completely avoid xml, and build the whole layout with java code?

Alternative question: can I somehow retrieve a View, like I would use findViewById(int)? For example: LinearLayout linearLayout=(LinearLayout)findViewById(R.id.xxx); This way I could access the base layout, and manipulate however I want to.

Thanks!

Answer

CommonsWare picture CommonsWare · Oct 1, 2013

is there any way to completely avoid xml, and build the whole layout with java code?

No, sorry. RemoteViews relies heavily on layout XML resources. While there is an addView() method, it turns around and adds another RemoteViews, and that puts you right back where you started from.

can I somehow retrieve a View, like I would use findViewById(int)?

No, because there is no View in your process. You configure the widgets via the various setter methods on RemoteViews.