0

I have a table with some rows having a textview and imageview.I am changing the background of row on click using a selector drawable.I also need to change the color of text and the imageview image on click.I tried using selector for textview as explained in here

The color of the textview needs to be changed on click of the parent table row not on the click of the textview.The changed color should not persist.It should be just an indication of the selected row click

but it doesn't seem to work.Any help would be appreciated

Community
  • 1
  • 1
Krishnanunni Jeevan
  • 1,719
  • 1
  • 15
  • 24

2 Answers2

0

A simple way to achieve this is to set the onClick property for the textview in your xml

android:onClick="changeColor"

Then in the activity that the view belongs to have a method

public void changeColor(View v){
    v.setBackgroundColor(color);
}

v will be the view calling the changeColor() method (hence it's the one you want to change the color of)

You can extend this (or write other methods to set as onClick methods for your other views) to change properties of other views by casting v to the appropriate View (i.e. TextView to change the text color, or ImageView for changing an image drawable)

and if you want to find exactly what view is calling the method you can switch on v.getId() with the case statements being the ids set in R i.e. the ones you set with the android:id="@+id/name property

bennettaur
  • 1,243
  • 10
  • 8
  • The color should change on click of parent row not the textview.I don't need to persist the color .So I think changing in java code behind is not the right solution – Krishnanunni Jeevan Aug 22 '12 at 02:22
  • Sorry I glossed over the part where you said you were using a selector. Could you possibly post your XML so we can see what exactly you're doing? – bennettaur Aug 24 '12 at 05:38
0

Set OnTouchListener for Table row as in android TextView : Change Text Color on click

In the ontouch event get the textview child from the row and change its color ..

Community
  • 1
  • 1
Krishnanunni Jeevan
  • 1,719
  • 1
  • 15
  • 24