Search code examples
androidbuttonpressed

android: 2 overlapping buttons to show pressed states


I have 2 buttons overlapping with each other. Is it possible that when pressing the top button, the bottom button also showing its pressed state?

Thanks!


Solution

  • Button has a setPressed method which you could use in one of the buttons onClickListener to set the other button to a pressed state. You could then set an OnTouchListener on that button and listen for MotionEvent.ACTION_UP to set it back to a non pressed state.

    button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                button2.setPressed(true);
            }
    });
    button.setOnTouchListener(new View.OnTouchListener() {
            @Override
            public boolean onTouch(View v, MotionEvent event) {
                if (event.getAction() == MotionEvent.ACTION_UP)
                    button2.setPressed(false);
                return false;
            }
    });