I'm looking at adding a listview to my Android application with a thumbnail image. The thumbnail on the left and text on the right.
Is there any way of populating the list view with seperate xml files, for example, could I have separate xmls for Austria, UK, USA etc and add them to the listview?
The reason I ask is, I plan to reuse the individual countries multiple times, as in, shown in an A to Z but then again by Continent etc.
If this is possible, could I get an example of how this is done or a link with a tutorial etc?
Thanks
Do you really want separate .xml files? Or do you want to reuse one .xml file and just replace its content?
If you want separate .xml files (meaning multiple different Views in your Adapter)
take a look at the Adapters methods:
getViewTypeCount() - returns how many different view layouts you have
getItemViewType(int pos) - returns correct view type id by position
With these methods you can specify the number of different types in your adapter, and load different layout files depending on the type.
You could for example have an Adapter like this supporting multiple layouts. (e.g. one item being an actual item, the other one being a separator between the items)
For more specific information, please take a look at this great tutorial:
http://android.amberfog.com/?p=296
Related
I am really confused, which among GridView, Tableview or multiple listview to be used to develop a UI as shown in below image.
I don't know how much list data (categories and its sub categories) is in web. It is dynamically added from wed, and sent to my android app.
Please suggest me, what view is best for developing above UI.
Note: I want to make it programmatically.
Thank you in advance for your co-operation.
Keep in mind that the image you have shown as an example is quite large and such an implementation would make for a cluttered UI on smaller screen sizes. That being said, the example you provided could be achieved in Android by using a GridView. Each major category (Automotive, Jobs, etc.) would be a grid item, with the sub element implemented as either a TextView or even a ListView within the grid item. Something like this:
As both #PedroHawk and #bryan mentioned, you could also use an ExpandableListView; in this scenario, the major categories (Automotive, Jobs, etc.) would be header items and the sub elements would be children of the header. Something like this:
However, with this implementation you would be limited to a one-dimensional list - that is you would not be able to have major categories next to each other as shown in your example.
With both of these implementations (ExpandableListView and GridView) you can dynamically add more elements as you receive data.
Like PedroHawk said, ExpandableListView is an option. But also consider because of the screen size, you may not want to try and port the UI directly over. What about a single ListView with just the main categories and the user can drill down?
I'm new to android and don't know how to make a ListView with more than 1 layout. I need 5 different layouts in the same ListView. The list won't have more than 5 items each with a different layout.
Also will there be a problem if one layout is a VideoView?
I don't have any code to post, sins I didn't know where to start.
Thanks in advance!
You need to override the getViewTypeCount() and getItemViewType() in your adapter.
The getViewTypeCount() would return 5 and getItemViewType() would determine what type of view (out of the 5) is to be displayed.
If you are using Eclipse, go to the relevant xml layout in the /res/layout folder (for MainActivity.java it might be activity_main.xml). From here you can add views/layouts inside your ListView by dragging them onto the ListView in the 'stucture' window to the right of your screen. They will be in the order you place them, top to bottom.You can get your viewslayouts from the widgets panel or add them programatically in Java (do this in the corresponding Java file).
Alternatively, you can write your views/layouts in using xml, make sure they are between the ListView tags so it contains them.
I need to implement listview with multiple headers and list item under each header are different.
For example the first header name is weather, under this header, each list item has city name and current temprature.
The second header name is contact, and the each item under this header contains person name, contact number, call icon, message icon etc.
Can anybody know how to implement this in android?
Thanks
Mindus
In order to achieve Multiple header and different layout in a ListView you should use Section ListView
Section is like Header
And you can inflate different layout. Smiler example is given here Link
For Complete Source code go through the below link.
http://amitandroid.blogspot.in/2013/05/android-section-listview.html
As far as I know ListView can have multiple headers, but you can't locate them at the postion you want.The all will be located at the top http://developer.android.com/reference/android/widget/HeaderViewListAdapter.html
I advice you to use separators.Also this can help you https://github.com/commonsguy/cwac-merge
With this MergeAdapter you can insert adapters and views in whatever order you want and then present them all as a single adapter to a listview and consequently you can achieve multiple headers simulataion.
you will have to make different layouts for each type of cell (separate for weather and separate for contacts) , you will have to override getView method of list view as well and upon need, just set your desired view for cell to update it.
Thanks guys, Finally i got it with two different listview with separate linearlayouts. And use textview as a header in each linear layout.
I would suggest not using a straight linear layout for entire sections.
For headers and lists, I would recommend a MergedAdapter, https://github.com/commonsguy/cwac-merge, either that or roll your own.
For multiple cell layout types, this is support in the listviewadapter with the following methods from BaseAdapter
getItemViewType(int position)
getViewTypeCount()
This allows you to specify how many different types of layout are in use and then recycle the layouts appropriately.
I have created a custom adapter to display a list, there is an image that is displayed in each row ( the image is the same for all rows, except using an array i am assigning it different values depending on the position). An xml file defines the relative layout that i am using. My problem is that i can either get the entire row to be clickable or nothing at all, I only want this image to be clickable, instead of the entire row. How would i be able to do this ? i am new to android and am pretty much following different tutorials trying to create my list. Any help would be appreciated.
layout is like this :
TEXT:
[Image]
TEXT:
thats wat a row looks like...getting two texts from two different arrays and shows it, a third array is used to link to the image. I just want this image to be clickable instead of the entire row.
Thanks
Android's list component manages clicks per row. This makes it very difficult to achieve what you want to do. Two solutions come into mind:
1) If your list is never very long you could simply use linear layout and scroll view to build the list. This approach won't work if you fill in the list dynamically and you can't be sure that there won't be a very large number of rows as it would use too much memory in that case.
2) Other option is to use ListView but make your text components and images different view types in list ie. break you row into three.
That can be achieved overriding list adapter's getItemViewType(int)
http://developer.android.com/reference/android/widget/Adapter.html#getItemViewType(int)
In this approach you can make the image rows clickable but the text rows not.
I'd like to use different row descriptors (from the XML layout resource) in one ListView. Can I and how?
Also, I'd like to be able to programmatically change the size of the rows, based on the data in each row, in a ListView. Can I and how?
Thank you in advance--I LOVE stackoverflow.
M
I'd like to use different row
descriptors (from the XML layout
resource) in one ListView. Can I and
how?
Step #1: Override your Adapter class and implement newView()/bindView() (or getView() if ArrayAdapter)
Step #2: Inflate the rows you want when you want them from the layouts you want
Step #3: Override getViewTypeCount() and getItemViewType(), returning appropriate values, so Android knows to use different object pools for each distinct type of row
This book excerpt covers #1 and #2, though for only one type of row. I don't have any samples handy for multiple types of rows, sorry.
Also, I'd like to be able to
programmatically change the size of
the rows, based on the data in each
row, in a ListView. Can I and how?
Put in bigger stuff in the row. Heights of rows are determined by their contents.