2

I want to put some style in the ListView in such a way that when an item of ListView is hovered, the color of that list item changes. I dont have any idea about how to achieve this.

Please suggest some other ideas about styling the ListView as well.

Regards,

user182944
  • 7,897
  • 33
  • 108
  • 174
  • 1
    See these links: (1) http://www.hambonious.com/2010/06/customizing-android-listview-colors.html (2) http://www.typeoneerror.com/articles/post/android-changing-listview-background-colors.......... I hope this may help you – Krishna Suthar Apr 21 '12 at 06:17
  • http://stackoverflow.com/questions/4051297/how-do-i-style-selected-item-in-android-listview – Khan Apr 21 '12 at 06:18
  • you can do it dynamically if you are using custom adapter for your listview. In your getview method you will be inflating the view. There you can set background color whatever you want. May be it will help you. – Bharat Sharma Apr 21 '12 at 06:25
  • Hi, Can you give a sample code for the same? – user182944 Apr 21 '12 at 08:17

1 Answers1

1

Use selector to define what should happen when you focus the item, select the item and press the item, I doubt you cannot hover over a view, since Android devices use touch screen interface and not a pointing interface like a mouse..

This should help you:

Create a XML file in drawable folder named listselector.xml

 <?xml version="1.0" encoding="utf-8"?>
 <selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:state_focused="true" android:drawable="@color/blue" /> <!-- focused -->
    <item android:state_focused="true" android:state_pressed="true" android:drawable="@color/blue" /> <!-- focused and pressed-->
    <item android:state_pressed="true" android:drawable="@color/green" /> <!-- pressed -->
    <item android:drawable="@color/white" /> <!-- default -->
 </selector>

Then set the background of your TextView which you use in ListView to this like

android:background = "@drawable/listselector",

that should do it.

Arif Nadeem
  • 8,524
  • 7
  • 47
  • 78
  • Hi, I am getting this compilation error message: No resource found that matches the given name (at 'drawable' with value '@color/blue'). listselector.xml Please help – user182944 Apr 21 '12 at 07:29
  • Create a new XML file called colors in values folder in your project add HTML color codes for blue green and white into that XML file like this #00ff00 #0000ff and all the other colors you want and reference it back using android:drawable="@color/blue" in listselector.xml file – Arif Nadeem Apr 21 '12 at 07:35
  • ListView does not contains any TextView, i am binding the items of Listview using a List. This is the code for my ListView: TheListView background is red and focus is not working, on clicking it becomes blue for a moment and again becomes red – user182944 Apr 21 '12 at 07:58
  • This is my color.xml: Colors.xml: #ff0000 #00ff00 #0000ff #000000 – user182944 Apr 21 '12 at 08:02
  • use TextView in separate xml file and inflate it as a row in your ListView see this example for more http://www.technotalkative.com/android-%E2%80%93-listview-%E2%80%93-2-custom-listview/ – Arif Nadeem Apr 21 '12 at 10:49