Javanes
Javanes

Reputation: 91

Change View Text Color by code

Well, I developed a menu with eight Buttons for an App. So, every time the user clicks on in one of the buttons, such button changes its background. And I would would like to change its color as well. But I got now idea how, since setTextColor does not work with Views.

I'm using View because its part of onClick method that I override in order to achieve what I want. So, here go the code:

@Override
    public void onClick(View v) {
        // TODO Auto-generated method stub

        v.setBackgroundResource(R.drawable.degrade_menu);

    }

So, how could I change the text color?

Cheers,

Upvotes: 0

Views: 72

Answers (3)

dreambit.io dreambitio
dreambit.io dreambitio

Reputation: 1902

Cast your v to TextView and then set the text color. Do not forget to read color from resourse

((TextView)v).setTextColor(getResources().getColor(R.color.errorColor));

Upvotes: 1

Arno van Lieshout
Arno van Lieshout

Reputation: 1660

Cast the view to a button. Then you can use settextcolor

Upvotes: 0

mbmc
mbmc

Reputation: 5105

quick solution:

final Button button = (Button) findViewById(<id>);
button.setOnClickListener(new OnClickListener() {
    @Override
    public void onClick(View v) {
        button.setTextColor(<color>);
    }
};

better solution: use states

Upvotes: 0

Related Questions