Office Hours — Today, April 14

Yesterday, April 13

Apr 14
8:50 AM
Mark M.
has entered the room
Mark M.
turned off guest access
Mark M.
turned on guest access
9:05 AM
Lucy
has entered the room
Mark M.
hello, Lucy!
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... :-)
could you be more specific?
Lucy
More specifically, I have a ListView where the row is a TextView and an ImageView.
The TextView is populated via a String Array (String[]). If the String array is empty then I set the image to INVISIBLE.
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.
It seems like a straightforward thing that I wanted to do...
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
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
what sort of a ListAdapter are you using for the ListView and its rows?
Lucy
View paste
	class TitlesAdapter extends ArrayAdapter<String> {
If I set the ListView's layout_height to "fill_parent" then it works as expected. The first item's image is drawn.
(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
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.
Well, maybe here is a different question that could solve it...
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.
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?
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...
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.
That's a good point. No, I have not stepped in with a debugger. Will do that next.
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
set the ImageView height to 0dip and weight to 1
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!
Weight is for LinearLayout only I believe?
Mark M.
correct
9:25 AM
Lucy
THAT WORKED!!!
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.
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
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
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
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.
Thanks again. That was my only question for today. I appreciate it!
Mark M.
you are very welcome
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
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.
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.
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.
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
(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.
This was a very helpful session for me, thank you!
Mark M.
you're welcome!
10:00 AM
Mark M.
that's a wrap for today's chat
the transcript will be posted to http://commonsware.com/office-hours/
the next chat is Thursday at 4pm US Eastern
have a pleasant day!
Lucy
has left the room
Mark M.
turned off guest access

Yesterday, April 13

 

Office Hours

People in this transcript

  • Lucy
  • Mark Murphy