Difficult to try and phrase this question, but I will try my best.
Basically, I have an application and I would like to split the code up more. To try and explain this, will give an example of one of my screens.
In my home screen, I have a title, user details, balance, next bill details and usage details. I want to split each of these sections into their own views. So what I have in my main XML file is I have 5 different RelativeLayouts, like this
<RelativeLayout android:orientation="vertical"
android:layout_below="@+id/title" android:layout_width="fill_parent"
android:layout_height="wrap_content" android:id="@+id/layout1">
</RelativeLayout>
One for each function I want to have on my home screen. And as you can see I also have each layout set below the previous layout, so the order is title, layout1, layout2, layout3, layout4. (The reason I am doing this is, that I want each layout to be interchangable, i.e. I could remove layout2, and order then be title, layout1,layout3,layout4 as I may not need the 2nd layout depending on what is required of the app)
So in my main activity class, I have called each of these layouts.
layoutTitle = (RelativeLayout) findViewById(R.id.title);
layout1 = (RelativeLayout) findViewById(R.id.layout1);
layout2 = (RelativeLayout) findViewById(R.id.layout2);
layout3 = (RelativeLayout) findViewById(R.id.layout3);
layout4 = (RelativeLayout) findViewById(R.id.layout4);
Then I use layout inflator like so
View view;
LayoutInflater inflater = (LayoutInflater) getBaseContext()
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
view = inflater.inflate(R.layout.userdata, null);
to set up the layout for each layout I have defined in my xml file.
What I want to know is, is it possible to inflate another activity rather than an XML file? The reason I ask this is, that each function I said the home screen is made up of, title, user details, balance etc, I have an activity for each of these. Each of these functions has custom functionality that I have designed, such as animation etc. 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.
If I was able to inflate another activity, then this would not be a problem, as I would set up the functionality for each function in activity, then just inflate that class.
I hope my question was clear, if not I can expand on any areas you are not sure about.
Would be very grateful for any assistance!!
EDIT: If it is not possible to inflate a class, then is there any other way method you could recommend that could solve my problem?
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 (asViewGroup). They are protected, so each of the variableslayout1,layout2andlayout3are available to all subclass activities of this superclass.And when on a specific activity, I populate each of the layouts on my
onCreatemethod with: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).