i have a complex view which contains 4 list views arranged as per the requirement. i have been been able to implement and get it working. but this is a sole activity and data needs to supplied internally (within the activity).
i want to define a way where in, this complex view is like a reusable component which is called by other activities that provide data for all 4 list views and then the view shows up in the screen.
could somebody please guide me as to how do i go about achieving this functionality.
You should define your listview structure in a layout file of its own. Then you can use whats called inflation, which lets you "inject" seperate layout files into your main layout in run-time. Take a look at:
http://developer.android.com/reference/android/view/LayoutInflater.html
Take note at the introductory notes. Android is already inflating an XML resource, namely the layout file you´ve defined in setContentView(), you can grab the current instance of the inflater and use it as you see fit, saving greatly on memory as opposed to instantiating it yourself.
Related
I am new to android programming am trying to learn it.
So basically there are two ways of creating whatever is going to be visible on the screen :-
We create views and view group objects inside layout.xml files. And then as the need be we access those views that are already existing
in the layout through our java programs by accessing their ids ( as
r.java….). So basically when we start a particular activity, we
set the content to be displayed corresponding to that activity using
setContentView method, to which we pass the layout.xml file, inside
which we have defined the different views and view groups to be
displayed on the screen.
The second way is we create these views dynamically though our java programs and then set them as the content on the screen using
setContentView again.
Now the above is my basic understanding. Please let me know if the above needs correction.
Now what I want to understand here is :-
Is there a way that using the first method itself, we can do the
vice-versa, as in say instead of fetching the the views through their
ids from the layout.xml files, can we already have a predefined
layout.xml file with different views having ids, and now through our
java programs can we just access those views though their ids and set
their values, something like say (in javascript) :-
document.getElementById(“someTextBoxId”).value= “some calculated
value from java code here”
Thanks.
I'm quite new to Android so go easy on me!
I'm building an app that catalogs a bunch of restaurants and displays each of their menus, depending on the selection of the user.
Is it possible to store the info of each restaurant in a separate resource file, and have a template-like activity that will display the particular info that the user selects from a spinner?
Yes, it is possible. You can replace views (which can be loaded from different layout.xml files). You can load a layout XML file using LayoutInflater. You can add views into a layout (which is a ViewGroup) using one of the addView() methods, and there are also methods to remove views.
It is unusual, however, to hardcode data into any layout. It is usually better to store the data in a database or file, and use a layout which is more like a template, where you replace the data in the layout at runtime.
I have a listview where the items contain different amount of child textviews.
min 5
max 20
i've tried both ways and I only have the vm to test my apps on so I can't really tell any difference performance wise.
but what's the best way to do this?
should I create the 20 textviews in my xml and just hide the ones I don't use?
or just create and add new textviews everytime, resulting in no "Ghost views"
Issues to realise:
Code cleanliness-> Doing anything more than basic layouts in code can get very messy, very fast.
Code re-use-> It’s extremely easy to inflate an XML layout into a specified view
with one or two lines of code
code Performance-> Creating the objects necessary for an in-code layout leads to unnecessary garbage collection. As per the Android Designing for Performance article, “avoid creating short-term temporary objects if you can.”
Attribute availability-> Defining Views in an XML layout exposes attributes that are not always available by object methods.
Possible disadvantages:
It make take more time to do an XML layout versus defining the layout in code, especially if there are only one or two interface elements that need to be changed.
After thinking about what I want to accomplish, it makes sense for me to use XML layouts for the dynamic view changes I need. It would be more than just a few lines of code to do my layout changes without utilizing XML layouts.
Now you can decide according to your requirements.
I see this question here, and it makes me wonder if what I'm asking isn't really possible:
How to share a view across various activities
Basically, I have a common footer view that I'm inflating (including) in all of my views. However, it uses the same repetitive code to do that. My thought was to create a parent activity class to do this, but it doesn't seem correct to have one activity render the view of another. So should I just create a utility class of some sort, or is there a better way?
You can include other layout XML files directly in another layout file. So whenever you set content to a layout file, along comes your footer for the ride.
If your footer needs code to drive it, just create a custom class for it along with the layout file. Then perhaps during instantiation you can drive the code that needs to execute.
This is a blog of how to do it.
include is very useful while reusing View components.
But remember, if any problem occurs while using include tag, wrap the included view by an arbitrary layout.
If I understand your question, another way of having multiple Activities use the same View instance is by doing something like creating your own Application class (it's seriously easy).
MyApplication extends Application ...
#Override
public void onCreate(), onConfigurationChanged(), onLowMemory(), onTerminate(), getIstance().
As there is only a single intance of "Application" that is statically available it makes it a good place to store and share various objects that need to be passed around.
I'm reading the book 'Hello, Android'. In the Sudoku example, it uses a options menu. It needs a MenuInflater that we use to read the menu definition from XML and turns it into a real view.
To use button, textview, or many other views, I don't need to inflate them.
My question is, in what situations, I need inflaters? Why doesn't Android treat menus like other views?
You need an inflater at every place that you want to dynamically create a view out of an XML file.
Activity layouts are automatically inflated when you call setContentView() as they're always required.
But when the menu is required — which is only when the user first presses the Menu button — the XML-defined layout needs to be manually inflated.
Similarly, if you have a ListView, you don't know in advance what rows will exist, so we have to inflate a View from XML for each row in the list, as they're required.
Inflaters are mainly used for parsing Xml layout into view objects. As mentioned above the inflation is needed for making a link between the UI defined in the Xml for manipulating and making developer.
Whenever UI Updation is needed we need inflation and UI Updation is done through view object and developer can dynamically create view and add to existing view.
Hence inflation helps developer to change behaviour of UI in xml layout according to specified condition in a program.
With inflation, we are able define controllers in MVC for each xml layout where xml is view.
Menu is also a view it has to inflated In certain code such setContentView(specifiedLayout) includes inflation
But in earlier version it was not like this it was like setContextView(getInflater().inflate(specifiedLayout))
for ease of programming,android developers have incorporated inflation in setContentview() and there are lot scenarios like add view to layout addView(),etc..In most cases inflation has incorporated in code that why most of beginner does know inflation concept and has difficulties in understanding inflation in android.