0

I'm trying to get the icon changing color when I press the icon like the toolbar on Instagram or like this Toolbar

I have searched and the only findings is the case of the button with text, I need with Icons.Thanks

Tiutiu
  • 53
  • 1
  • 7
  • you could do at runtime. check this [post](http://stackoverflow.com/a/32657160/3425390). – Akash Patel Oct 27 '16 at 12:47
  • I believe the best way to do this, is to create two kind of icons. That means two states. Unselected and Selected. In your files, have home_icon_selected.png(blue thing) and home_icon_unselected.png.(grey thing) Then onClick of this button(Image/View or whatever) check the current image background/src and if it's not the "home_icon_selected.png" change it to that. – Antonios Tsimourtos Oct 27 '16 at 12:52
  • Thanks @Tony am trying to follow your steps. – Tiutiu Oct 27 '16 at 13:47

1 Answers1

1

If you are using Vector Assets, then you can change the color in the xml with:

android:tint="@color/myColor" or android:tint="#ff00ff"

I recommend you use the colors.xml in the values folder. That way you can change the colors through the whole project at one place :)

Or in the code with:

iconName.setColorFilter(Color.parseColor("#ff00ff"));

If you want the color change to happen on a click, toggle or different event. Then you would just need to apply the corresponding listener. For example a OnClickListener if you want this to happen on a click.

nameOfClickableObject.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            //to do code here
        }
    });

Edit: What are you currently using to display the icons? Have you got anything you could share?