Aakash Anuj
Aakash Anuj

Reputation: 3871

Android Button color not changing programatically

I have this piece of code, which is expected to change the color of the button to BLUE, but it does not have any effect. The code goes into this if statement but does not change the color. On the other hand , the same statement when used earlier does actually change the color of the button. Why is this so?

   if(t.equals("a"))
                 {
                     Toast toast5=Toast.makeText(getApplicationContext(),"a found", Toast.LENGTH_SHORT);  
                     toast5.show();          
                     btn6.getBackground().setColorFilter(Color.BLUE, PorterDuff.Mode.MULTIPLY);
                 }

Upvotes: 0

Views: 238

Answers (3)

Ajay S
Ajay S

Reputation: 48592

// Get Handle for the Tab buttons
Button btnTab1 = (Button) findViewById(R.id.button_tab1);
Button btnTab2 = (Button) findViewById(R.id.button_tab1);

// set the colors correctly
btnTab1.setBackgroundResource(R.color.lightblue);
btnTab2.setBackgroundResource(R.color.darkblue);

Upvotes: 3

NAP-Developer
NAP-Developer

Reputation: 3796

You implement for this code,

    btn6.setBackgroundColor(Color.colorChoose);
    btn6.setBackgroundDrawable(Drawable drawable);
    btn6.setBackgroundResource(int resid);

Upvotes: -1

Hardik Joshi
Hardik Joshi

Reputation: 9507

Use

btn6.setBackgroundColor(Color.BLUE);

Upvotes: 0

Related Questions