I assigned OnTouchListener one of my Button and now when I push the sidebar ( DrawerLayout ), the Button also works at the same time. OnTouchListener been assigned to handle only the press of a button. How to make DrawerLayout intercept onTouch from buttons (buttons are located on the main Activity )? I would be grateful for the help.

  • one
    Why onTouch if there is onClickListener? - Silento pm
  • I just need to separately process DOWN and UP. I am a newbie, so I haven’t found another way yet - Wlad
  • I did not come across this, but I will assume that when you move forward - the sidebar causes the onTouch callback to react, try throwing the panel into a fragment, or a dialogue, to see if this is the case. If yes, then you can already proceed from something - Silento

1 answer 1

 View.OnTouchListener listener = new View.OnTouchListener() { @Override public boolean onTouch(View v, MotionEvent event) { if(event.getAction() == MotionEvent.ACTION_DOWN){ // действие при нажатии на кнопку return true; } return false; } }; button.setOnTouchListener(listener); 

.ACTION_DOWN - the state of the button, there are many, the studio will tell =)

UPD

To make an animation of pressing (reducing) a button, do the following: In the drawable folder drawable create a button_test.xml :

 <?xml version="1.0" encoding="utf-8"?> <selector xmlns:android="http://schemas.android.com/apk/res/android"> <item android:drawable="@drawable/button_t" android:state_pressed="true" /> <item android:drawable="@drawable/button_t" android:state_focused="true" /> <item android:drawable="@drawable/button_n" /> </selector> 

where _t picture of the state of the pressed button, _n is released, normal. And then in the markup of the activation or where your button is located, set the attribute of this button

 <Button ../ android:background="@drawable/button_test" /.. /> 

UPD2

 View.OnTouchListener listener = new View.OnTouchListener() { @Override public boolean onTouch(View v, MotionEvent event) { if(event.getAction() == MotionEvent.ACTION_DOWN){ button.setScaleX(0.5f); button.setScaleY(0.5f); return true; } if(event.getAction() == MotionEvent.ACTION_UP){ button.setScaleX(1.0f); button.setScaleY(1.0f); return true; } return false; } }; button.setOnTouchListener(listener); 
  • Did I understand correctly - I should not have hung OnTouchListener on the Activity and button.setOnTouchListener (this). And instead do as above? I'll try it now - Wlad
  • @VladVitvitsky, create a listener in onCreate and hang it in the same place on your desired buttons, as in the example above. - Pollux
  • Did not help. I understand that when you move your finger, the buttons do not have to react, as with OnClickListener. How to do it with OnTouch? - Wlad
  • @VladVitvitsky, in fact, the Drawer should not touch the activation buttons at the time of nomination, it should block them. Maybe in the code itself Drawer you have attributed something extra. - Pollux
  • I'll try to ask otherwise. How to make the button so that when pressed (touching) decreases, and when released it returns to its original size. I tried OnKeyListener, there is no reaction at all. - Wlad