I've modified a list view to have a different selection color and item backgrounds. Things are working perfectly until I set a custom color on the application or activity background (solid color, png, gradient). I tried using cache color hint also, but it does not seem to work.
I've been on google pretty much the whole of yesterday and haven't found an answer. The closest I've come is this thread:
The gentleman seems to say that this is as designed. If it is I'd like to hear how I could change the background color of the entire list view (I'm talking about the area in the list view that may be empty).
If you want to look at some code you will find it here.
Thanks
Manish
I'm not entirely clear on what you are asking, so let me explain how all the items that you are playing with work together, and perhaps your answer will be in there somewhere. There are three distinct visual elements that you have described in your question:
A list item's background
The list selector
The overall ListView's background
By default, they will be drawn in that hierarchy, meaning (1) is drawn on top of (2), (2) is drawn on top of (3), and so on. On a ListView with no customization, items 2 & 3 are transparent and the selector is a drawable with multiple states. Because of this, any custom colors you apply to the Activity or any View beneath the ListView is going to show through, due to the transparency.
If you look at the order in which these things are drawn, you can start to see how customizing one element may cover up any customization done to another element. For example, if you set an opaque background on each item of the list, and listSelector attribute the list has will be completely covered up (unless drawSelectorOnTop is set...which is why when developer do that they provided a drawable with states as the background item).
ListView is just another view, and you may set its background attribute directly if you wanted to provide a background to the entire ListView and hide the Views displaying underneath it. Again, if you were to set a custom list item background, this would not be visible except for areas where the ListView is large enough to display all its elements and there is extra space below them.
The job of cacheColorHint is to optimize scrolling performance by telling each list item to draw itself with a solid color background (while scrolling) instead of being transparent. If you set a solid color background on ListView or the Activity beneath, this value should be set to the same color. If your background is a gradient or image, you cannot use this optimization and it should be set to transparent (i.e. #0000)
Hope that Helps.
Related
For my list view, each row holds a view (which is a custom view that extends LinearLayout). However, the blue highlighting doesn't appear when you do an onHold action on a list view item. In my experience, this is default, for which you don't even need to code for. How does one turn on or turn off this?
It's not a matter of turning it on or off. It's actually on, but you can't see it.
The "on hold color" effect is achieved via the ListView's selector. It's a state list drawable, means it's displayed differently depending on the view's state (e.g. pressed, selected, enabled, checked, &c).
The only problem is that this selector is drawn behind the item view. Therefore, if the item view is opaque (i.e. it has a non-transparent background) it won't be visible.
The easiest solution is to make your own background a state drawable, and make it transparent when the item is selected, hence the built-in selector will be visible. Another option, if you want, is to set your own selection color, via the same method.
Please check Romain Guy's World of ListView from I/O 2010, around the 31:00 mark (slides here, it's on slide 36).
I have a list view in my app with elements where an image covers pretty much all of the view. There was an article by google where they explain that selectors should be drawn on top of the item if the item's background is not visible or else you wont see the selection highlight. Can't find this article anymore.
The list view has been correctly configured with drawSelectorOnTop and a selector for various states. Each item on a list view is a RelativeLayout with a background selector for various states including activated. When selecting multiple items and marking them as checked, the items are correctly marked but since a big image covers almost all of the view, just a fraction of the background is shown in the highlighted color.
I fixed my problem by wrapping the relative layout into a frame layout and setting a foreground drawable to draw the highlights. It works, but I don't like adding the extra layout on my elements.
Ideally, the selector of the listview should be used to mark the items, but apparently not. I included all possible states on the list view selector but it never leaves the item highlighted.
My question is: is wrapping items in a framelayout the correct way of doing this or is there a better approach?
i was requested to make in android a view that groups several items like checkboxes or text views in vertical rows, separated by transparent dividers while the background is with a certain alpha level and the edges are round.
I thought of two solutions and i hope for some feedback on good\bad or other solutions if you got'em.
just use regualr linear layout but have a single style A that uses a 9 patch as background, includes padding,margins and whatever i need to make it look like what i want. i then create another style A.up and A.down that represents the upper most and lower most items that will use a different 9-path with round corners.
inherit from linear layout, in the onMeasure and layoutChildren add to all the children some kind of space between them, i can create new attribute for it that can be customized in a style. i can override the dispatchDraw to paint the background for each view before it draws so i can paint my round borders, my only demand will be that each View added to this layout will have to be with transparent background.
So what do you think ?
Eventually i decided to use a List with customized divider.
It looks good, however a list got a very nasty bug when it comes down to items with states like buttons and clickable textViews,
you get no focus for the item and don't see the ornage bar
you don't seem to get the evnets flowing to the children of the View in the list.
I'm notsure how to resolve that one, i've seen numerous mails about it in the developres mailing list and here, most saying don't put statefull objects in a list.
So it mist not be the solution for me.
Nest thing i'll try is extending the normal layouts to have a bar in their bottom and use regualr linear layout with round corners drawable.
How to do the following stuffs in ListView
Only show scroll bar when user flip the list.
By default, if the list is more than the screen, there is always a scrollbar on the right side.
Is there a way to set this scrollbar only shows when user flip the list?
Keep showing the list background image when scrolling.
I've set an image as the background of the ListView, but when I scroll the list, the background image will disappear and only shows a black list view background.
Is there any way to keep showing the list background image when scrolling?
Don't show the shadow indicator.
When the list has more items to display, there is a black-blur shadow to indicate user
that there are more items. Is there a way to remove this item?
The issue with the black background is easy to fix. It's an issue with the cacheColorHint - a way to tell Android what the background color of your list is so that it can more easily create the "fading edge" at the top and bottom of your list when the user is scrolling. However, if your background isn't a solid color then the cacheColorHint does more harm than good. See this post from the Android developer's blog for more information. The way to fix it is just to add this to your ListView xml:
android:cacheColorHint="#00000000"
You can change that color code to whatever you want; it's in AARRGGBB format, also sometimes known as HTML color codes but with an extra 2 digits in front for transparency.
As for removing the scrollbar unless the user is actually scrolling, I'm not sure. Experiment with the various android:scrollbar... settings. If you're using Eclipse, you can type out android:scrollbar and then hit ctrl + spacebar to make it suggest options.
You can just turn scrollbars on and off using setVerticalScrollbarEnabled(). The "shadow" indicator is called the fading edge in our APIs. There are various methods to control the fading edges in the base View class.
I have a simple ListView and on that ListView I have placed a number of custom defined Views. The CustomView has ImageView and two TextViews.
The CustomView also has a "stateful drawable" as background, so that the background image (a 9-patch) changes if you press the Row in the ListView. When pressing the Row, the background image changes to a Red-ish thing.
The problem is that when the background changes from the default greyish, all the Views in the CustomView (ImageView and TextViews) still have their greyish background and thus creates very ugly greay boxes on top of the now redish background.
What is the best way to solve that problem? I hoped that such things were handled automatically (as it is done in for example .NET), but I was wrong it seems.
I got it =)
You can use the color "transparent"
android:background="#android:color/transparent"
Set the Views with that color, and its all good =)