views:

91

answers:

3

I have a ListViewItem with a custom gradient background. The default selector no longer highlights these rows anymore. Highlight only works if I set the background to transparent. How can I get the highlight?

<ListView android:id="@+id/symbolsListView"
        android:layout_width="fill_parent" android:background="@drawable/transparent_background"
        android:layout_height="390dp" android:divider="@drawable/ui_divider_line"
        android:cacheColorHint="#00000000" android:listSelector="@drawable/blue" android:drawSelectorOnTop="true">
+1  A: 

Use a <selector> to display a "pressed" state with your gradient background.

mbaird
now my listview turns red everytime I click on a row. i want just the listitem to turn red. check out my selector xml
Sheehan Alam
Perhaps I misunderstood. Are you setting a gradient background on each row, or on the ListView itself?
mbaird
gradient background on each row
Sheehan Alam
+1  A: 

your some states that are focused and window_focused. Below i mentioned the code for selector try this.

<?xml version="1.0" encoding="UTF-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android"&gt;

    <item android:state_window_focused="false" android:drawable="@color/transparent" />

    <!--
        Even though these two point to the same resource, have two states so
        the drawable will invalidate itself when coming out of pressed state.
    -->
    <item android:state_focused="true" android:state_enabled="false"
        android:state_pressed="true" android:drawable="@color/transparent" />
    <item android:state_focused="true" android:state_enabled="false"
        android:drawable="@color/transparent" />

    <item android:state_focused="true" android:state_pressed="true"
        android:drawable="@color/solid_red" />
    <item android:state_focused="false" android:state_pressed="true"
        android:drawable="@color/solid_red" />

    <item android:state_focused="true" android:drawable="@color/solid_red" />

</selector>

Hope it works.

Praveen Chandrasekaran
same results, entire list view turns red
Sheehan Alam
@Sheehan Alam: what is your goal man? do you want to change the List item Selector's color? then you have an attribute `android:listSelector="@color/solid_red"` is enough on the `ListView`.
Praveen Chandrasekaran
Check out my updated code. I tried setting just the listSelector to red. I just want the row that is being highlighted, to have a red highlight instead of the orange one. I don't want the entire listview to change colors, only the highlight for the row being selected.
Sheehan Alam
@Sheehan Alam: Please check that attribute you have set `android:listSelector="@drawable/blue"` and `android:background="@drawable/transparent_background"`. that attributes have a right value.
Praveen Chandrasekaran
Yes, I am using both of those values on another activity and they appear correctly there.
Sheehan Alam
A: 

The current selection in a ListView is highlighted by an additional drawable that you provide using the "listSelector" attribute.

By default, the selector drawable is drawn behind the list item. Therefore, if the gradient background of yout item is opaque, the selector will never be shown.

There are 2 solutions :

  • Make the background gradient transparent by lowering alpha values.
  • Set the "drawSelectorOnTop" attribute to true, so that the selector is drawn on top of the list item. In this case, make sure your selector drawable is not fully opaque.
DavLink
how can I make sure it is not fully opaque?
Sheehan Alam
even when I set drawSelectorOnTop, the entire listview changes color. I just want the row to highlight, not the entire listview
Sheehan Alam
If the entire ListView color changes, you have set a background drawable with states. Change it to a solid color, @null for transparent or a drawable that does not change depending on the state.Follow this rule : no states on ListView background, no states on list item layout background, state-dependent listSelector drawable.
DavLink
@anybody: how can you lower the alpha value of a background image? Can yuo clarify with a code snippet?
kiki