0

I have some textViews in my LinearLayout. They are clickable and I would like to make them onClick like for a ListView. For the listView, when the user clicks an item, the background becomes green I think.

I know that i can do this manually with

tv.SetBackgroundColor(Color.GREEN);

But is there something automaticaly to do this, like for the listView where the selection is managed automaticaly.

Thank you.

Milos Cuculovic
  • 19,631
  • 51
  • 159
  • 265
  • By default listview is defined with such particular feature. I think we cant do that for textview.. – code_finder May 31 '12 at 08:11
  • Check this [answer][1] [1]: http://stackoverflow.com/questions/10713929/change-listview-item-background-on-click-android It may help you – Krishna Suthar May 31 '12 at 08:14

1 Answers1

1

You need to define the background as a new XML file containing a list of states.

http://developer.android.com/guide/topics/resources/color-list-resource.html

For example, create a file called background_states.xml in your drawable folder and write something like this:

<?xml version="1.0" encoding="utf-8"?> 
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item
    android:state_selected="true"
   android:drawable="@color/white" ></item>
<item
    android:state_pressed="true"
   android:drawable="@color/white" ></item>
<item 
    android:drawable="@color/black" /> 
</selector> 

Then define this new file as background in your TextView:

<TextView
   android:layout_width="wrap_content"
   android:layout_height="wrap_content"
   android:background="@drawable/background_states"

See the link above for more information about the different states.

MRD
  • 7,146
  • 1
  • 21
  • 21
  • This seems to be a good idea. I have one problem, I am getting this error in the xml file: error: Error: No resource found that matches the given name (at 'drawable' with value '@Color/green'). – Milos Cuculovic May 31 '12 at 09:04
  • The format @color/color_name is used for custom colors and they must be defined in your res/values/colors.xml file. If you want to use Android predefined colors, you should write @android:color/green – MRD May 31 '12 at 09:06
  • Hm, this is not working for me, I am adding dynamicaly (programatically) the TextViews in the linear layout. What I dit is for each TV, i am using tv.setBackgroundColor(R.drawable.background_states); but when clicked, nothing changes. – Milos Cuculovic May 31 '12 at 09:17
  • Can you try with tv.setBackgroundResource(R.drawable.background_states)? – MRD May 31 '12 at 09:22
  • I am getting this error now E/AndroidRuntime(2768): Caused by: org.xmlpull.v1.XmlPullParserException: Binary XML file line #5: tag requires a 'drawable' attribute or child tag defining a drawable – Milos Cuculovic May 31 '12 at 09:28
  • It looks like you don't have defined the android:drawable attribute in one item in your R.drawable.background_states. Can you please edit your question copying your code on this file? – MRD May 31 '12 at 09:36
  • let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/11978/discussion-between-miguel-rodelas-and-ana) – MRD May 31 '12 at 09:38