Android ListView default onHold(?) color - android

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).

Related

Android - correct way to highlight multiple image items in Grid/Listview

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?

How does listSelector of ListView work?

Romain Guy himself states that TouchMode does not have any selection or focus in this link. I used android:listSelector="?android:colorPressedHighlight" for a listview and the items I touched retained a blue background. How does this work if TouchMode doesn't have selection?
If listSelector is not the prescribed way to show selected items in touch, how exactly is it done in the youtube app (the red bg in the attached image).
listSelector on ListViews are for phones with bezels/D-pads or GoogleTVs with D-pads
Notice that once you touch them, they remain highlighted. That's because the touch event is mapped to a click event and the selection stays highlighted because that's the only way to keep track of what you've selected (assuming your screen is not touch enabled and for instance you're only controlling the device from the Directional Pad of your remote control).

setting custom background overrides ListView customization

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.

How do you get views to flash red when clicked?

I've noticed that in most stock Android apps when you click on a View of pretty much any sort, the background of the View briefly flashes red before proceeding with whatever it's supposed to do.
I can't figure out how to do this. In some cases it seems to be built in, but not always. The Button objects I have in my Activity all flash red when clicked and I didn't have to do anything to make that happen. However, the items in my ListView do not. Each row in my ListView corresponds to a ViewGroup containing multiple other views. I want the whole row (ViewGroup) to flash red when it's clicked.
I should also point out that, functionally, everything is working properly. I click on my list item and it responds just as I designed it. It just doesn't flash red. Anyone know what I'm missing?
You could just add this to your main layout used for your items in your list :
android:addStatesFromChildren="true" android:background="#android:drawable/list_selector_background"
When one child is selected then all the parent (i.e your layout / the whole row as you said) will get selected, same for other states as pressed, etc. And the selector will apply a short transistion.
Btw, red is vendor dependent, on my Archos, it's a nice orange.
Regards,
Stéphane

How to do the following in ListView

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.

Categories

Resources