Home > Enterprise >  How to change the background color depending on the text view
How to change the background color depending on the text view

Time:11-02

I would like to change the background color depending on the bluetooth status. When the button is clicked, the text (Bluetooth on) appears in the text view. After the second press of the same button, the text (Bluetooth Off) appears and I wanted to change the background color depending on the text. `


switch (Tv.getText().toString()){
    case "Bluetooth ON":
        layoutleft.setBackgroundResource(R.color.Green);
        break;

    case "Bluetooth OFF":
        layoutleft.setBackgroundResource(R.color.Red);
        break;


}

`

I tried to do it like this: (code ) seems to work but doesn't change dynamically until after turning the screen off and on.

CodePudding user response:

Did you already try using a different method on the TextView?

There are lost of different methods for just the background, such as Tv.setBackgroundColor(R.color.Green);

Tv.setBackgroundResource(R.color.Green) just looks into the drawable folder for a Resource called whatever you've named it :).

Hope this solves your problem.

CodePudding user response:

switch (Tv.getText().toString()){
    case "Bluetooth ON":
        layoutleft.setBackgroundColor(Color.parseColor("#5CE058"));
        break;

    case "Bluetooth OFF":
        layoutleft.setBackgroundColor(Color.parseColor("#FFF6A5"));
        break;


}
  • Related