3

I'm looking for a way to change the color of text in a Button with onClick. I want the selected button text color to change and the other button's text to revert to the default color. This way (below) seems very inefficient. Is there a better way to go about it? Also, how would I revert to the original color with onClick?

public void onClick(View v) {
    switch (v.getId()){
        case R.id.button1:
            TextView textView1 = (TextView) findViewById(R.id.button1);
            textView1.setTextColor(Color.RED);
            logLevel = "E";
            //Change the rest to default (white)
        break;
        case R.id.button2:
            TextView textView2 = (TextView) findViewById(R.id.button2);
            textView2.setTextColor(Color.RED);
            logLevel = "W";
            //Change the rest to white
        break;
        case R.id.button3:
            TextView textView3 = (TextView) findViewById(R.id.button3);
            textView3.setTextColor(Color.RED);
            logLevel = "D";
            //Change the rest to white
        break;
        case R.id.button4:
            TextView textView4 = (TextView) findViewById(R.id.button4);
            textView4.setTextColor(Color.RED);
            logLevel = "I";
            //Change the rest to white
        break;
    }

    retrieveLog(logLevel);
}
Cœur
  • 37,241
  • 25
  • 195
  • 267
ono
  • 2,984
  • 9
  • 43
  • 85

2 Answers2

11

Is there a better way to go about it?

Step #1: Add a TextView[] buttons data member to the activity or fragment

Step #2: In onCreate(), after setContentView(), call findViewById() four times, one per button, and put each button into the buttons array

Step #3: Rewrite onClick() to:

for (TextView button : buttons) {
  if (button==v) {
    button.setTextColor(Color.RED);
  }
  else {
    button.setTextColor(Color.WHITE);
  }
}
CommonsWare
  • 986,068
  • 189
  • 2,389
  • 2,491
  • I believe you also need `button[0].setOnClickListener(this);` in `onCreate();` or something along those lines? – ono Jun 10 '13 at 19:40
  • @ono: True -- I skipped over some of the details not strictly tied to the rewrite of `onClick()`. My apologies for any confusion. – CommonsWare Jun 10 '13 at 19:50
  • Also, how do I assign the logLevel values using this method? Do I still need a switch/case implementation for that? – ono Jun 10 '13 at 20:05
  • @ono: Not necessarily. You could always stuff the log level associated with a given button in the tag property of the button itself, then retrieve it with `getTag()`. – CommonsWare Jun 10 '13 at 20:08
2

Just as with drawables, Android allows you to set up a selector for the text color too. That way you don't have to worry about programmatically changing colors at all, as the framework will take care of that.

For example, in res/color/text_color_selector.xml:

<?xml version="1.0" encoding="utf-8"?>
 <selector xmlns:android="http://schemas.android.com/apk/res/android">
     <item android:state_pressed="true"
           android:color="#000000" /> <!-- pressed -->
     <item android:state_focused="true"
           android:color="#000000" /> <!-- focused -->
     <item android:color="#FFFFFF" /> <!-- default -->
</selector>

Then reference it like any other color:

<Button ... 
    android:textColor="@color/text_color_selector" />

Source: Android selector & text color


Edit: I may have misunderstood the initial question, as it seems like you'll want to persist the changed text color after clicking it. You can potentially still use the above for that, but change your Button into something that supports a checked state. That means that when checked you'll have one color, and when unchecked its inverted version. Obviously, you can style the result in such a way that it looks just like a plain button (without actual checkmark).

Community
  • 1
  • 1
MH.
  • 45,303
  • 10
  • 103
  • 116