Apr 14 | 8:50 AM |
Mark M. | has entered the room |
Mark M. | turned off guest access |
Mark M. | turned on guest access |
Apr 14 | 9:05 AM |
Lucy | has entered the room |
Mark M. |
hello, Lucy!
|
Mark M. |
how can I help you today?
|
Lucy |
Hi Mark! I have a layout that I thought was simple and it works 99% of the time. But I have a strange problem. Is that too much of a question for this forum?
|
Mark M. |
well, "a strange problem" is a bit vague... :-)
|
Mark M. |
could you be more specific?
|
Lucy |
More specifically, I have a ListView where the row is a TextView and an ImageView.
|
Lucy |
The TextView is populated via a String Array (String[]). If the String array is empty then I set the image to INVISIBLE.
|
Lucy |
The problem: The first row, while not empty, gets its image set to invisible every time. There are two circumstances where it works. Let me run them right now and I'll post.
|
Lucy |
It seems like a straightforward thing that I wanted to do...
|
Lucy |
Do you want me to post my XML for the Fragment, ListView and single row?
|
Mark M. |
no, because that's not where your problem is
|
Apr 14 | 9:10 AM |
Lucy |
Oh OK. I see.
|
Mark M. |
if you are setting the image to invisible in Java code, and this one position always has its image set to invisible, then the problem is in your Java code
|
Mark M. |
what sort of a ListAdapter are you using for the ListView and its rows?
|
Lucy | |
Lucy |
If I set the ListView's layout_height to "fill_parent" then it works as expected. The first item's image is drawn.
|
Lucy |
(I did see the posts about why using wrap_content is not performant...but that's a different issue.)
|
Mark M. |
wrap_content does not work reliably in general
|
Mark M. |
for ListView, anyway
|
Lucy |
Yeah, after reading much on Stack Overflow I see the problems. Also, I'll mention that if I don't set the image to be invisible and I use wrap_content...then all images do get painted properly.
|
Lucy |
Well, maybe here is a different question that could solve it...
|
Lucy |
The reason I'm using wrap_content for LV's layout_height is because my list is only 5 items long. And I don't want it to display on the whole page. I've tried wrapping it in nested layouts but haven't been successful.
|
Lucy |
Maybe I haven't used the right kind of nesting of my ListView inside a LinearLayout?
|
Mark M. |
what else is on the screen that would take up the rest of the space that is not used by the ListView?
|
Apr 14 | 9:15 AM |
Lucy |
I put an image there. It gets forced off the screen when I set ListView's height to fill_parent. Let me check right now...
|
Lucy |
Yes, I have an image that fills the rest of the screen. I'm emulating only a phone device for now.
|
Mark M. |
Is the image above or below the ListView? And how big do you want the image to be?
|
Lucy |
The image is below the ListView. The image is fairly big. The ListView can take up 3/4 of the space and the image 1/4, for example. My attempts at layout_weight haven't fixed the overall issue where the first item's image doesn't get painted.
|
Mark M. |
"My attempts at layout_weight haven't fixed the overall issue where the first item's image doesn't get painted" -- well, android:layout_weight is the right answer for the size rules that you want
|
Lucy |
btw I'm pretty sure the first item's image is made invisible because I've made the images clickable...and if I click in that area nothing happens, just like the other invisible items.
|
Mark M. |
did you put a breakpoint, or a Log.d() call, or something to see whether your make-the-image-invisible code is being called in this case?
|
Lucy |
Yeah. I doing something wrong. Its only ever the first item that has a problem.
|
Lucy |
That's a good point. No, I have not stepped in with a debugger. Will do that next.
|
Apr 14 | 9:20 AM |
Lucy |
I will try again by using: LinearLayout with weighting for LV and Image. And with fill_parent for LV's layout_height.
|
Mark M. |
no
|
Mark M. |
set the ImageView height to 0dip and weight to 1
|
Mark M. |
set the ListView height to 0dip and weight to 3
|
Lucy |
OK! I'll try that right now!
|
Mark M. |
that will give you your 3/4 to 1/4 split
|
Lucy |
I have not tried what you just described. I'll try it now!
|
Lucy |
Weight is for LinearLayout only I believe?
|
Mark M. |
correct
|
Apr 14 | 9:25 AM |
Lucy |
THAT WORKED!!!
|
Lucy |
WOW
|
Mark M. |
yeah, my general rule is: don't use wrap_content for ListView
|
Lucy |
I'll not use it any more. I saw that it has performance implications, too.
|
Apr 14 | 9:30 AM |
Lucy |
So...to summarize...using those specs essentially says "Do not use height specification, use only weighting to calculate that area of space"?
|
Mark M. |
in effect, yes
|
Mark M. |
LinearLayout makes two passes through the children
|
Lucy |
In other words, neutralize the height spec so that its not used. And since its required to specify height in the layout.
|
Mark M. |
in effect, yes
|
Mark M. |
in the first pass through, LinearLayout uses the specific height (or width for a horizontal LinearLayout)
|
Lucy |
Thank you! I saw posts indicating this trick but I wasn't able to pull it off.
|
Mark M. |
in the second pass through, LinearLayout uses the weights to handle cases where the specific heights are too little or too much for the available space
|
Mark M. |
by setting the heights to 0dip, you basically have all of the available height distributed by weight
|
Lucy |
Its a great trick. Thanks for this information about the two passes.
|
Lucy |
Thanks again. That was my only question for today. I appreciate it!
|
Mark M. |
you are very welcome
|
Apr 14 | 9:45 AM |
Lucy |
I've thought of a new question
|
Mark M. |
go right ahead
|
Lucy |
If the number of items in my ListView becomes more than 5 or if the text in the 5 items is longer sometimes and shorter other times...then the ListView will require a different amount of space each time. My question is: when the ListView uses less space, in the case where the text of the items is minimal, how to get the ListView to not display the extra chunk of space at the bottom?
|
Mark M. |
that is not the layout rule that you requested earlier
|
Mark M. |
more importantly, if the image is not going to use the space, and the list is not going to use the space, what exactly *will* use the space?
|
Lucy |
Yes, I may not have understood this case.
|
Lucy |
I can create an invisible divider to take up the space. If my LV's weight is small enough and is always forced into scrolling mode then I think that would solve it.
|
Apr 14 | 9:50 AM |
Lucy |
The items in my list are arbitrary and thus will affect the size of the actual list at runtime.
|
Mark M. |
"I can create an invisible divider to take up the space" - but that is still empty space
|
Lucy |
I see what you mean.
|
Lucy |
I will think about how else to design this screen.
|
Mark M. |
generally speaking, Android UI designs allocate X space to the ListView, where X is fixed regardless of how many rows are in the ListView
|
Mark M. |
(with the noteworthy exception of zero rows, in which case the ListView might be replaced by something else, a.k.a., the "empty view")
|
Lucy |
OK got it. That makes sense. I see the dilemma.
|
Lucy |
This was a very helpful session for me, thank you!
|
Mark M. |
you're welcome!
|
Apr 14 | 10:00 AM |
Mark M. |
that's a wrap for today's chat
|
Mark M. |
the transcript will be posted to http://commonsware.com/office-hours/
|
Mark M. |
the next chat is Thursday at 4pm US Eastern
|
Mark M. |
have a pleasant day!
|
Lucy | has left the room |
Mark M. | turned off guest access |