After reading Dynamically add elements to a listView Android , I understand clearly how to dynamically add row elements to a listView.
However, I have one idea that how to dynamically add column elements to a listview.
For example, there are 2 columns originally. After I click a button, I would like to add one one more column.
Can anyone provide example code?
Thank for all sharing.
Just add rows dynamically, but make a custom view with two different elements... So there'll be two elements on your every view of row. And first, set first items while seconds are invisible and after clicking button set others... So it'll seem that you're adding column...
Related
I'm making an app where there's one main activity that is a Fragment. I basically have a few tabs and each tab/page layout I want a different layout. For one of them, I want to know if I can dynamically create a table of, say 20-ish rows with X amount of columns. Instead of having to manually create each row inside the layout .axml file.
The reason for wanting to dynamically create it is that I'm obtaining some data from online that I want to automatically put inside a table.
You will only need to create 1 layout for the item and it will be your item 'template' to inflate it into your ListView dynamically. Take a look at this RecyclerView example (which is the improved ListView)
I want to create ListView with 2 columns, something like this:
So I want on different onItemClick for item1 and item2. I can create this layout with custom adapter, but when i click on first row I get InItemClik for whole row. Can someone tell me how can I do this, or give me some useful links....
IMPORTANT:
This is old question, nowadays this type of layout should be created with RecycleView and LinearLayoutManager for one column only or GridLayoutManager for more columns.
add two LinearLayouts(for example) in your custom list xml file and insted of setting OnItemclik listener to list set onClilkListener to these layouts seperately for each.
It should work for you
Use GridView instead of ListView and set it to 2 columns.
I would like to display 2 listview items in the same line, like favourite contacts in the default phone app. I do not need a full working solution, but only a guideline what to do.
Illustration:
There is a list of favourite contacts and there are 2 contacts (illustrated only by pictures) in 1 line.
Thanks!
You can write your own adapter to handle this, or you can use a GridView.
To use the custom Adapter strategy:
Create a row layout that has space for two elements.
In xml, for whatever you now consider one 'row', make a horizontal LinearLayout that can hold two of them next to each other. That's your new definition of a row.
In your Adapter, fill each row with two elements from your data set instead of the usual 1.
Take care to handle the case where the data set only has one element left.
Your alternative is to use a GridView,
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 couldn't find this question already being asked on this forum.
I am little short on time to search anymore.
I have a multi-column ListView with three TextViews.
I need to have header for each of the column. I am using Android 1.5 SDK.
Can somebody help me here. I can't use addHeaderView since it adds just one view which would contain just one title string.
Thanks
Nayn
One easy solution to this will be to use same layout params for the header as you are using for the list item.Eg:
If you have three TextViews, suppose 1st one is right aligned, 2nd one is to the left of this and 3rd one is to the left of second one. Now for the header as you have separate headers for each column, you can use the same layout params for the three column headers i.e 1st one is right aligned, 2nd one is to the left of this and 3rd one is to the left of second one.
This is just an eg. the basic idea is to keep layout params(relative position,margins,padding,etc) same for both of them.
I tried this and it works for me.
I added a separate table above the list view which has all the columns listed. This solves my problem. Just that data is not aligned properly with the columns. I'll think about it later