OnClick change tablerow background color
Asked Answered
P

2

3

So I'm trying to find an easy way to get the background color or a table row to change when its clicked on. I've been trying to find a way to call what the background color is and check it but I haven't found a way to call the color. Here is what I have right now.

    RowName = (TableRow) findViewById(R.id.RowName); 
    RowName.setBackgroundColor(Color.TRANSPARENT);

    RowName.setOnClickListener(new View.OnClickListener() {

        public void onClick(View v) {

            if (RowName.equals(Color.TRANSPARENT))
            RowName.setBackgroundColor(Color.YELLOW);

            else if (RowName.equals(Color.YELLOW))
            RowName.setBackgroundColor(Color.TRANSPARENT);
        }
    });

I know that its wrong. Hopefully you can see what I'm trying to accomplish. If not, what I want to do is have the table row start of transparent. When someone clicks on the table row I want it to change to yellow. Then, if they click it again, I want it to change back to transparent. Thanks.

Pollster answered 10/12, 2010 at 15:33 Comment(1)
In case someone is looking for a background color behavior on click like button, see https://mcmap.net/q/112796/-how-can-i-highlight-the-table-row-on-click/427545Suit
P
2

So here is what wound up working. Make sure you have your TableRows named. Before my on create I have

private TableRow RowName;

I also have

int state = 0;

. I then add the code

public void RowName(View view) {
  switch (state) {
  case 0:
      RowName.setBackgroundColor(Color.YELLOW);
      state = 1;
      break;
  case 1:
      RowName.setBackgroundColor(Color.TRANSPARENT);
      state = 0;
      break;
  }
}

To get it to work, go into your xml and in the OnClick property add RowName or the name of the public void that you are working with. Enjoy.

Pollster answered 22/1, 2011 at 21:8 Comment(0)
M
8

You need to set the background color of your row to a state list drawable (that handles select, pressed, active, non-active).

http://developer.android.com/guide/topics/resources/drawable-resource.html#StateList

The XML should look something like this:

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <!--  Active state -->
    <item android:state_selected="true" android:state_focused="false"
        android:state_pressed="false" android:drawable="@android:color/transparent" />
    <!--  Inactive state-->
    <item android:state_selected="false" android:state_focused="false"
        android:state_pressed="false" android:drawable="@android:color/transparent" />
    <!--  Pressed state-->
    <item android:state_pressed="true" android:drawable="@android:color/yellow" />
    <!--  Selected state (using d-pad) -->
    <item android:state_focused="true" android:state_selected="true"
        android:state_pressed="false" android:drawable="@android:color/yellow" />
</selector>
Merciless answered 10/12, 2010 at 17:51 Comment(3)
Ok. Cool. So I have it working for the most part. Is there any way to get it so that it will remain yellow until it is pressed again though?Pollster
Ok I see what you are doing now. Well then the state list is probably not what you want, but rather something similar to what you originally had. Have many rows do you have? Because if you don't have that many, you can keep a variable around that saves the state of the row (instead of looking at the current background).Merciless
I have between 20 and 25 rows.Pollster
P
2

So here is what wound up working. Make sure you have your TableRows named. Before my on create I have

private TableRow RowName;

I also have

int state = 0;

. I then add the code

public void RowName(View view) {
  switch (state) {
  case 0:
      RowName.setBackgroundColor(Color.YELLOW);
      state = 1;
      break;
  case 1:
      RowName.setBackgroundColor(Color.TRANSPARENT);
      state = 0;
      break;
  }
}

To get it to work, go into your xml and in the OnClick property add RowName or the name of the public void that you are working with. Enjoy.

Pollster answered 22/1, 2011 at 21:8 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.