2

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!

3
  • You can add your custom view using remoteViews.addView(viewId, nestedView) Commented Oct 1, 2013 at 12:27
  • I was looking at that, probably that's what I will have to work with.. Commented Oct 1, 2013 at 12:37
  • 1
    It will help u.developer.android.com/reference/android/widget/… Commented Oct 1, 2013 at 12:44

2 Answers 2

6

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.

Sign up to request clarification or add additional context in comments.

4 Comments

so if I understand it correctly, the RemoteViews is kind of a "replacement" of View in AppWidget context? I see that I can use setTextViewText(int, CharSequence) to set the displayed text, but can I retrieve it, like getTextViewText()? Unfortunately, there is no such method. Sorry if the questions are trivial or lame, I just started to learn it a few hours ago. :)
@hundeva: "the RemoteViews is kind of a "replacement" of View in AppWidget context?" -- it is a serialized set of commands for another process to define Views for the app widget. "but can I retrieve it, like getTextViewText()?" -- no. The app widget API is write-only.
@CommonsWare Any idea how this app manage to do the impossible?
@Pedram: Presumably, they are creating and showing bitmaps, or composing an app widget from a series of smaller layouts using addViews(). Either of those would seem to fit what that app offers, and I would bet they're going with the bitmap route, given their described feature set. If you have additional concerns in this area, ask a separate Stack Overflow question.
0

You can take Linear Layout pragmatically like
LinearLayout mLinearLayout = new LinearLayout(this);

also you can add its chilld using

Button mButton = new Button(this);   

mLinearLayout.addView(mButton);

setContentView(mLinearLayout);

1 Comment

this is true for any Activity, however, you can't do this when working with AppWidgetProvider

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.