Hide button and show touch again

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

How can i do this?

+6
source share
2 answers

Deploy 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 case you will want to refer to the button and set its visibility:

  button1.setVisibility(View.VISIBLE); or View.INVISIBLE 
+9
source

Try it. This may solve your problem.

  LinearLayout mainLay = (LinearLayout) findViewById(R.id.lLay); int flagForButton = 0; Button b1 = new Button(this); b1.setVisibility(0); mainLay .setOnTouchListener(new OnTouch Listener) { public boolean onTouchEvent(MotionEvent event) { int action = event.getAction(); if (action==0) { } else if (action == 1) { if (flagForButton ==1) { b1.setVisibility(0); flagForButton = 0; } else { b1.setVisibility(4); flagForButton = 1; } } } }); 
0
source

Source: https://habr.com/ru/post/889763/


All Articles