Search code examples
androidpalettefloating-action-button

Apply Palette color to fab on pressed state


can someone give me a hint how I apply, a color generated by the Palette class, to a fab-button for the pressed state?
I am using a Listener to get the generated colors as discribed here, I can apply the default-state background color in the public void setPalette(Palette palette) action using fab.setBackgroundColor(palette.get...()) but I don't know if there is a method to set the pressed-state background color, or if I have to use an OnClick or OnTouch Listener on the fab to set the appropriate background color on the event.


Solution

  • Hey i know this is late but it might help someone

    You do the below declaration of variables

    int[][] states = new int[][] {
                            new int[] { android.R.attr.state_enabled}, // enabled
                            new int[] {-android.R.attr.state_enabled}, // disabled
                            new int[] {-android.R.attr.state_checked}, // unchecked
                            new int[] { android.R.attr.state_pressed}  // pressed
                    };
    
                    int[] colors = new int[] {
                            Color.RED,
                            Color.BLUE,
                            Color.GREEN,
                            Color.YELLOW
                    };
    
    (Button) btn = (Button)  findViewById(R.id.fab).setBackgroundTintList(new ColorStateList(states, colors));
    

    And your are all done, i spend a ton of time looking for this. hope it helps someone.