I'm looking to create a music app and I'd like to make a GridView similar to what Google Play uses where they inject elements that will span rows and columns like the Soilwork album does in this screenshot:
I've thought about using a ListView and populating rows with custom elements, but I couldn't think of a good way to use that with ViewHolder pattern, or really a way to make that reusable and account for differing number of items in width based on screen size (for example, the grid is only two items wide on phones, and "large" items span both columns and only one row).
I've also thought about using a ScrollView filled with custom ViewGroups, but that seems to run into the same issues I previously mentioned.
I next thought about using GridLayout, but that doesn't accept ListAdapters, and doesn't seem tuned to the kind of usage I'm looking at (nor does it seem to scroll)
I'd greatly appreciate if anyone could give me somewhere to start on this, or could point me to a library that does this. I've already checked out StaggeredGridView but it doesn't seem to accomplish what I'd like. My ideal solution would be a view which lays out items on an even grid like GridView and accepts view from a ListAdapter, also like GridView, but allow for elements to span, using the single cell constraint of GridView as the default behaviour.
Cheers.
EDIT
I have a perfectly functioning GridView as shown below, but I'd like to make items at regular intervals (every nth item) span more than one column and/or row, as shown in the previous screenshot.
Check out Parchment. GridDefinitionView may help you achieve the UI you are looking to build.
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?
Things are simple, I want to make a widget with 4 lines and 4 rows in it, on each cell there would be a click-able image and an action set by the user via the Settings page.
The layout is like this:
What layout element is recommended to be used for this scenario ? Should I use a GridView, TableLayout, more Linearlayouts ? Keep in mind that the spacing between items must be the same. I want to make it as light as possible. So, what layout ?
If I decide to use a GridView do you have any simple tutorial about
this? I can't manage to find a way of accessing the GridView from
AppWidgetProvider and set it's Adapter. Thank you.
LE: It seems that GridView is supported starting from Android 3.0.. please correct me if I'm wrong. In this case the only remaining thing to do is add 16 images and for each image add a onClickListener ? Brrr...
If you use a GridView then half of your work is done for you - The only layout and formatting elements you need to consider are on a Global (GridView) and Item level.
Using a GridView will also give your Scrolling functionality and the ability to change your row/columns count based on your device (4x4 on tablet, perhaps 2x8 on a phone).
Creating an extension of BaseAdapter to attach the Grid's children will also give you the flexibility to check items, multi select and will allow you to quickly modify the implementation in future by adding and removing items at will.
If this is simply a 4x4 grid which will always ALWAYS remain the same independent of device and each "Item" will always be the same, Use a RelativeLayout as it will be the most lightweight and efficient ViewGroup.
I am working on a booking engine android app like an airline booking system. To fetch the content of say all the available airlines specific to a passenger search, this is then displayed on the mobile's screen.
Which one, a table layout or a grid layout, will be effective considering screen loading time, system memory consumption, and additional features?
**EDIT: This line was correct when this answer was written, but no longer applies to 99.9%+ of all Android devices: There is no GridLayout in the Android API. **
(Note: As of API level 14, there finally is GridLayout; see the answers below. In addition, the V7 support library adds GridLayout support down to API 7. However, this answer's description of GridView is still accurate and very well stated.)
If you mean GridView, TableLayout and GridView are completely different things.
A GridView is basically like a ListView but whose items are arranged in a strict grid. It is attached to an Adapter, and retrieves views from the Adapter has the user scrolls through it. All elements in the grid must be the same size. The user can move a visible selector through each item -- the goal of a GridLayout is to display the data from an Adapter and let the user navigate and select each of the displayed items. The only difference from a ListView is that the items are put in a grid instead of in a vertical list.
TableLayout is just a layout manager, somewhat like a table in HTML. It does not itself do any scrolling; to have something that scrolls you must put the TableLayout in a ScrollView. This implies that all of the data you are displaying must be populated into the TableLayout up-front, so the ScrollView knows the total space it is to scroll in. It also does not directly give you per-"item" selection or interaction, because a TableLayout doesn't have items, it is just a layout manager.
You didn't actually give near enough useful information about what you are actually trying to do for anyone to recommend what to use. It depends a lot on what specifically you want.
I mean what will be useful in terms of "additional features"?!? Well what features are you looking for!
Anyway as a general rule, an Adapter-based view should be used for any situation where you have a significant amount of data that the user is scrolling view; these are a lot more efficient than having to create the entire view hierarchy up-front to display your data. They are also the only ones that automatically provide per-item selection and other such features. The primary view for this that applications use is ListView, though GridView can also be used.
Since android 4.0, there is such a thing as a GridLayout. GridLayout is always preferable to TableLayout. It provides all that you already have on TableLayout, and can replace other layouts too.
It seems quite cool, and it seems that Google wish it to be as popular as the LinearLayout (according to their videos of Android 4.0).
EDIT: if you have to show a lot of items, consider using RecyclerView with GridLayoutManager. This can help in terms of memory and CPU usage.
TableLayout configurations are normally straightforward to
accommodate, as GridLayout supports both row and column spanning.
TableRows can be removed, as they are not required by GridLayout. For
the same UI, a GridLayout will generally be faster and take less
memory than than a TableLayout.
TableLayout is supported on all android versions , while GridLayout requires level 11 (Android ICS 4.0 ) or higher , but it can be easily added through support librarry v7 to support level 7( Android 2.1) or higher
This is a nice presentation of GridLayout which also outlines the differences in comparison to TableLayout:
http://blog.stylingandroid.com/archives/669
One of the most important differences however is that it is only available in ICS (Ice Cream Sandwich) and newer. This currently means less than 30% of market share so for most developers the answer would be: wait a few years before you use GridLayout. YMMV of course.
I think GridView should work better because as it is implemented with view recycling and stuffs as inherited from AbsListView. GridView is more difficult to deploy because you have to use with Adapter but it will work effciently if you have a lot of heavy views to load such as images
In grid layouts, the components can automatically set to the view by taking numcolumns="autofit". Here we don't have need to set how much rows & columns we required, but in table layout, there we haven't such kind of option & we have to set how many rows & columns we required.
In table layout, we can't insert more than 1 item in a row without using relative layout
I think a TableLayout would be more easy to use. For a GridLayout you need to build custom adapters and so one which result in a more complicated application.
Looking at different sceensizes, a GridLayout will choose a good number of columns and rows by itself according to the content and it will be more flexible with adding items.
Also the GridLayout will be more flexible but it is some more work to make.
I'm using a ListView to show a list of items. These items are in a table format with columns and rows. Is there a table like adapter to make sure all the columns and rows line up? I know this brings in the complexity of how large each column should be, what to do with cut off text, and other things. I'm just curious if there is currently and adapter hiding somewhere for this task. Or maybe even another control?
The point of using ListView is to be able to scale to larger data sets by not having to create and layout views for all of the items up-front. Because of this, your request fundamentally conflicts with how ListView works -- ListView simply doesn't know how all of its items will layout, so there is no way for it to automatically make sure they align in some way.
You can ensure they align yourself just by writing the item layout appropriately. For example, very often in the UI you will have an icon followed by a label. If you ensure the icon is a specific size, then all of the list items will align. If you are dealing with elements that are more dynamic like text, you could do the same thing by enforcing up-front a specific width for those elements.
If you really want to have the UI compute the element sizes dynamically and align all of the rows based on them, that is what TableLayout does. It can do this because it always has all elements there to layout together. If you want to allow scrolling in it, you can wrap that in a ScrollView like another poster suggested. Just be aware that this approach will quickly fall apart as your number of rows increases significantly.
I was able to make TableLayout to behave like ListView (at least visually). Here is my answer.
There is GridView for that, but afaik it doesn't work with columns and rows. Luckily you seem to have been expecting some complexity :)
You can use a ListView or a ListFragment and populate items using each time a single TableRow inside a TableLayout (maybe using android:stretchColumns="0")
you'll have a TableLayout per line, so it's probably inefficient but it does what you are trying to do
I'm trying to tackle a problem that seemingly many Android developers have, which is how to intersperse lists with non-list data, in one big scrollable pane.
The model I have in mind is the screen for an individual app in the Market. You have a big description, a list of a few lazily loaded comments, and then some individual items that do different things, like visit the developer's web page, call them, etc. And then in between them all, are nice section headers.
Emulating this approach seems to be extremely hard. I've read enough SO answers and mailing list posts to know not to put a ListView inside of a ScrollView, but I want the same effect without using addHeader() and addFooter() with very complex header and footer views.
I've tried using a LinearLayout that I stock with views myself, but I can't get the pleasant click effects that default list items have (the orange background, white for long-click, etc.).
What do I do?
Take a look at my MergeAdapter, which is designed to handle scenarios like this.
Why not use a header? It's easy. Define the header contents in a separate layout. Your activity layout contains nothing but the ListView that you want at the bottom. No scroll view!
Then call
View headerView = getLayoutInflater().inflate(R.layout.header_layout, null);
ListView listView = (ListView) findViewById(R.id.my_list_view);
listView.addHeaderView(headerView, null, false);
It's crucial to call that form of addHeaderView so that the header is disabled. Otherwise, it can be selected, which looks totally weird.
Mark's example would work. basically you need an adapter with different view types. Another nice example is
http://jsharkey.org/blog/2008/08/18/separating-lists-with-headers-in-android-09/
which might work better than Mark's because you want to have separators and group things together.