Android Inflating Views
Solution 1:
I do exactly the same as you.
I use composition in my Java activities to achieve the sharing of functionalities across my activities. I think of activities as "context", and not as parts of my page that get replicated. So when I am in a certain context (an activity), I then display layout1, layout2 and layout3 with specific parameters, and specific contents that depends on that activity. On another activity, layout1, 2 or 3 could be different, but they have the same location on my screen all the time.
I use composition through views: all my activities have a superclass (call it anything you want, something like ActivityWithCustomLayout
, which contains all my layout as class members (as ViewGroup
). They are protected, so each of the variables layout1
, layout2
and layout3
are available to all subclass activities of this superclass.
And when on a specific activity, I populate each of the layouts on my onCreate
method with:
layout1.addView(...something inflated from an XML that depends on that specific activity...);
layout2.addView(...same principle...);
So in fact all my XML layouts are "parts" of activities, which I inflate at runtime into views, that I add dynamically to my activity when needed.
You're right, these explanations are not easy :)
If you target Android >= Honeycomb (including ICS) then have a look at the Fragment framework, it may be a simpler way to achieve all of that (haven't had a look at that yet).
Solution 2:
Not too much of an answer but I often find myself subclassing the layouts themselves. Then, in the onFinishInlate()
callback, I wire up all of my view references using findByViewId
. In your XML, you can replace <RelativeLayout>
with <com.company.CustomLayout>
, assuming that your CustomLayout
is a subclass of RelativeLayout
.
As mentioned, you can then pull them out into separate files and include them with the include
tag. This makes refactoring easy and allows you to reuse the layout components.
Or, if you inflate these subclassed layouts from within your code, you don't have to worry about all the messy findViewById
calls in your activity.
Fragments are also great and have similar life cycles to an activity. Good luck!
Solution 3:
I'm not sure that you need to inflate all of those Views from within your Activity. Look into using the include
and merge
tags within your Layout. This should help get you started: http://developer.android.com/resources/articles/layout-tricks-merge.html
You can also toggle the visibility of your Layouts. So, you can declare them in XML, initialize them as visibility="false"
, and then toggle that visibility in your Java code.
You also stated: "If I simply inflate these xml files, then I can't access the widgets inside so I can't set up the functionality from Java which is what I need."
You can access any Layout component after it has been added to your Activity using findViewById
and casting the object it returns to the appropriate type.
Post a Comment for "Android Inflating Views"