Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Button text color change programmatically

Tags:

java

android

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);
}
like image 650
ono Avatar asked Dec 01 '25 19:12

ono


2 Answers

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);
  }
}
like image 194
CommonsWare Avatar answered Dec 03 '25 11:12

CommonsWare


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).

like image 36
MH. Avatar answered Dec 03 '25 12:12

MH.



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!