1

I have an app with "menu" buttons. When I click on a button, a fragment is loaded. I want to assign a particular style on the button when it is clicked to indicate that it is active.

In js/html, it is so easy, I just add a class "active" but from I have read while searching online, it is not possible to change the style of a button in android once it has been set?? any alternative?

yeahman
  • 2,737
  • 4
  • 21
  • 25

1 Answers1

0

Since you only want to mark the button (option) as active while that fragment is loaded. You can try something like this :-

myActiveButton.setOnTouchListener(new OnTouchListener() { 

        @Override 
        public boolean onTouch(View v, MotionEvent event) {
            myActiveButton.setPressed(true); 
            return true; 
        } 
    }); 

One thing you need to keep in mind, is while user selects the other button you will need to change the state of the active button. So, you can keep some sort of variable to know the selected button.

or you can do something like this, consider the answer written by @methode.

Let me know if it helps :)

Community
  • 1
  • 1
Dave Ranjan
  • 2,966
  • 24
  • 55
  • Or, you can just change the background color of the button `onClick`. – Alpana Chauhan Mar 06 '16 at 07:41
  • yup you can change the background color but it becomes tedious for maintenance... keeping track of the background color etc.. wanted something like jquery toggleClass – yeahman Mar 06 '16 at 18:05