0

I am trying to change the text color of button on click event. But while the button click event is fired, button goes missing. Code mentioned below.

Button design in Layout XML file

<Button
         android:id="@+id/btnCategory1"
         android:background="#000000"
         android:layout_width="0dp"
         android:layout_height="wrap_content"
         android:layout_weight="1"
         android:textColor="#FFFFFF"
         android:layout_margin="10dp"        
         >
         </Button>

Activity.java file

@Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_add_expense);

        btnType1 = (Button)findViewById(R.id.btnCategory1);

        btnType1.setOnClickListener(this);
        }

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

        if(v == (View)btnType1)
        {
            btnType1.setTextColor(R.color.darkorange);      
        }           
    }

Tried the below Option also. Still Button Goes missing. Log statement is fired successfully.

btnType1.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                Log.v("AAAAAAAAAAA","BBBBBBBBBBB");

                // TODO Auto-generated method stub
                btnType1.setTextColor(R.color.orange); 

            }
        });

If someone can find the reason, kindly share it.

2 Answers 2

2

You cannot use just the R.color integer when calling setTextColor. You need to call getResources().getColor(R.color.YOURCOLOR) to set a color properly.

Make your button as below

Button bOne = (Button) findViewById(R.id.btnCategory1);

        bOne.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
                bOne.setTextColor(getResources().getColor(R.color.YOURCOLOR));

            }
        });
Sign up to request clarification or add additional context in comments.

Comments

2

Hmmmmm. I'm not seeing a good reason as to why that would happen.

I do think that there is maybe a better/cleaner way to do something so simple, and so I'll tell you it - go ahead and try it. This was should work.

Get rid of the btnType1.setOnClickListener(this); line from your java.

Then, go into your xml and add this to your button:

android:onClick="methodName"

Now, if you go into your java and create a method called methodName that takes a view as an argument:

public void methodName(View v) {
    btnType1.setTextColor(R.color.darkorgange);
}

The color should be updated!

EDIT: Just looked again and the reason the previous code wasn't working was because you were trying to update btnType2, not btnType1. Still, try the method I just gave you. It is good practie, and a cleaner and easier way to do things for the future.

EDIT2: All right, the mystery is solved. Here is your issue. When you do set color, you need to pass in the actual color, not just the id. Here is what you need to change that line to:

btnType1.setTextColor(getResources().getColor(R.color.darkorange));

4 Comments

That is very strange indeed. I'm not sure what to tell you. Maybe try changing the command to btnType1.setBackgroundColor(R.color.white); or something like that. See what happens.
OHHH your error is different. You aren't passing the right thing to your class. See my revised answer.
It was a typo. I am updating btnType1 only
Adding the code you mentioned in edit 2 gives me error. It could not be compiled

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.