Reyjohn
Reyjohn

Reputation: 2734

How to get the current text color of a button?

I want to check in a button click event if the current text color of that button is red or not?

I have done this so far:

ColorStateList mList = gridcell.getTextColors();

            int col=mList.getDefaultColor();


            switch(col)
            {
            case Color.RED:
                Toast.makeText(getApplicationContext(), "RED",
                        Toast.LENGTH_SHORT).show();
            break;

            }

But when I click on the button which's text color is red it doest toast any thing, The defaut color is white and its getting white in all te buttons. What can I do now?

Upvotes: 1

Views: 1388

Answers (3)

AkashG
AkashG

Reputation: 7888

Try this:

        Button button=(Button) findViewById(R.id.b);
        button.setTextColor(Color.RED);
        button.setOnClickListener(this);

on click event of button:

public void onClick(View v) {
        if(button.getCurrentTextColor()==Color.RED){
            Toast.makeText(getBaseContext(), "Red", Toast.LENGTH_SHORT).show();
        }
    }

Upvotes: 0

Furqi
Furqi

Reputation: 2403

plz check this

 ColorStateList mList = mButton.getTextColors();
 int col = mList.getDefaultColor();

 switch(col){
 case Color.RED:
 Toast.makeText(getApplicationContext(), "RED", Toast.LENGTH_SHORT).show();
 break;

 case Color.BLACK:
 Toast.makeText(getApplicationContext(), "BLACK", Toast.LENGTH_SHORT).show();
 break;

}

Upvotes: 0

Kumar Vivek Mitra
Kumar Vivek Mitra

Reputation: 33534

Try this...

ColorStateList mList = mButton.getTextColors();
int color = mList.getDefaultColor();

switch(color)
{
case Color.RED:
Toast.makeText(getApplicationContext(), "RED", Toast.LENGTH_SHORT).show();

break;

case Color.BLUE:
Toast.makeText(getApplicationContext(), "BLUE", Toast.LENGTH_SHORT).show();
break;

}

Upvotes: 2

Related Questions