1

How to change button text color On Clicking the button using hexa-decimal value? I'm using the below code on MainActivity but not working

button.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {

           // button.setTextColor(Color.GRAY);
            button.setTextColor(Color.(#808080));
         }
    });
aminography
  • 21,986
  • 13
  • 70
  • 74
Irfan Akram
  • 29
  • 1
  • 1
  • 11

5 Answers5

2

You should use Color.parseColor to get the int value of hex color string like below:

button.setTextColor(Color.parseColor("#808080"))
aminography
  • 21,986
  • 13
  • 70
  • 74
1
button.setTextColor(Color.parseColor("#ff0000")); 
Khemraj Sharma
  • 57,232
  • 27
  • 203
  • 212
Chirag Nahar
  • 159
  • 8
0

You can use this way

button.setTextColor(Color.parseColor("#ff0000")); 
mehul chauhan
  • 1,792
  • 11
  • 26
0

Define the color in your res/values/colors.xml file and use it like this:

button.setTextColor(getColor(R.color.your_color_name));

user8886048
  • 109
  • 6
0

It can be in a two way

Define Your color in the res/values/colors.xml

  1. Use it on your Activity.java

    button.setTextColor(getColor(R.color.defined_color_name));

  2. Use it on activity_name.xml

            <Button    
            android:id="@+id/button"    
            android:layout_width="wrap_content"    
            android:layout_height="wrap_content"    
            android:text="My Button"    
            android:textColor="@color/nameOfYourColor"  /> 
    
InsaneCat
  • 2,115
  • 5
  • 21
  • 40