Changing button color dynamically on Android

Asked

Viewed 42 times

0

I have a menu, with 3 buttons, and I need to change their color dynamically in code.

I’m currently doing it this way:

mDrawable.setColorFilter (0xff0099cc, PorterDuff.Mode.SRC_IN);

What happens is that it correctly applies the color button, but when I change Activity and click another button, the first button that is activated always stays with the color as if it were still activated. *This menu is included in the three activitys.

In the image below represents what happens, when I click on another button, it activates the other but the first remains with the color yet.

inserir a descrição da imagem aqui

  • When you change the color of a button you have to return the original color to the other buttons

  • @ramaral, even when I change Activity and everything is recreated? See that the color of the text Home, back to normal... just the icon that does not...

  • Not in principle, if indeed everything is recreated. You would have to see the code to know if in fact "everything is recreated".

  • From what I understand well... you are opening an Activity when the click on the button is made?

1 answer

0

I would need the code to see how you are doing, but basically, when you start an Activity, the previous one is destroyed, that is, this tablayout would be destroyed and recreate a new one. Then you would have to for each Activity, set on onCreate() the related colors, for example, if you are on the account screen, you would select the blue colors for that (icon and text of the account) (Remembering that by default, all icons and text will be white). Then for each Activity you would choose the color of the page you are on.

Another way that you could use to set colors automatically, would be passing information from the previous Activity to whatever you want... this information would be precisely the color of the icon and you could pass at the time of click.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.