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.
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.
The Android documentation implies that I should prefer to use XML when declaring the UI: http://developer.android.com/guide/topics/ui/declaring-layout.html
Regardless of whether you agree that it implies this, my personal preference is to use XML when possible.
However, for ListView the documentation only explains how to populate it programmatically: http://developer.android.com/guide/topics/ui/layout/listview.html
How do I, say, create a ListView with three elements using only the XML layout file? Is this impossible or undesirable?
Being used to developing for the Desktop, I am not accustomed to being required to manually create elements of UI containers, so I am confused as to whether I am missing something or things are different in Android.
ListView and similar Views will, in general, receive data coming from a dynamic source (like a SQLite database). In that case, the only way to populate it is programmatically.
However, if you have a static list of elements, you can populate it via XML with the following attribute: android:entries. The value would be an array resource.
Example:
<ListView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:entries="#array/your_array"
/>
ListView uses ListAdapter which provide a View for each Item in ListView. The template for this item view you can very well define as another XML layout. As far as data is concerned, you can place it in an XML file in res/values folder as a String Array etc. Or even parse from some other file.
A totally hardcoded layout will that be of a LinearLayout placed inside a ScrollView. Then you can add many child elements to LinearLayout.
i got the following "problem".
I want to have an activity thats shows me the standings of some teams at a specific gameday.
therefor i would add a spinner and a TableLayout. At the first Start the activity should show the standings of the actual gameday but then you can choose any other gaymeday and the standing should get updated.
Whats the best way to create this activity?
assemble the whole TableLayout with all TableRows and TextViews, give them ids and update those views via id during runtime. Problem: huge unflexible hardcoded layout.xml
assemble the layout during runtime, add ids, update via ids
assemble the layout during runtime. on update remove old views and create new ones
assemble the layout during runtime. on update restart the activity
just whant to know which one is the best. or is there any other way to achieve that
thx Cheetah
If I were you, I'd actually use a GridView with an Adapter. This will abstract away all the handling of layout changes. You just have to worry about mapping your data to appropriate views. This example maps ImageViews to a GridView, but there's no reason you couldn't map to TextViews containing your data in a GridView. Also, because you're using an adapter, you can take advantage of all the Loader classes and they're asynchronous loading capabilities.
In addition, using the approach will allow you program to easily adapt as your dataset changes. You may want to add more data to the table in the future and this approach will allow you to easily do that without having to constantly change your xml layouts.
Does the number of views change? If no. Best way is to use the already existent views and update their values. Try to avoid recreating/reinflating views since that's an expensive task.
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.
Is it possible to load a layout XML at runtime and load into activity?
In my app, I have various types of data like Person, Company, City, etc; The requirement is to dynamically load the layout, find views by tags (property names like Person.name, Person.address) and then fill in data. For example, if user has selected an object of type Company, we want to load a company.xml layout, inflate it and then associate various properties (company name, company slogan, city, address, revenue) to tagged views. One possibility I see here is - each view in the layout will be associated with property-name as tag and then appropriate data will be loaded in appropriate views.
What should be the best design you would recommend?
Is it possible to load a layout XML at runtime and load into activity?
Not using standard Android layout XML files. Those have to be precompiled into your APK. The API does not support inflating layouts from anything but layout resources.
You could create your own interpreter of Android layout XML files, or roll something else similar.