Search code examples
androidtouchhidevisibleinvisible

Hide the button and show again on touch


I am working on an application where I want to hide the button, or, say, make the button invisible when I touch the screen and again make the button visible when I again touch the screen.

How I can make it?


Solution

  • Implement an onTouchListener

     // Catch touch events here
    @Override
    public boolean onTouchEvent(MotionEvent event) {
        if (event.getAction() == MotionEvent.ACTION_DOWN) {
            System.out.println("Touch Down X:" + event.getX() + " Y:" + event.getY());
        } 
        if (event.getAction() == MotionEvent.ACTION_UP) {
            System.out.println("Touch Up X:" + event.getX() + " Y:" + event.getY());
        }
        return super.onTouchEvent(event);
    }
    

    Then in this you'll want to reference the button and set it's visibility:

      button1.setVisibility(View.VISIBLE); or View.INVISIBLE