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
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 have a button in each item of a ListView whose background is defined by an XML, one background when enabled and another when disabled. When the ListView loads, it comes out correct. But, for some reason I can't figure out, if I scroll down and then scroll back up, the wrong background shows up.
I'd like to know the solution to this problem, but besides that, in general what I want to accomplish is this:
I have a button in the ListView to take the user to the website for the given item. If there is no website, I want the button to disappear, or be disabled. I seem to have the same problem with both options.
Thanks in advance for your efforts
It seems most likely that the problem lies with your getView() method. Android recycles views to save memory, so, for example, when you scroll down, it calls getView(int, View, ViewGroup) on your adapter where View is the item that just left the top of the screen. If you're not re-populating the item with the new data from the adapter, (ie, just returning convertView) it will put the View that left the top of the screen where the "new" one should be.
I have a problem that would be hard to explain. In my listview the list item's have imageviews. I would like to have an 'on click' or 'on pressed' effect that changes the background of the whole list row, and also the imageviews. I tried 2 approaches:
a) defining the xml selector state list for the row's layout
b) setting an onClickListener in java code
Option a) is no good because I can only change the items background and can't change the imageviews (defining a state list for imageviews is no good because it only works when the exact object is touched. and I want the effect whenever any location of the row is touched)
option b) works almost correct - i set the background resource for the layout of the list item and also change the image resource for imageviews. however, afterwards they stay like that. I am now wondering how can I change back to normal to achieve the effect of a short 'blink' in the java code. Just like with a button - you press it and it blinks orange for a split of a second.
PS: the permanent change of resources is important, because on press I open another activity with item's details and when I press the 'back button' the row looks like permanently selected.
I found a solution with using the OnTouchListener instead of onClickListener.
I have a quite problematic UI layout to implement, and I'm not sure if it's even possible using standard UI widgets. It looks something like this:
Picture 1
The green, lined thing is supposed to be a ListView, and the red rectangle is another View. This red View should be scrolled with the ListView, as if it's part of it. Some list-elements should also be narrower, because that embedded View gets in their way. Could you please recommend any ideas? Can this be done somehow with the Android UI framework? I was thinking about some kind of a floating View above the ListView, which reacts to the List's scrolling events too, but it doesn't seem like an elegant solution.
Thanks
I don't think you can accomplish that easily with a ListView. You could do the overlay using a FrameLayout, but it would be very awkward to get it to stay probably aligned as the user scrolls.
How many elements are you talking about?
I would probably use a LinearLayout within a ScrollPane to simulate the ListView.
Or, a TableLayout where the overlayed view is contained within a single, complex row.
I would set the green rows that the red block overlap and the red block as one big view in the listview. So the items in your listview would be (for the example pic) two green rows, then the view of three green rows and the overlapping red block, and then the remainder of the green rows.
Trying to have the red block on an overlay that scrolls with the listview sounds like more trouble than it's worth.