Sparki
Sparki

Reputation: 161

android passing textView ID to function

I have an onClick like this:

public void onClick(View v) {
    updateTable(v.getId()); //All ID's are Int, e.g. id=400
}

public void updateTable(int intID) {
    TextView itemToChange = (TextView) findViewById(intID);
    itemToChange.setTextColor(Color.RED);
}

So I have the ID of the TextView (id=400) how do I change the colour of it?

What I am trying to do is findViewById(int) or how can I pass v to updateTable() and use

((TextView) v).setTextColor(Color.RED);

Any ideas?

Upvotes: 0

Views: 2057

Answers (1)

Frank Sposaro
Frank Sposaro

Reputation: 8531

If you want to pass v into your function, then why don't you just pass v?

public void updateTable(View view)

You'll still be able to get the id back out with the same function call. Now you don't even need to do findViewById because you already have a handle on the view. Just cast it to a textView.

TextView colorChanger = (TextView) view;

Upvotes: 1

Related Questions