Search code examples
androideventslistviewonclickhighlighting

Hightlight the ListView row when a contained UI element of row is touched


I want to enable the orange highlight that occurs when touching a listView row.

If the onclick event was generated from the row itself, I can adjust the hightlight by setting the style of the listview. However in my case, the clicking event is generated by the TextView underneath with onclick event attached to it.

When touch event occurs, Listview isn't aware of clicking is happening. It doesn't receive focused or pressed event.

Is there bubbling or capturing technique I could use like Flex did? Or any suggestion/solution?

Thank you very much.


Solution

  • View tempView = null;  // Class Variable to temporary store Clicked Row's view
    

    Method:

     public void onItemClick(AdapterView parent, View v, int position,long id)
    {
        listView.setFocusable(true);
        listView.setSelected(true);
        v.setBackgroundColor(Color.LTGRAY); // CHANGE COLOR OF SELECTED ROW HERE>
        selectedId = (int)id;
    
        if(tempView != null){
            //If row is already clicked then reset its color to default row color
            tempView.setBackgroundColor(Color.TRANSPARENT);
    
        }
        tempView = v;
    

    }

    Hope this helps. Thanks :)